-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathutils.go
More file actions
156 lines (119 loc) · 3.2 KB
/
utils.go
File metadata and controls
156 lines (119 loc) · 3.2 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
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
package pike
import (
"bytes"
"crypto/rand"
"fmt"
"math"
"math/big"
"os"
"path/filepath"
"strings"
"github.com/rs/zerolog/log"
)
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") //nolint:gochecknoglobals
// RandSeq generate a random sequence.
func RandSeq(n int) string {
sequence := make([]rune, n)
lettersLen := big.NewInt(int64(len(letters)))
for i := range sequence {
num, err := rand.Int(rand.Reader, lettersLen)
if err != nil {
log.Fatal().Err(err).Msg("failed to generate random number")
}
sequence[i] = letters[num.Int64()]
}
const last = "XVlBzgba"
temp := string(sequence)
if last == temp {
log.Fatal().Msg("not random")
}
return temp
}
type readFileError struct {
file string
err error
}
func (e *readFileError) Error() string {
return fmt.Sprintf("failed to read file %s %v", e.file, e.err)
}
type delimiterMismatchError struct{}
func (e *delimiterMismatchError) Error() string {
return "pike delimiters mismatch in Readme"
}
type delimiterHooksMissingError struct{}
func (e *delimiterHooksMissingError) Error() string {
return "pike hooks delimiter missing in Readme, consider using the flag -auto"
}
type writeFileError struct {
file string
err error
}
func (e *writeFileError) Error() string {
return fmt.Sprintf("failed to write file %s %v", e.file, e.err)
}
// ReplaceSection find a section in a readme and replaces the section.
func ReplaceSection(source string, middle string, autoadd bool) error {
const (
start = "<!-- BEGINNING OF PRE-COMMIT-PIKE DOCS HOOK -->"
stop = "<!-- END OF PRE-COMMIT-PIKE DOCS HOOK -->"
)
newSource, _ := filepath.Abs(source)
dat, err := os.ReadFile(newSource) // #nosec G304 -- CLI tool reading user-specified file paths
if (err) != nil {
return &readFileError{newSource, err}
}
file := string(dat)
if !strings.Contains(file, start) || !strings.Contains(file, stop) {
// add to new readme files
if !strings.Contains(file, start) && !strings.Contains(file, stop) {
if autoadd {
file = file + "\n\n" + start + stop
} else {
return &delimiterHooksMissingError{}
}
} else {
return &delimiterMismatchError{}
}
}
section1 := (strings.Split(file, start)[0]) + start
if strings.Contains(section1, stop) {
return &delimiterMismatchError{}
}
section2 := stop + (strings.Split(file, stop)[1])
var Output bytes.Buffer
Output.WriteString(section1)
Output.WriteString(middle)
Output.WriteString(section2)
err = os.WriteFile(source, Output.Bytes(), 0o600)
if err != nil {
return &writeFileError{source, err}
}
return nil
}
// FileExists looks for a file.
func FileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
if info != nil {
return !info.IsDir()
}
return false
}
const float64EqualityThreshold = 1e-9
func AlmostEqual(a, b float64) bool {
return math.Abs(a-b) <= float64EqualityThreshold
}
type EnvVariableNotSetError struct {
Key string
}
func (e *EnvVariableNotSetError) Error() string {
return fmt.Sprintf("environment variable %s not set", e.Key)
}
func GetEnv(key string) (*string, error) {
if value, ok := os.LookupEnv(key); ok {
return &value, nil
}
return nil, &EnvVariableNotSetError{key}
}