-
Notifications
You must be signed in to change notification settings - Fork 3
/
utility.go
355 lines (312 loc) · 10.7 KB
/
utility.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
package main
import (
"bytes"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"
"github.com/goccy/go-json"
"github.com/pkg/xattr"
"github.com/zeebo/blake3"
)
// removeDuplicates removes duplicate binaries from the list (used in ./install.go)
func removeDuplicates(binaries []string) []string {
seen := make(map[string]struct{})
result := []string{}
for _, binary := range binaries {
if _, ok := seen[binary]; !ok {
seen[binary] = struct{}{}
result = append(result, binary)
}
}
return result
}
// contanins will return true if the provided slice of []strings contains the word str
func contains(slice []string, str string) bool {
for _, v := range slice {
if v == str {
return true
}
}
return false
}
// fileExists checks if a file exists.
func fileExists(filePath string) bool {
_, err := os.Stat(filePath)
return !os.IsNotExist(err)
}
// isDirectory checks if the given path is a directory.
func isDirectory(path string) (bool, error) {
info, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
return false, nil // Path does not exist
}
return false, err
}
return info.IsDir(), nil
}
// isExecutable checks if the file at the specified path is executable.
func isExecutable(filePath string) bool {
info, err := os.Stat(filePath)
if err != nil {
return false
}
return info.Mode().IsRegular() && (info.Mode().Perm()&0o111) != 0
}
// validateProgramsFrom checks the validity of programs against a remote source
func validateProgramsFrom(config *Config, programsToValidate []string, metadata map[string]interface{}) ([]string, error) {
installDir := config.InstallDir
remotePrograms, err := listBinaries(metadata)
if err != nil {
return nil, fmt.Errorf("failed to list remote binaries: %w", err)
}
files, err := listFilesInDir(installDir)
if err != nil {
return nil, fmt.Errorf("failed to list files in %s: %w", installDir, err)
}
programsToValidate = removeDuplicates(programsToValidate)
validPrograms := make([]string, 0, len(programsToValidate))
// Inline function to validate a file against the remote program list
validate := func(file string) (string, bool) {
fullBinaryName := listInstalled(file) // Get the full binary name of the file
if config.RetakeOwnership == true {
fullBinaryName = filepath.Base(file)
if fullBinaryName == "" {
return "", false // If we couldn't get a valid name, return invalid
}
}
// Check if the full name exists in the remote programs
if contains(remotePrograms, fullBinaryName) {
return fullBinaryName, true
}
return "", false
}
if len(programsToValidate) == 0 {
// Validate all files in the directory
for _, file := range files {
if fullName, valid := validate(file); valid {
validPrograms = append(validPrograms, fullName)
}
}
} else {
// Validate only the specified programs
for _, program := range programsToValidate {
file := filepath.Join(installDir, program)
if fullName, valid := validate(file); valid {
validPrograms = append(validPrograms, fullName)
}
}
}
return validPrograms, nil
}
func listInstalled(binaryPath string) string {
if isSymlink(binaryPath) {
return ""
}
// Retrieve the fullName of the binary
fullBinaryName, err := getFullName(binaryPath)
if err != nil || fullBinaryName == "" {
return "" // If we can't get the full name, consider it invalid
}
return fullBinaryName
}
// errorEncoder generates a unique error code based on the sum of ASCII values of the error message.
func errorEncoder(format string, args ...interface{}) int {
formattedErrorMessage := fmt.Sprintf(format, args...)
var sum int
for _, char := range formattedErrorMessage {
sum += int(char)
}
errorCode := sum % 256
fmt.Fprint(os.Stderr, formattedErrorMessage)
return errorCode
}
// errorOut prints the error message to stderr and exits the program with the error code generated by errorEncoder.
func errorOut(format string, args ...interface{}) {
os.Exit(errorEncoder(format, args...))
}
// GetTerminalWidth attempts to determine the width of the terminal.
// It first tries using "stty size", then "tput cols", and finally falls back to 80 columns.
func getTerminalWidth() int {
// Try using stty size
cmd := exec.Command("stty", "size")
cmd.Stdin = os.Stdin
out, err := cmd.Output()
if err == nil {
// stty size returns rows and columns
parts := strings.Split(strings.TrimSpace(string(out)), " ")
if len(parts) == 2 {
width, _ := strconv.Atoi(parts[1])
return width
}
}
// Fallback to tput cols
cmd = exec.Command("tput", "cols")
cmd.Stdin = os.Stdin
out, err = cmd.Output()
if err == nil {
width, _ := strconv.Atoi(strings.TrimSpace(string(out)))
return width
}
// Fallback to 80 columns
return 80
}
// NOTE: \n will always get cut off when using a truncate function, this may also happen to other formatting options
// truncateSprintf formats the string and truncates it if it exceeds the terminal width.
func truncateSprintf(indicator, format string, a ...interface{}) string {
// Format the string first
formatted := fmt.Sprintf(format, a...)
// Check if output is piped
if isPipedOutput() {
return formatted // No truncation if output is being piped to another program
}
// Determine the truncation length & truncate the formatted string if it exceeds the available space
availableSpace := getTerminalWidth() - len(indicator)
if len(formatted) > availableSpace {
formatted = formatted[:availableSpace]
// Remove trailing punctuation and spaces
for strings.HasSuffix(formatted, ",") || strings.HasSuffix(formatted, ".") || strings.HasSuffix(formatted, " ") {
formatted = formatted[:len(formatted)-1]
}
formatted = fmt.Sprintf("%s%s", formatted, indicator) // Add the indicator (the dots)
}
return formatted
}
// truncatePrintf is a drop-in replacement for fmt.Printf that truncates the input string if it exceeds a certain length.
func truncatePrintf(disableTruncation, addNewLine bool, format string, a ...interface{}) (n int, err error) {
if disableTruncation {
return fmt.Printf(format, a...)
}
if addNewLine {
return fmt.Println(truncateSprintf(indicator, format, a...))
}
return fmt.Print(truncateSprintf(indicator, format, a...))
}
// listFilesInDir lists all files in a directory
func listFilesInDir(dir string) ([]string, error) {
entries, err := os.ReadDir(dir)
if err != nil {
return nil, err
}
files := make([]string, 0, len(entries))
for _, entry := range entries {
if !entry.IsDir() {
files = append(files, filepath.Join(dir, entry.Name()))
}
}
return files, nil
}
// getFullName retrieves the full binary name from the extended attributes of the binary file.
// If the binary does not exist, it returns the basename. If the full name attribute cannot be retrieved, it returns an error.
func getFullName(binaryPath string) (string, error) {
// Check if the binary file exists using the existing isFileExist function
if !fileExists(binaryPath) {
// Return the basename if the file doesn't exist
return filepath.Base(binaryPath), nil
}
// Retrieve the "user.FullName" attribute
fullName, err := xattr.Get(binaryPath, "user.FullName")
if err != nil {
// Return an error if the full name cannot be retrieved but the binary exists
return "", fmt.Errorf("full name attribute not found for binary: %s", binaryPath)
}
return string(fullName), nil
}
// addFullName writes the full binary name to the extended attributes of the binary file.
func addFullName(binaryPath string, fullName string) error {
// Set the "user.FullName" attribute
if err := xattr.Set(binaryPath, "user.FullName", []byte(fullName)); err != nil {
return fmt.Errorf("failed to set xattr for %s: %w", binaryPath, err)
}
return nil
}
// removeNixGarbageFoundInTheRepos corrects any /nix/store/ or /bin/ binary path in the file.
func removeNixGarbageFoundInTheRepos(filePath string) error {
// Read the entire file content
content, err := os.ReadFile(filePath)
if err != nil {
return fmt.Errorf("failed to read file %s: %v", filePath, err)
}
// Regex to match and remove the /nix/store/.../ prefix in the shebang line, preserving the rest of the path
nixShebangRegex := regexp.MustCompile(`^#!\s*/nix/store/[^/]+/`)
// Regex to match and remove the /nix/store/*/bin/ prefix in other lines
nixBinPathRegex := regexp.MustCompile(`/nix/store/[^/]+/bin/`)
// Split content by lines
lines := strings.Split(string(content), "\n")
// Flag to track if any corrections were made
correctionsMade := false
// Handle the shebang line separately if it exists and matches the nix pattern
if len(lines) > 0 && nixShebangRegex.MatchString(lines[0]) {
lines[0] = nixShebangRegex.ReplaceAllString(lines[0], "#!/")
// Iterate through the rest of the lines and correct any /nix/store/*/bin/ path
for i := 1; i < len(lines); i++ {
if nixBinPathRegex.MatchString(lines[i]) {
lines[i] = nixBinPathRegex.ReplaceAllString(lines[i], "")
}
}
correctionsMade = true
}
// If any corrections were made, write the modified content back to the file
if correctionsMade {
if err := os.WriteFile(filePath, []byte(strings.Join(lines, "\n")), 0644); err != nil {
return fmt.Errorf("failed to correct nix object [%s]: %v", filepath.Base(filePath), err)
}
fmt.Printf("[%s] is a nix object. Corrections have been made.\n", filepath.Base(filePath))
}
return nil
}
func fetchJSON(url string, v interface{}) error {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return fmt.Errorf("error creating request for %s: %v", url, err)
}
req.Header.Set("Cache-Control", "no-cache, no-store, must-revalidate")
req.Header.Set("Pragma", "no-cache")
req.Header.Set("Expires", "0")
client := &http.Client{}
response, err := client.Do(req)
if err != nil {
return fmt.Errorf("error fetching from %s: %v", url, err)
}
defer response.Body.Close()
body := &bytes.Buffer{}
if _, err := io.Copy(body, response.Body); err != nil {
return fmt.Errorf("error reading from %s: %v", url, err)
}
if err := json.Unmarshal(body.Bytes(), v); err != nil {
return fmt.Errorf("error decoding from %s: %v", url, err)
}
return nil
}
// calculateChecksum calculates the checksum of a file
func calculateChecksum(filePath string) (string, error) {
file, err := os.Open(filePath)
if err != nil {
return "", err
}
defer file.Close()
hasher := blake3.New()
if _, err := io.Copy(hasher, file); err != nil {
return "", err
}
return fmt.Sprintf("%x", hasher.Sum(nil)), nil
}
// isPipedOutput checks if the output is piped
func isPipedOutput() bool {
// Check if stdout is a pipe
fileInfo, err := os.Stdout.Stat()
if err != nil {
return false // Default to not piped if there's an error
}
return (fileInfo.Mode() & os.ModeNamedPipe) != 0
}
func isSymlink(filePath string) bool {
fileInfo, err := os.Lstat(filePath)
return err == nil && fileInfo.Mode()&os.ModeSymlink != 0
}