-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathutil.go
More file actions
214 lines (187 loc) · 6.72 KB
/
util.go
File metadata and controls
214 lines (187 loc) · 6.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"regexp"
"strconv"
"time"
)
// ascii Splash logo. We used to have a package for this
// but it was only for the logo so why not just static-print it?
func ascii() {
fmt.Printf("\n\n")
fmt.Printf(green + "| |/ / / /\\ \\ | |) ) / /\\ \\ | |\n")
fmt.Printf(brightgreen + "|__|\\__\\/__/¯¯\\__\\|__|\\__\\/__/¯¯\\__\\|__| \n")
fmt.Printf(brightred + "v" + semverInfo() + white)
fmt.Printf(brightred + " coordinator")
}
// StatsDetail is an object containing strings relevant to the status of a coordinator node.
type StatsDetail struct {
ChannelName string `json:"channel_name"`
ChannelDescription string `json:"channel_description"`
Version string `json:"version"`
ChannelContact string `json:"channel_contact"`
PubKeyString string `json:"pub_key_string"`
TxObjectsOnDisk int `json:"tx_objects_on_disk"`
GraphUsers int `json:"tx_graph_users"`
}
func delay(seconds time.Duration) {
time.Sleep(seconds * time.Second)
}
// printLicense Print the license for the user
func printLicense() {
fmt.Printf(brightgreen + "\n" + appName + " v" + semverInfo() + white + " by " + appDev)
fmt.Printf(brightgreen + "\n" + appRepository + "\n" + appURL + "\n")
fmt.Printf(brightwhite + "\nMIT License\nCopyright (c) 2020-2021 RockSteady, TurtleCoin Developers")
fmt.Printf(brightblack + "\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in allcopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.")
fmt.Printf("\n")
}
// menuVersion Print the version string for the user
func menuVersion() {
fmt.Printf("%s - v%s\n", appName, semverInfo())
}
// fileExists Does this file exist?
func fileExists(filename string) bool {
referencedFile, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !referencedFile.IsDir()
}
// fileContainsString This is a utility to see if a string in a file exists.
func fileContainsString(str, filepath string) bool {
accused, _ := ioutil.ReadFile(filepath)
isExist, _ := regexp.Match(str, accused)
return isExist
}
// menuExit Exit the program
func menuExit() {
os.Exit(0)
}
func timeStamp() string {
current := time.Now()
return current.Format("2006-01-02 15:04:05")
}
func unixTimeStampNano() string {
timestamp := strconv.FormatInt(time.Now().UTC().UnixNano(), 10)
return timestamp
}
func writeTxToDisk(gtxType, gtxHash, gtxData, gtxPrev string) {
timeNano := unixTimeStampNano()
txFileName := timeNano + ".json"
createFile(txFileName)
txJSONItems := []string{gtxType, gtxHash, gtxData, gtxPrev}
txJSONObject, _ := json.Marshal(txJSONItems)
fmt.Printf(white+"\nWriting file...\nFileName: %s\nTransaction Body Object\n%s", txFileName, string(txJSONObject))
writeFile(txFileName, string(txJSONObject))
}
func createDirIfItDontExist(dir string) {
if _, err := os.Stat(dir); os.IsNotExist(err) {
err = os.MkdirAll(dir, 0755)
handle("Could not create directory: ", err)
}
}
// checkDirs Check if directory exists
func checkDirs() {
createDirIfItDontExist(configDir)
createDirIfItDontExist(p2pConfigDir)
createDirIfItDontExist(p2pWhitelistDir)
createDirIfItDontExist(p2pBlacklistDir)
createDirIfItDontExist(certPathDir)
createDirIfItDontExist(certPathSelfDir)
createDirIfItDontExist(certPathRemote)
}
// handle Ye Olde Error Handler takes a message and an error code
func handle(msg string, err error) {
if err != nil {
fmt.Printf(brightred+"\n%s: %s"+white, msg, err)
}
}
// createFile Generic file handler
func createFile(filename string) {
var _, err = os.Stat(filename)
if os.IsNotExist(err) {
var file, err = os.Create(filename)
handle("", err)
defer file.Close()
}
}
// writeFile Generic file handler
func writeFile(filename, textToWrite string) {
var file, err = os.OpenFile(filename, os.O_RDWR, 0644)
handle("", err)
defer file.Close()
_, err = file.WriteString(textToWrite)
err = file.Sync()
handle("", err)
}
// writeFileBytes Generic file handler
func writeFileBytes(filename string, bytesToWrite []byte) {
var file, err = os.OpenFile(filename, os.O_RDWR, 0644)
handle("", err)
defer file.Close()
_, err = file.Write(bytesToWrite)
err = file.Sync()
handle("", err)
}
// readFile Generic file handler
func readFile(filename string) string {
text, err := ioutil.ReadFile(filename)
handle("Couldnt read the file: ", err)
return string(text)
}
func readFileBytes(filename string) []byte {
text, err := ioutil.ReadFile(filename)
handle("Couldnt read the file: ", err)
return text
}
// deleteFile Generic file handler
func deleteFile(filename string) {
err := os.Remove(filename)
handle("Problem deleting file: ", err)
}
func validJSON(stringToValidate string) bool {
return json.Valid([]byte(stringToValidate))
}
func countWhitelistPeers() int {
directory := p2pWhitelistDir + "/"
dirRead, _ := os.Open(directory)
dirFiles, _ := dirRead.Readdir(0)
count := 0
for range dirFiles {
count++
}
return count
}
func cleanData() {
if wantsClean {
// cleanse the whitelist
directory := p2pWhitelistDir + "/"
dirRead, _ := os.Open(directory)
dirFiles, _ := dirRead.Readdir(0)
for index := range dirFiles {
fileHere := dirFiles[index]
nameHere := fileHere.Name()
fullPath := directory + nameHere
deleteFile(fullPath)
}
// cleanse the blacklist
blackList, _ := ioutil.ReadDir(p2pBlacklistDir + "/")
for _, f := range blackList {
fileToDelete := p2pBlacklistDir + "/" + f.Name()
fmt.Printf("\nDeleting file: %s", fileToDelete)
deleteFile(f.Name())
}
fmt.Printf(brightyellow+"\nPeers clear: %s"+white, brightgreen+"✔️")
// cleanse the remote certs
remoteCert, _ := ioutil.ReadDir(certPathRemote + "/")
for _, f := range remoteCert {
fileToDelete := certPathRemote + "/" + f.Name()
fmt.Printf("\nDeleting file: %s", fileToDelete)
deleteFile(fileToDelete)
}
fmt.Printf(brightyellow+"\nCerts clear: %s"+white, brightgreen+"✔️")
}
}