forked from apache/mynewt-newt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
996 lines (820 loc) · 22.4 KB
/
util.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package util
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"os/signal"
"path/filepath"
"runtime"
"sort"
"strconv"
"strings"
"syscall"
"time"
log "github.com/sirupsen/logrus"
"github.com/otiai10/copy"
)
var Verbosity int
var PrintShellCmds bool
var ExecuteShell bool
var EscapeShellCmds bool
var logFile *os.File
func ParseEqualsPair(v string) (string, string, error) {
s := strings.Split(v, "=")
return s[0], s[1], nil
}
type NewtError struct {
Parent error
Text string
StackTrace []byte
}
const (
VERBOSITY_SILENT = 0
VERBOSITY_QUIET = 1
VERBOSITY_DEFAULT = 2
VERBOSITY_VERBOSE = 3
)
func (se *NewtError) Error() string {
return se.Text
}
func NewNewtError(msg string) *NewtError {
err := &NewtError{
Text: msg,
StackTrace: make([]byte, 65536),
}
stackLen := runtime.Stack(err.StackTrace, true)
err.StackTrace = err.StackTrace[:stackLen]
return err
}
func FmtNewtError(format string, args ...interface{}) *NewtError {
return NewNewtError(fmt.Sprintf(format, args...))
}
func PreNewtError(err error, format string, args ...interface{}) *NewtError {
baseErr := err.(*NewtError)
baseErr.Text = fmt.Sprintf(format, args...) + "; " + baseErr.Text
return baseErr
}
func ChildNewtError(parent error) *NewtError {
for {
newtErr, ok := parent.(*NewtError)
if !ok || newtErr == nil || newtErr.Parent == nil {
break
}
parent = newtErr.Parent
}
newtErr := NewNewtError(parent.Error())
newtErr.Parent = parent
return newtErr
}
func FmtChildNewtError(parent error, format string,
args ...interface{}) *NewtError {
ne := ChildNewtError(parent)
ne.Text = fmt.Sprintf(format, args...)
return ne
}
// Print Silent, Quiet and Verbose aware status messages to stdout.
func WriteMessage(f *os.File, level int, message string,
args ...interface{}) {
if Verbosity >= level {
str := fmt.Sprintf(message, args...)
f.WriteString(str)
f.Sync()
if logFile != nil {
logFile.WriteString(str)
}
}
}
// Print Silent, Quiet and Verbose aware status messages to stdout.
func StatusMessage(level int, message string, args ...interface{}) {
WriteMessage(os.Stdout, level, message, args...)
}
// Print Silent, Quiet and Verbose aware status messages to stderr.
func ErrorMessage(level int, message string, args ...interface{}) {
WriteMessage(os.Stderr, level, message, args...)
}
func NodeExist(path string) bool {
if _, err := os.Stat(path); err == nil {
return true
} else {
return false
}
}
// Check whether the node (either dir or file) specified by path exists
func NodeNotExist(path string) bool {
if _, err := os.Stat(path); os.IsNotExist(err) {
return true
} else {
return false
}
}
func FileModificationTime(path string) (time.Time, error) {
fileInfo, err := os.Stat(path)
if err != nil {
epoch := time.Unix(0, 0)
if os.IsNotExist(err) {
return epoch, nil
} else {
return epoch, NewNewtError(err.Error())
}
}
return fileInfo.ModTime(), nil
}
func ChildDirs(path string) ([]string, error) {
children, err := ioutil.ReadDir(path)
if err != nil {
return nil, NewNewtError(err.Error())
}
childDirs := []string{}
for _, child := range children {
name := child.Name()
if !filepath.HasPrefix(name, ".") &&
!filepath.HasPrefix(name, "..") &&
child.IsDir() {
childDirs = append(childDirs, name)
}
}
return childDirs, nil
}
func Min(x, y int) int {
if x < y {
return x
}
return y
}
func Max(x, y int) int {
if x > y {
return x
}
return y
}
type logFormatter struct{}
func (f *logFormatter) Format(entry *log.Entry) ([]byte, error) {
// 2016/03/16 12:50:47 [DEBUG]
b := &bytes.Buffer{}
b.WriteString(entry.Time.Format("2006/01/02 15:04:05.000 "))
b.WriteString("[" + strings.ToUpper(entry.Level.String()) + "] ")
b.WriteString(entry.Message)
b.WriteByte('\n')
return b.Bytes(), nil
}
func initLog(level log.Level, logFilename string) error {
log.SetLevel(level)
var writer io.Writer
if logFilename == "" {
writer = os.Stderr
} else {
var err error
logFile, err = os.Create(logFilename)
if err != nil {
return NewNewtError(err.Error())
}
writer = io.MultiWriter(os.Stderr, logFile)
}
log.SetOutput(writer)
log.SetFormatter(&logFormatter{})
return nil
}
// Initialize the util module
func Init(logLevel log.Level, logFile string, verbosity int) error {
// Configure logging twice. First just configure the filter for stderr;
// second configure the logfile if there is one. This needs to happen in
// two steps so that the log level is configured prior to the attempt to
// open the log file. The correct log level needs to be applied to file
// error messages.
if err := initLog(logLevel, ""); err != nil {
return err
}
if logFile != "" {
if err := initLog(logLevel, logFile); err != nil {
return err
}
}
Verbosity = verbosity
PrintShellCmds = false
ExecuteShell = false
return nil
}
// Escapes special characters for Windows builds (not in an MSYS environment).
func fixupCmdArgs(args []string) {
if EscapeShellCmds {
for i, _ := range args {
args[i] = strings.Replace(args[i], "{", "\\{", -1)
args[i] = strings.Replace(args[i], "}", "\\}", -1)
}
}
}
func LogShellCmd(cmdStrs []string, env map[string]string) {
envLogStr := ""
if len(env) > 0 {
s := EnvVarsToSlice(env)
envLogStr = strings.Join(s, " ") + " "
}
log.Debugf("%s%s", envLogStr, strings.Join(cmdStrs, " "))
if PrintShellCmds {
StatusMessage(VERBOSITY_DEFAULT, "%s\n", strings.Join(cmdStrs, " "))
}
}
// EnvVarsToSlice converts an environment variable map into a slice of `k=v`
// strings.
func EnvVarsToSlice(env map[string]string) []string {
keys := make([]string, 0, len(env))
for k, _ := range env {
keys = append(keys, k)
}
sort.Strings(keys)
slice := make([]string, 0, len(env))
for _, key := range keys {
slice = append(slice, fmt.Sprintf("%s=%s", key, env[key]))
}
return slice
}
// SliceToEnvVars converts a slice of `k=v` strings into an environment
// variable map.
func SliceToEnvVars(slc []string) (map[string]string, error) {
m := make(map[string]string, len(slc))
for _, s := range slc {
parts := strings.SplitN(s, "=", 2)
if len(parts) != 2 {
return nil, FmtNewtError("invalid env var string: \"%s\"", s)
}
m[parts[0]] = parts[1]
}
return m, nil
}
// EnvironAsMap gathers the current process's set of environment variables and
// returns them as a map.
func EnvironAsMap() (map[string]string, error) {
slc := os.Environ()
m, err := SliceToEnvVars(slc)
if err != nil {
return nil, err
}
return m, nil
}
// Execute the specified process and block until it completes. Additionally,
// the amount of combined stdout+stderr output to be logged to the debug log
// can be restricted to a maximum number of characters.
//
// @param cmdStrs The "argv" strings of the command to execute.
// @param env Additional key,value pairs to inject into the
// child process's environment. Specify null
// to just inherit the parent environment.
// @param logCmd Whether to log the command being executed.
// @param maxDbgOutputChrs The maximum number of combined stdout+stderr
// characters to write to the debug log.
// Specify -1 for no limit; 0 for no output.
//
// @return []byte Combined stdout and stderr output of process.
// @return error NewtError on failure. Use IsExit() to
// determine if the command failed to execute
// or if it just returned a non-zero exit
// status.
func ShellCommandLimitDbgOutput(
cmdStrs []string, env map[string]string, logCmd bool,
maxDbgOutputChrs int) ([]byte, error) {
var name string
var args []string
// Escape special characters for Windows.
fixupCmdArgs(cmdStrs)
if logCmd {
LogShellCmd(cmdStrs, env)
}
if ExecuteShell && (runtime.GOOS == "linux" || runtime.GOOS == "darwin") {
cmd := strings.Join(cmdStrs, " ")
name = "/bin/sh"
cmd = strings.Replace(cmd, "\"", "\\\"", -1)
cmd = strings.Replace(cmd, "<", "\\<", -1)
cmd = strings.Replace(cmd, ">", "\\>", -1)
args = []string{"-c", cmd}
} else {
if strings.HasSuffix(cmdStrs[0], ".sh") {
var newt_sh = os.Getenv("NEWT_SH")
if newt_sh == "" {
newt_sh = "bash"
}
name = newt_sh
args = cmdStrs
} else {
name = cmdStrs[0]
args = cmdStrs[1:]
}
}
cmd := exec.Command(name, args...)
if env != nil {
m, err := EnvironAsMap()
if err != nil {
return nil, err
}
for k, v := range env {
m[k] = v
}
cmd.Env = EnvVarsToSlice(m)
}
o, err := cmd.CombinedOutput()
if maxDbgOutputChrs < 0 || len(o) <= maxDbgOutputChrs {
dbgStr := string(o)
log.Debugf("o=%s", dbgStr)
} else if maxDbgOutputChrs != 0 {
dbgStr := string(o[:maxDbgOutputChrs]) + "[...]"
log.Debugf("o=%s", dbgStr)
}
if err != nil {
err = ChildNewtError(err)
log.Debugf("err=%s", err.Error())
if len(o) > 0 {
err.(*NewtError).Text = string(o)
}
return o, err
} else {
return o, nil
}
}
// Execute the specified process and block until it completes.
//
// @param cmdStrs The "argv" strings of the command to execute.
// @param env Additional key,value pairs to inject into the
// child process's environment. Specify null
// to just inherit the parent environment.
//
// @return []byte Combined stdout and stderr output of process.
// @return error NewtError on failure.
func ShellCommand(cmdStrs []string, env map[string]string) ([]byte, error) {
return ShellCommandLimitDbgOutput(cmdStrs, env, true, -1)
}
// Run interactive shell command
func ShellInteractiveCommand(cmdStr []string, env map[string]string,
flagBlock bool) error {
// Escape special characters for Windows.
fixupCmdArgs(cmdStr)
var newt_sh string
if runtime.GOOS == "windows" && strings.HasSuffix(cmdStr[0], ".sh") {
newt_sh = os.Getenv("NEWT_SH")
if newt_sh == "" {
bash, err := exec.LookPath("bash")
if err != nil {
return err
}
newt_sh = bash
}
cmdStr = append([]string{newt_sh}, cmdStr...)
}
log.Print("[VERBOSE] " + cmdStr[0])
c := make(chan os.Signal, 1)
// Block SIGINT, at least.
// Otherwise Ctrl-C meant for gdb would kill newt.
if flagBlock == false {
signal.Notify(c, os.Interrupt)
signal.Notify(c, syscall.SIGTERM)
go func() {
<-c
}()
}
m, err := EnvironAsMap()
if err != nil {
return err
}
for k, v := range env {
m[k] = v
}
envSlice := EnvVarsToSlice(m)
// Transfer stdin, stdout, and stderr to the new process
// and also set target directory for the shell to start in.
// and set the additional environment variables
pa := os.ProcAttr{
Env: envSlice,
Files: []*os.File{os.Stdin, os.Stdout, os.Stderr},
}
// Start up a new shell.
proc, err := os.StartProcess(cmdStr[0], cmdStr, &pa)
if err != nil {
signal.Stop(c)
return NewNewtError(err.Error())
}
// Release and exit
state, err := proc.Wait()
signal.Stop(c)
if err != nil {
return NewNewtError(err.Error())
}
if state.ExitCode() != 0 {
return FmtNewtError(
"command %v exited with nonzero status %d",
cmdStr, state.ExitCode())
}
return nil
}
func CopyFile(srcFile string, dstFile string) error {
in, err := os.Open(srcFile)
if err != nil {
return ChildNewtError(err)
}
defer in.Close()
info, err := in.Stat()
if err != nil {
return ChildNewtError(err)
}
dstDir := filepath.Dir(dstFile)
if err := os.MkdirAll(dstDir, os.ModePerm); err != nil {
return ChildNewtError(err)
}
out, err := os.OpenFile(dstFile, os.O_CREATE|os.O_WRONLY|os.O_TRUNC,
info.Mode())
if err != nil {
return ChildNewtError(err)
}
defer out.Close()
if _, err = io.Copy(out, in); err != nil {
return ChildNewtError(err)
}
return nil
}
func CopyDir(srcDirStr, dstDirStr string) error {
opt := copy.Options{
OnSymlink: func(src string) copy.SymlinkAction {
return copy.Shallow
},
}
err := copy.Copy(srcDirStr, dstDirStr, opt)
if err != nil {
return ChildNewtError(err)
}
return nil
}
func MoveFile(srcFile string, destFile string) error {
// First, attempt a rename. This will succeed if the source and
// destination are on the same disk.
if err := os.Rename(srcFile, destFile); err == nil {
return nil
}
// Otherwise, copy the file and delete the old path.
if err := CopyFile(srcFile, destFile); err != nil {
return err
}
if err := os.RemoveAll(srcFile); err != nil {
return ChildNewtError(err)
}
return nil
}
func MoveDir(srcDir string, destDir string) error {
// First, attempt a rename. This will succeed if the source and
// destination are on the same disk.
if err := os.Rename(srcDir, destDir); err == nil {
return nil
}
// Otherwise, copy the directory and delete the old path.
if err := CopyDir(srcDir, destDir); err != nil {
return err
}
if err := os.RemoveAll(srcDir); err != nil {
return ChildNewtError(err)
}
return nil
}
func CallInDir(path string, execFunc func() error) error {
wd, err := os.Getwd()
if err != nil {
return err
}
os.Chdir(path)
err = execFunc()
os.Chdir(wd)
return err
}
// Reads each line from the specified text file into an array of strings. If a
// line ends with a backslash, it is concatenated with the following line.
func ReadLines(path string) ([]string, error) {
file, err := os.Open(path)
if err != nil {
return nil, NewNewtError(err.Error())
}
defer file.Close()
lines := []string{}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
concatted := false
if len(lines) != 0 {
prevLine := lines[len(lines)-1]
if len(prevLine) > 0 && prevLine[len(prevLine)-1:] == "\\" {
prevLine = prevLine[:len(prevLine)-1]
prevLine += line
lines[len(lines)-1] = prevLine
concatted = true
}
}
if !concatted {
lines = append(lines, line)
}
}
if scanner.Err() != nil {
return lines, NewNewtError(scanner.Err().Error())
}
return lines, nil
}
// Removes all duplicate strings from the specified array, while preserving
// order.
func UniqueStrings(elems []string) []string {
set := make(map[string]bool)
result := make([]string, 0)
for _, elem := range elems {
if !set[elem] {
result = append(result, elem)
set[elem] = true
}
}
return result
}
// Sorts whitespace-delimited lists of strings.
//
// @param wsSepStrings A list of strings; each string contains one or
// more whitespace-delimited tokens.
//
// @return A slice containing all the input tokens, sorted
// alphabetically.
func SortFields(wsSepStrings ...string) []string {
slice := []string{}
for _, s := range wsSepStrings {
slice = append(slice, strings.Fields(s)...)
}
slice = UniqueStrings(slice)
sort.Strings(slice)
return slice
}
// Converts the specified string to an integer. The string can be in base-10
// or base-16. This is equivalent to the "0" base used in the standard
// conversion functions, except octal is not supported (a leading zero implies
// decimal).
//
// The second return value is true on success.
func AtoiNoOctTry(s string) (int, bool) {
var runLen int
for runLen = 0; runLen < len(s)-1; runLen++ {
if s[runLen] != '0' || s[runLen+1] == 'x' {
break
}
}
if runLen > 0 {
s = s[runLen:]
}
i, err := strconv.ParseInt(s, 0, 0)
if err != nil {
return 0, false
}
return int(i), true
}
// Converts the specified string to an integer. The string can be in base-10
// or base-16. This is equivalent to the "0" base used in the standard
// conversion functions, except octal is not supported (a leading zero implies
// decimal).
func AtoiNoOct(s string) (int, error) {
val, success := AtoiNoOctTry(s)
if !success {
return 0, FmtNewtError("Invalid number: \"%s\"", s)
}
return val, nil
}
func IsNotExist(err error) bool {
newtErr, ok := err.(*NewtError)
if ok {
err = newtErr.Parent
}
return os.IsNotExist(err)
}
// Indicates whether the provided error is of type *exec.ExitError (raised when
// a child process exits with a non-zero status code).
func IsExit(err error) bool {
newtErr, ok := err.(*NewtError)
if ok {
err = newtErr.Parent
}
_, ok = err.(*exec.ExitError)
return ok
}
func FileContentsChanged(path string, newContents []byte) (bool, error) {
oldContents, err := ioutil.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
// File doesn't exist; write required.
return true, nil
}
return true, NewNewtError(err.Error())
}
rc := bytes.Compare(oldContents, newContents)
return rc != 0, nil
}
func CIdentifier(s string) string {
s = strings.Replace(s, "/", "_", -1)
s = strings.Replace(s, "-", "_", -1)
s = strings.Replace(s, " ", "_", -1)
return s
}
func FilenameFromPath(s string) string {
s = strings.Replace(s, "/", "_", -1)
s = strings.Replace(s, " ", "_", -1)
s = strings.Replace(s, "\t", "_", -1)
s = strings.Replace(s, "\n", "_", -1)
return s
}
func IntMax(a, b int) int {
if a > b {
return a
} else {
return b
}
}
func IntMin(a, b int) int {
if a < b {
return a
} else {
return b
}
}
func PrintStacks() {
buf := make([]byte, 1024*1024)
stacklen := runtime.Stack(buf, true)
fmt.Printf("*** goroutine dump\n%s\n*** end\n", buf[:stacklen])
}
// Attempts to convert the specified absolute path into a relative path
// (relative to the current working directory). If the path cannot be
// converted, it is returned unchanged.
func TryRelPath(full string) string {
pwd, err := os.Getwd()
if err != nil {
return full
}
rel, err := filepath.Rel(pwd, full)
if err != nil {
return full
}
return rel
}
// StringMapStringToItfMapItf converts a map[string]string to the more generic
// map[interface{}]interface{} type.
func StringMapStringToItfMapItf(
sms map[string]string) map[interface{}]interface{} {
imi := map[interface{}]interface{}{}
for k, v := range sms {
imi[k] = v
}
return imi
}
// FileContains indicates whether the specified file's contents are equal to
// the provided byte slice.
func FileContains(contents []byte, path string) (bool, error) {
oldSrc, err := ioutil.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
// File doesn't exist; contents aren't equal.
return false, nil
}
return false, NewNewtError(err.Error())
}
rc := bytes.Compare(oldSrc, contents)
return rc == 0, nil
}
// Keeps track of warnings that have already been reported.
// [warning-text] => struct{}
var warnings = map[string]struct{}{}
// Displays the specified warning if it has not been displayed yet.
func OneTimeWarning(text string, args ...interface{}) {
body := fmt.Sprintf(text, args...)
if _, ok := warnings[body]; !ok {
warnings[body] = struct{}{}
body := fmt.Sprintf(text, args...)
ErrorMessage(VERBOSITY_QUIET, "WARNING: %s\n", body)
}
}
// OneTimeWarningError displays the text of the specified error as a warning if
// it has not been displayed yet. No-op if nil is passed in.
func OneTimeWarningError(err error) {
if err != nil {
OneTimeWarning("%s", err.Error())
}
}
func MarshalJSONStringer(sr fmt.Stringer) ([]byte, error) {
s := sr.String()
j, err := json.Marshal(s)
if err != nil {
return nil, ChildNewtError(err)
}
return j, nil
}
// readDirRecursive recursively reads the contents of a directory. It returns
// [dir-paths],[file-paths]. All returned strings are relative to the provided
// base directory.
func readDirRecursive(path string) ([]string, []string, error) {
var dirs []string
var files []string
var iter func(crumbs string) error
iter = func(crumbs string) error {
var crumbsPath string
if crumbs != "" {
crumbsPath = "/" + crumbs
}
f, err := os.Open(path + crumbsPath)
if err != nil {
return ChildNewtError(err)
}
defer f.Close()
infos, err := f.Readdir(-1)
if err != nil {
return ChildNewtError(err)
}
for _, info := range infos {
name := fmt.Sprintf("%s/%s", crumbs, info.Name())
if info.IsDir() {
dirs = append(dirs, name)
if err := iter(name); err != nil {
return err
}
} else {
files = append(files, name)
}
}
return nil
}
if err := iter(""); err != nil {
return nil, nil, err
}
return dirs, files, nil
}
// DirsAreEqual compares the contents of two directories. Directories are
// equal if 1) their subdirectory structures are identical, and 2) they contain
// the exact same set of files (same names and contents).
func DirsAreEqual(dira string, dirb string) (bool, error) {
dirsa, filesa, err := readDirRecursive(dira)
if err != nil {
return false, err
}
dirsb, filesb, err := readDirRecursive(dirb)
if err != nil {
return false, err
}
if len(dirsa) != len(dirsb) || len(filesa) != len(filesb) {
return false, nil
}
// Returns the intersection of two sets of strings.
intersection := func(a []string, b []string) map[string]struct{} {
ma := make(map[string]struct{}, len(a))
for _, p := range a {
ma[p] = struct{}{}
}
isect := map[string]struct{}{}
for _, p := range b {
if _, ok := ma[p]; ok {
isect[p] = struct{}{}
}
}
return isect
}
// If the intersection lengths are equal, both directories have the same
// structure.
isectDirs := intersection(dirsa, dirsb)
if len(isectDirs) != len(dirsa) {
return false, nil
}
isectFiles := intersection(filesa, filesb)
if len(isectFiles) != len(filesa) {
return false, nil
}
// Finally, compare the contents of files in each directory.
for _, p := range filesa {
patha := fmt.Sprintf("%s/%s", dira, p)
bytesa, err := ioutil.ReadFile(patha)
if err != nil {
return false, ChildNewtError(err)
}
pathb := fmt.Sprintf("%s/%s", dirb, p)
unchanged, err := FileContains(bytesa, pathb)
if err != nil {
return false, err
}
if !unchanged {
return false, nil
}
}
return true, nil
}