-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.go
More file actions
78 lines (66 loc) · 1.67 KB
/
utils.go
File metadata and controls
78 lines (66 loc) · 1.67 KB
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
package shared
import (
"bufio"
"fmt"
"os"
"path/filepath"
)
// CLI Commands
const (
AUTOCMD = "auto"
MANUALCMD = "manual"
TrackAutoCMD = AUTOCMD
TrackManualCMD = MANUALCMD
)
const (
InfoCollectionSuccess = "All benchmarks and profile processing completed successfully!"
IMPROVEMENT = "IMPROVEMENT"
REGRESSION = "REGRESSION"
STABLE = "STABLE"
)
const (
MainDirOutput = "bench"
ProfileTextDir = "text"
ProfileBinDir = "bin"
PermDir = 0o755
PermFile = 0o644
FunctionsDirSuffix = "_functions"
TextExtension = "txt"
ConfigFilename = "config_template.json"
GlobalSign = "*"
)
func GetScanner(filePath string) (*bufio.Scanner, *os.File, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, nil, fmt.Errorf("cannot read profile file %s: %w", filePath, err)
}
scanner := bufio.NewScanner(file)
return scanner, file, nil
}
// CleanOrCreateDir cleans a directory if it exists, or creates one if it.
func CleanOrCreateDir(dir string) error {
info, err := os.Stat(dir)
if err != nil {
if os.IsNotExist(err) {
if err = os.MkdirAll(dir, PermDir); err != nil {
return fmt.Errorf("failed to create %s directory: %w", dir, err)
}
return nil
}
return err
}
if !info.IsDir() {
return fmt.Errorf("path is not a directory: %s", dir)
}
entries, err := os.ReadDir(dir)
if err != nil {
return fmt.Errorf("failed to read directory: %w", err)
}
for _, entry := range entries {
path := filepath.Join(dir, entry.Name())
if err = os.RemoveAll(path); err != nil {
return fmt.Errorf("failed to remove %s: %w", path, err)
}
}
return nil
}