|
| 1 | +package util |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "mahonia" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "strconv" |
| 9 | + "strings" |
| 10 | + "time" |
| 11 | +) |
| 12 | + |
| 13 | +var CmdOutputDecoder mahonia.Decoder |
| 14 | +const GBK = "936" |
| 15 | +const UTF8 = "65001" |
| 16 | + |
| 17 | +func Exists(path string) bool { |
| 18 | + _, err := os.Stat(path) //os.Stat获取文件信息 |
| 19 | + if err != nil { |
| 20 | + if os.IsExist(err) { |
| 21 | + return true |
| 22 | + } |
| 23 | + return false |
| 24 | + } |
| 25 | + return true |
| 26 | +} |
| 27 | + |
| 28 | + |
| 29 | +func ParseDuration(s string) (t time.Duration, err error) { |
| 30 | + unit := s[len(s)-1:] |
| 31 | + var timeUnit int64 |
| 32 | + switch unit { |
| 33 | + case "s": |
| 34 | + timeUnit = int64(time.Second) |
| 35 | + case "m": |
| 36 | + timeUnit = int64(time.Minute) |
| 37 | + case "h": |
| 38 | + timeUnit = int64(time.Hour) |
| 39 | + case "d": |
| 40 | + timeUnit = int64(time.Hour * 24) |
| 41 | + default: |
| 42 | + err = errors.New("invalid time unit") |
| 43 | + return t, err |
| 44 | + } |
| 45 | + |
| 46 | + countStr := s[0 : len(s)-1] |
| 47 | + count, err := strconv.Atoi(countStr) |
| 48 | + if err != nil { |
| 49 | + return t, err |
| 50 | + } |
| 51 | + var temp int64 |
| 52 | + temp = int64(count) * timeUnit |
| 53 | + t = time.Duration(temp) |
| 54 | + return t, nil |
| 55 | +} |
| 56 | + |
| 57 | + |
| 58 | +func RunCommandWithRetry(count int, name string, args ...string) (output string, err error) { |
| 59 | + for i := 0; i < count; i++ { |
| 60 | + output, err = RunCommand(name, args...) |
| 61 | + if err == nil { |
| 62 | + return output, err |
| 63 | + } |
| 64 | + } |
| 65 | + return output, err |
| 66 | +} |
| 67 | + |
| 68 | + |
| 69 | +// Run os command and return output |
| 70 | +func RunCommand(name string, args ...string) (output string, err error) { |
| 71 | + if CmdOutputDecoder == nil { |
| 72 | + err = getCmdEncode() |
| 73 | + if err != nil { |
| 74 | + return "", nil |
| 75 | + } |
| 76 | + } |
| 77 | + cmd := exec.Command(name, args...) |
| 78 | + b, err := cmd.Output() |
| 79 | + output = CmdOutputDecoder.ConvertString(string(b)) |
| 80 | + if err != nil { |
| 81 | + return output, err |
| 82 | + } |
| 83 | + return output, nil |
| 84 | +} |
| 85 | + |
| 86 | +// get cmd encode format |
| 87 | +func getCmdEncode() error { |
| 88 | + cmd := exec.Command("chcp") |
| 89 | + b, err := cmd.Output() |
| 90 | + if err != nil { |
| 91 | + return err |
| 92 | + } |
| 93 | + if strings.Contains(string(b), GBK) { |
| 94 | + CmdOutputDecoder = mahonia.NewDecoder("gbk") |
| 95 | + } else if strings.Contains(string(b), UTF8) { |
| 96 | + CmdOutputDecoder = mahonia.NewDecoder("utf8") |
| 97 | + } else { |
| 98 | + CmdOutputDecoder = mahonia.NewDecoder("utf8") |
| 99 | + } |
| 100 | + return nil |
| 101 | +} |
| 102 | + |
| 103 | +//Value Description |
| 104 | +//0 No files were copied. No failure was encountered. No files were mismatched. The files already exist in the destination directory; therefore, the copy operation was skipped. |
| 105 | +//1 All files were copied successfully. |
| 106 | +//2 There are some additional files in the destination directory that are not present in the source directory. No files were copied. |
| 107 | +//3 Some files were copied. Additional files were present. No failure was encountered. |
| 108 | +//5 Some files were copied. Some files were mismatched. No failure was encountered. |
| 109 | +//6 Additional files and mismatched files exist. No files were copied and no failures were encountered. This means that the files already exist in the destination directory. |
| 110 | +//7 Files were copied, a file mismatch was present, and additional files were present. |
| 111 | +//8 Several files did not copy. |
| 112 | +func DealRobocopyResult(o string, e error) (output string, err error) { |
| 113 | + if e != nil && strings.EqualFold(e.Error(), "exit status 2") { |
| 114 | + return o, nil |
| 115 | + } |
| 116 | + return o, e |
| 117 | +} |
0 commit comments