-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
128 lines (107 loc) · 2.97 KB
/
utils.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
package main
import (
"bufio"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"
)
func getNullDevice() string {
if runtime.GOOS == "windows" {
return "NUL"
}
return "/dev/null"
}
func parseTimeStringToSeconds(timeStr string) (float64, error) {
parts := strings.Split(timeStr, ":")
var durationStr string
switch len(parts) {
case 1:
durationStr = parts[0] + "s"
case 2:
durationStr = parts[0] + "m" + parts[1] + "s"
case 3:
durationStr = parts[0] + "h" + parts[1] + "m" + parts[2] + "s"
default:
return 0, fmt.Errorf("invalid time format")
}
duration, err := time.ParseDuration(durationStr)
if err != nil {
return 0, err
}
return duration.Seconds(), nil
}
func getKeyStringValue(input string, sep string) (string, string) {
arr := strings.SplitN(string(input), sep, 2)
if len(arr) == 2 {
return strings.TrimSpace(arr[0]), strings.TrimSpace(arr[1])
}
return strings.TrimSpace(arr[0]), ""
}
func getKeyIntValue(input string, sep string) (string, int, error) {
arr := strings.SplitN(string(input), sep, 2)
key := arr[0]
value, err := strconv.ParseInt(arr[1], 10, 0)
return key, int(value), err
}
func getKeyValuesFromCommand(cmd *exec.Cmd, sep string) (map[string]string, error) {
fmt.Printf("\n%+v\n\n", cmd)
stdout, err := cmd.StdoutPipe()
if err != nil {
return nil, fmt.Errorf("cmd.StdoutPipe() failed with %s", err)
}
if err := cmd.Start(); err != nil {
return nil, fmt.Errorf("cmd.Start() failed with %s", err)
}
defer cmd.Wait() // Ensures that the command finishes and resources are cleaned up
scanner := bufio.NewScanner(stdout)
keyValues := make(map[string]string)
for scanner.Scan() {
text := strings.TrimSpace(scanner.Text())
if text == "" {
continue
}
key, value := getKeyStringValue(text, sep)
keyValues[key] = value
}
if err := scanner.Err(); err != nil {
return nil, fmt.Errorf("scanner error: %s", err)
}
return keyValues, nil
}
// func getKeyValuesFromCommand(cmd *exec.Cmd, sep string) (map[string]string, error) {
// stdout, err := cmd.StdoutPipe()
// // stdout, err := cmd.StderrPipe()
// if err != nil {
// log.Fatalf("cmd.StdoutPipe() failed with %s\n", err)
// }
// keyValues := map[string]string{}
// scanner := bufio.NewScanner(stdout)
// fmt.Printf("\n%+v\n\n", cmd)
// cmd.Start()
// for scanner.Scan() {
// text := strings.TrimSpace(scanner.Text())
// if text == "" {
// continue
// }
// fmt.Printf("%s\n", text)
// key, value := getKeyStringValue(text, sep)
// keyValues[key] = value
// }
// return keyValues, scanner.Err()
// }
func getSafePath(path string) string {
ext := filepath.Ext(path)
base := strings.TrimSuffix(path, ext)
safePath := base + ext
_, err := os.Stat(safePath)
for i := 1; err == nil; i++ {
safePath = base + "." + strconv.FormatInt(int64(i), 10) + ext
_, err = os.Stat(safePath)
}
return safePath
}