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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
|
package main
import (
"bufio"
"fmt"
"math"
"os"
"io"
"os/exec"
"runtime"
"strconv"
"strings"
"unicode/utf8"
"math/rand"
"time"
"path/filepath"
"archive/tar"
)
const (
framewidth int = 50
li string = "\u2500"
mli string = "\u2500\u2530\u2500\u2500" //Masterline -T-
ssli string = " \u2520\u2500\u2500" //Slave Line L__
sli string = " \u2520\u2500 " //Slave Line L_
nli string = " \u2503 " //noone Line |
fli string = " \u2516\u2500\u2500" //Footer Line L
)
var positive = [...]string {"Done","Accomplished","Finished","Congrats","Completed"}
var negative = [...]string {"Failed","Try Again","Sorry","Not Quite Yet","Incomplete"}
func posR() (out string){
s1 := rand.NewSource(time.Now().UnixNano())
r1 := rand.New(s1)
n := r1.Intn(len(positive))
out = positive[n]
return
}
func negR() (out string){
s1 := rand.NewSource(time.Now().UnixNano())
r1 := rand.New(s1)
n := r1.Intn(len(negative))
out = negative[n]
return
}
func line(aslongas string, hidden bool) (out string) {
n := utf8.RuneCountInString(aslongas)
if hidden {
out = strings.Repeat(" ",n)
}else{
out = strings.Repeat(li,n)
}
return
}
func sub(text string) (out string) {
out = ssli +text
n := utf8.RuneCountInString(out)
r := framewidth - n
if r < 1 {
out = out + li
}else{
out = out + strings.Repeat(li,r)
}
return
}
func frame(text string, head bool) (out string) {
if head {
out = mli + text
}else{
out = fli + text
}
n := utf8.RuneCountInString(out)
r := framewidth - n
if r < 1 {
out = out + li
}else{
out = out + strings.Repeat(li,r)
}
if head {
out = "\n" + out
//}else{
// out = out + "\n"
}
return
}
// return the int as a string
func Sint(in int) (string) {
return fmt.Sprint(in)
}
// return the float as a string
func Sflo(in float64) (string) {
return fmt.Sprintf("%.2f",in)
}
func getGitTag() string {
var (
cmdOut []byte
err error
)
cmd := "git"
args := []string{"tag", "--points-at", "HEAD"}
if cmdOut, err = exec.Command(cmd, args...).Output(); err != nil {
panic(err)
}
return string(cmdOut)
}
//Remove a string from a []string and the int number of strings afterwards
func removeStringFromArray(arr []string, rem string, after int) (out []string) {
found := false
i := 0
for _, st := range arr {
if found {
if i < after {
i++
} else {
found = false
}
} else {
if st == rem {
found = true
} else {
out = append(out, st)
}
}
}
return
}
//Ask a Question an read the answer
func isSure(quest string) bool {
fmt.Printf("%s (type 'y/Y/yes' to confirm) : ", quest)
in := bufio.NewReader(os.Stdin)
line, err := in.ReadString('\n')
if runtime.GOOS == "windows" {
line = strings.TrimSuffix(line, "\r\n") //for Windows
} else {
line = strings.TrimSuffix(line, "\n")
}
checkErr(err)
if line == "yes" || line == "y" || line == "Y" {
return true
} else {
return false
}
}
//Test if error is nil and panic if not
//TODO log seperately
func checkErr(err error) {
if err != nil {
panic(err)
}
}
//make One String with Linebreaks out of an input String Array
func StrLines(in []string,border string) (out string) {
i := 0
for _, s := range in {
neu := ""
if i>0 {
neu = "\n"
}
neu = neu + fmt.Sprintf("%s%s", border, s)
out = out + neu
i++
}
return
}
//Ask a question return the Answer
func getInput(quest string) string {
fmt.Print(quest)
in := bufio.NewReader(os.Stdin)
line, err := in.ReadString('\n')
if runtime.GOOS == "windows" {
line = strings.TrimSuffix(line, "\r\n") //for Windows
} else {
line = strings.TrimSuffix(line, "\n")
}
checkErr(err)
return line
}
//show Old Value, Ask Question and Return answer
func getNewInput(quest, old string) string {
if old != "" {
fmt.Println("Current:", old)
}
fmt.Print(quest)
in := bufio.NewReader(os.Stdin)
line, err := in.ReadString('\n')
if runtime.GOOS == "windows" {
line = strings.TrimSuffix(line, "\r\n") //for Windows
} else {
line = strings.TrimSuffix(line, "\n")
}
checkErr(err)
if line == "" {
return old
} else {
return line
}
}
func stringArray2String(in []string, delim string) string {
var out string
for i, a := range in {
if i == 0 {
out = a
} else {
out = fmt.Sprintf("%s%s%s", out, delim, a)
}
}
return out
}
func IntArray2String(in []int, delim string) (out string) {
for i,a := range in {
if i == 0 {
out = fmt.Sprintf("%v",a)
} else {
out = fmt.Sprintf("%s%s%v", out, delim, a)
}
}
return
}
func String2IntArray(in, delim string) (out []int) {
read := strings.Split(in, delim)
for _, s := range read {
is, err := strconv.Atoi(s)
checkErr(err)
out = append(out, is)
}
return
}
func string2FloatArray(in string, delim string) (out []float64) {
read := strings.Split(in, delim)
for _, s := range read {
fs, err := strconv.ParseFloat(s, 64)
checkErr(err)
out = append(out, fs)
}
return
}
// Takes a String and Cuts it into []string elements by delim
func String2StringArray(in string, delim string) (out []string) {
read := strings.Split(in, delim)
for _, s := range read {
out = append(out, s)
}
return
}
func RetardRound(x float64) float64 {
t := math.Trunc(x)
if math.Abs(x-t) >= 0.5 {
return t + math.Copysign(1, x)
}
return t
}
func Round(x, unit float64) float64 {
// return math.Round(x/unit) * unit
return RetardRound(x/unit) * unit
}
// TEst if two time objects happen on same date
func DateEqual(date1, date2 time.Time) bool {
y1, m1, d1 := date1.Date()
y2, m2, d2 := date2.Date()
return y1 == y2 && m1 == m2 && d1 == d2
}
func cleanString(in string) (out string) {
work := strings.Replace(in, "/", "-", -1)
work = strings.Replace(work, "*", "-", -1)
work = strings.Replace(work, ":", "-", -1)
work = strings.Replace(work, "<", "-", -1)
work = strings.Replace(work, ">", "-", -1)
work = strings.Replace(work, "?", "-", -1)
work = strings.Replace(work, "|", "-", -1)
work = strings.Replace(work, "+", "-", -1)
work = strings.Replace(work, ",", "-", -1)
work = strings.Replace(work, ";", "-", -1)
work = strings.Replace(work, "=", "-", -1)
work = strings.Replace(work, "[", "-", -1)
out = strings.Replace(work, "]", "-", -1)
return
}
func sumFloatArray(in []float64) (sum float64) {
for _, e := range in {
sum += e
}
return
}
func isElement(some int, group []int) bool {
for _, e := range group {
if e == some {
return true
}
}
return false
}
func AddNewElement(base, news []int) (out []int) {
out = base
for _,e := range news {
if !isElement(e,base) {
out = append(out,e)
}
}
return
}
func removeItems(master, rem []int) []int {
var out []int
for _, v := range master {
if !isElement(v, rem) {
out = append(out, v)
}
}
return out
}
func IsStrElement(some string, group []string) bool {
for _, e := range group {
if e == some {
return true
}
}
return false
}
func Tar(src string, writers ...io.Writer) error {
// ensure the src actually exists before trying to tar it
if _, err := os.Stat(src); err != nil {
return fmt.Errorf("Unable to tar files - %v", err.Error())
}
mw := io.MultiWriter(writers...)
//gzw := gzip.NewWriter(mw)
//defer gzw.Close()
tw := tar.NewWriter(mw)
defer tw.Close()
// walk path
return filepath.Walk(src, func(file string, fi os.FileInfo, err error) error {
// return on any error
if err != nil {
return err
}
// create a new dir/file header
header, err := tar.FileInfoHeader(fi, fi.Name())
if err != nil {
return err
}
// update the name to correctly reflect the desired destination when untaring
header.Name = strings.TrimPrefix(strings.Replace(file, src, "", -1), string(filepath.Separator))
// write the header
if err := tw.WriteHeader(header); err != nil {
return err
}
// return on non-regular files (thanks to [kumo](https://medium.com/@komuw/just-like-you-did-fbdd7df829d3) for this suggested update)
if !fi.Mode().IsRegular() {
return nil
}
// open files for taring
f, err := os.Open(file)
if err != nil {
return err
}
// copy file data into tar writer
if _, err := io.Copy(tw, f); err != nil {
return err
}
// manually close here after each file operation; defering would cause each file close
// to wait until all operations have completed.
f.Close()
return nil
})
}
|