/
common.go
executable file
·402 lines (349 loc) · 8.52 KB
/
common.go
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 core
import (
"crypto/md5"
"crypto/rand"
"encoding/hex"
"fmt"
"github.com/cmcoffee/go-snuglib/cfg"
"github.com/cmcoffee/go-snuglib/eflag"
"github.com/cmcoffee/go-snuglib/nfo"
"github.com/cmcoffee/go-snuglib/xsync"
"io"
"os"
"path/filepath"
"strings"
"sync/atomic"
"time"
)
// Menu item flags
type FlagSet struct {
FlagArgs []string
*eflag.EFlagSet
}
// Required for each task object.
type KiteBrokerTask struct {
Flags FlagSet
DB SubStore
Report *TaskReport
KW KWSession
}
// Sets the flags for the task.
func (T *KiteBrokerTask) KiteBrokerTask_set_flags(Flags FlagSet) {
T.Flags = Flags
}
// Initializes the task's database
func (T *KiteBrokerTask) KiteBrokerTask_set_db(DB SubStore) {
T.DB = DB
}
// Sets a report for the task.
func (T *KiteBrokerTask) KiteBrokerTask_set_report(input *TaskReport) {
T.Report = input
}
// Sets the kw api session for the task
func (T *KiteBrokerTask) KiteBrokerTask_set_session(user KWSession) {
T.KW = user
}
// Provides summary of the task after completion.
func (T *KiteBrokerTask) KiteBrokerTask_report_summary(errors uint32) {
if T.Report == nil {
return
}
T.Report.Summary(errors)
}
// Parse flags assocaited with task.
func (f *FlagSet) Parse() (err error) {
if err = f.EFlagSet.Parse(f.FlagArgs[0:]); err != nil {
return err
}
return nil
}
// Task Interface
type Task interface {
New() Task
Name() string
Desc() string
Init() error
Main() error
KiteBrokerTask_set_flags(Flags FlagSet)
KiteBrokerTask_set_db(DB SubStore)
KiteBrokerTask_set_report(*TaskReport)
KiteBrokerTask_set_session(user KWSession)
KiteBrokerTask_report_summary(error uint32)
}
type TaskArgs map[string]interface{}
// Allows a KitebrokerTask to launch another KiteBrokerTask.
func (T KWSession) RunTask(input Task, db Database, report *TaskReport, args...map[string]interface{}) (err error) {
var arg_string []string
for _, arg := range args {
for k, v := range arg {
var dash string
if len(k) == 1 {
dash = "-"
} else {
dash = "--"
}
switch x := v.(type) {
case string:
if k == "args" {
arg_string = append(arg_string, x)
} else {
arg_string = append(arg_string, fmt.Sprintf("%s%s=%s", dash, k, x))
}
case []string:
for _, line := range x {
arg_string = append(arg_string, fmt.Sprintf("%s%s=%s", dash, k, line))
}
default:
arg_string = append(arg_string, fmt.Sprintf("%s%s=%v", dash, k, v))
}
}
}
input.KiteBrokerTask_set_db(db.(SubStore))
flags := FlagSet{EFlagSet: eflag.NewFlagSet("SubTask", eflag.ReturnErrorOnly)}
flags.FlagArgs = arg_string
input.KiteBrokerTask_set_flags(flags)
if err = input.Init(); err != nil {
return err
}
input.KiteBrokerTask_set_session(T)
input.KiteBrokerTask_set_report(report)
if err = input.Main(); err != nil {
return err
}
return nil
}
const (
NONE = ""
SLASH = string(os.PathSeparator)
)
// Import from go-nfo.
var (
Log = nfo.Log
Fatal = nfo.Fatal
Notice = nfo.Notice
Flash = nfo.Flash
Stdout = nfo.Stdout
Warn = nfo.Warn
Defer = nfo.Defer
Debug = nfo.Debug
Exit = nfo.Exit
PleaseWait = nfo.PleaseWait
Stderr = nfo.Stderr
ProgressBar = nfo.ProgressBar
Path = filepath.Clean
TransferCounter = nfo.TransferCounter
NewLimitGroup = xsync.NewLimitGroup
FormatPath = filepath.FromSlash
GetPath = filepath.ToSlash
Info = nfo.Aux
)
var (
transferMonitor = nfo.TransferMonitor
leftToRight = nfo.LeftToRight
rightToLeft = nfo.RightToLeft
noRate = nfo.NoRate
)
type (
ReadSeekCloser = nfo.ReadSeekCloser
BitFlag = xsync.BitFlag
LimitGroup = xsync.LimitGroup
ConfigStore = cfg.Store
)
var error_counter uint32
// Returns amount of times Err has been triggered.
func ErrCount() uint32 {
return atomic.LoadUint32(&error_counter)
}
func Err(input ...interface{}) {
atomic.AddUint32(&error_counter, 1)
nfo.Err(input...)
}
// Converts string to date.
func StringDate(input string) (output time.Time, err error) {
if input == NONE {
return
}
output, err = time.Parse(time.RFC3339, fmt.Sprintf("%sT00:00:00Z", input))
if err != nil {
if strings.Contains(err.Error(), "parse") {
err = fmt.Errorf("Invalid date specified, should be in format: YYYY-MM-DD")
} else {
err_split := strings.Split(err.Error(), ":")
err = fmt.Errorf("Invalid date specified:%s", err_split[len(err_split)-1])
}
}
return
}
// Fatal Error Check
func Critical(err error) {
if err != nil {
Fatal(err)
}
}
// Splits path up
func SplitPath(path string) (folder_path []string) {
if strings.Contains(path, "/") {
path = strings.TrimSuffix(path, "/")
folder_path = strings.Split(path, "/")
} else {
path = strings.TrimSuffix(path, "\\")
folder_path = strings.Split(path, "\\")
}
if len(folder_path) == 0 {
folder_path = append(folder_path, path)
}
for i := 0; i < len(folder_path); i++ {
if folder_path[i] == NONE {
folder_path = append(folder_path[:i], folder_path[i+1:]...)
}
}
return
}
// Returns please wait prompt back to default setting.
func DefaultPleaseWait() {
PleaseWait.Set(func() string { return "Please wait ..." }, []string{"[> ]", "[>> ]", "[>>>]", "[ >>]", "[ >]", "[ <]", "[ <<]", "[<<<]", "[<< ]", "[< ]"})
}
// MD5Sum function for checking files against appliance.
func MD5Sum(filename string) (sum string, err error) {
checkSum := md5.New()
file, err := os.Open(filename)
if err != nil {
return
}
var (
o int64
n int
r int
)
for tmp := make([]byte, 16384); ; {
r, err = file.ReadAt(tmp, o)
if err != nil && err != io.EOF {
return NONE, err
}
if r == 0 {
break
}
tmp = tmp[0:r]
n, err = checkSum.Write(tmp)
if err != nil {
return NONE, err
}
o = o + int64(n)
}
if err != nil && err != io.EOF {
return NONE, err
}
md5sum := checkSum.Sum(nil)
s := make([]byte, hex.EncodedLen(len(md5sum)))
hex.Encode(s, md5sum)
return string(s), nil
}
// Generates a random byte slice of length specified.
func RandBytes(sz int) []byte {
if sz <= 0 {
sz = 16
}
ch := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/"
chlen := len(ch)
rand_string := make([]byte, sz)
rand.Read(rand_string)
for i, v := range rand_string {
rand_string[i] = ch[v%byte(chlen)]
}
return rand_string
}
// Error handler for const errors.
type Error string
func (e Error) Error() string { return string(e) }
// Creates folders.
func MkDir(name ...string) (err error) {
for _, path := range name {
subs := strings.Split(path, string(os.PathSeparator))
for i := 0; i < len(subs); i++ {
p := strings.Join(subs[0:i+1], string(os.PathSeparator))
if p == "" {
p = "."
}
f, err := os.Stat(p)
if err != nil {
if os.IsNotExist(err) {
err = os.Mkdir(p, 0766)
if err != nil {
return err
}
} else {
return err
}
}
if f != nil && !f.IsDir() {
return fmt.Errorf("mkdir: %s: file exists", f.Name())
}
}
}
return nil
}
// Parse Timestamps from kiteworks
func ReadKWTime(input string) (time.Time, error) {
input = strings.Replace(input, "+0000", "Z", 1)
return time.Parse(time.RFC3339, input)
}
// Write timestamps for kiteworks.
func WriteKWTime(input time.Time) string {
t := input.UTC().Format(time.RFC3339)
return strings.Replace(t, "Z", "+0000", 1)
}
// Create standard date YY-MM-DD out of time.Time.
func DateString(input time.Time) string {
pad := func(num int) string {
if num < 10 {
return fmt.Sprintf("0%d", num)
}
return fmt.Sprintf("%d", num)
}
//due_time := input.AddDate(0, 0, -1)
return fmt.Sprintf("%s-%s-%s", pad(input.Year()), pad(int(input.Month())), pad(input.Day()))
}
func CombinePath(name ...string) string {
if name == nil {
return NONE
}
if len(name) < 2 {
return name[0]
}
return fmt.Sprintf("%s%s%s", name[0], SLASH, strings.Join(name[1:], SLASH))
}
// Provides human readable file sizes.
func HumanSize(bytes int64) string {
names := []string{
"Bytes",
"KB",
"MB",
"GB",
}
suffix := 0
size := float64(bytes)
for size >= 1000 && suffix < len(names)-1 {
size = size / 1000
suffix++
}
return fmt.Sprintf("%.1f%s", size, names[suffix])
}
func Rename(oldpath, newpath string) error {
return os.Rename(oldpath, newpath)
}
func IsBlank(input ...string) bool {
for _, v := range input {
if len(v) == 0 {
return true
}
}
return false
}
func Dequote(input *string) {
if len(*input) > 0 && (*input)[0] == '"' {
*input = (*input)[:1]
}
if len(*input) > 0 && (*input)[len(*input)-1] == '"' {
*input = (*input)[:len(*input)-1]
}
}