-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
297 lines (258 loc) · 7.16 KB
/
main.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
package main
import (
"flag"
"fmt"
"os"
"os/user"
"sort"
"sync"
cid "github.com/ipfs/go-cid"
multihash "github.com/multiformats/go-multihash"
)
var (
Version string
Build string
)
type LsColors struct {
DirColor string
SymlinkColor string
ExecutableColor string
DotFileColor string
CIDColor string
}
// Default color formatting if no ls_colors variable set in .bashrc or .zshrc
func getLsColors() LsColors {
defaultColors := LsColors{
DirColor: "\033[34m",
SymlinkColor: "\033[36m",
ExecutableColor: "\033[31m",
DotFileColor: "\033[37m",
CIDColor: "\033[35m",
}
lsColorsEnv := os.Getenv("LS_COLORS")
if lsColorsEnv == "" {
return defaultColors
}
colors := defaultColors
for _, colorSetting := range split(lsColorsEnv, ":") {
parts := split(colorSetting, "=")
if len(parts) != 2 {
continue
}
colorCode := "\033[" + parts[1] + "m"
switch parts[0] {
case "di":
colors.DirColor = colorCode
case "ln":
colors.SymlinkColor = colorCode
case "ex":
colors.ExecutableColor = colorCode
case "cid":
colors.CIDColor = colorCode
}
}
return colors
}
func split(s, sep string) []string {
var parts []string
start := 0
for i := 0; i < len(s); i++ {
if s[i:i+1] == sep {
parts = append(parts, s[start:i])
start = i + 1
}
}
parts = append(parts, s[start:])
return parts
}
// Expand tilde to the full home directory path
func expandTilde(path string) (string, error) {
if path[:1] == "~" {
usr, err := user.Current()
if err != nil {
return "", err
}
return usr.HomeDir + path[1:], nil
}
return path, nil
}
func main() {
// Print version and build number
// fmt.Printf("Version: %s, Build: %s\n", Version, Build)
// CLI flags
helpFlag := flag.Bool("h", false, "Display help information")
versionFlag := flag.Bool("v", false, "Display version information")
flag.Parse()
if *helpFlag {
fmt.Println("Usage: cidls [OPTIONS] [DIRECTORY]")
fmt.Println("List information about the files in the DIRECTORY (the current directory is the default).")
fmt.Println("\nOptions:")
fmt.Println(" -h\tDisplay this help message")
fmt.Println(" -v\tDisplay version information")
return
}
if *versionFlag {
fmt.Printf("Version: %s, Build: %s\n\n", Version, Build)
return
}
// Get directory from $1 argument or use the current directory
var dir string
if len(os.Args) > 1 {
dir = os.Args[1]
} else {
var err error
dir, err = os.Getwd()
if err != nil {
fmt.Println("Error: unable to get current directory:", err)
return
}
}
// Get CID version from $1 argument or use default v1
var cidVersion int = 1
if len(os.Args) > 2 {
versionArg := os.Args[2]
if versionArg == "0" {
cidVersion = 0
} else if versionArg != "1" {
fmt.Println("Error: Invalid CID version. Use 0 or 1.")
return
}
}
// Expand the tilde ~ to the full home directory path
dir, err := expandTilde(dir)
if err != nil {
fmt.Println("Error expanding tilde:", err)
return
}
// Check if directory exists and if it's readable
if _, err := os.Stat(dir); os.IsNotExist(err) {
fmt.Printf("Error: Directory %s does not exist.\n", dir)
return
} else if os.IsPermission(err) {
fmt.Printf("Error: Permission denied for directory %s, try using sudo?\n", dir)
return
}
// Read directory contents
files, err := os.ReadDir(dir)
if err != nil {
fmt.Println("Error reading directory:\n", err)
return
}
// Separate directories and files
var dirs, regularFiles []os.DirEntry
for _, file := range files {
if file.IsDir() {
dirs = append(dirs, file)
} else {
regularFiles = append(regularFiles, file)
}
}
// Sort directories and files
sort.Slice(dirs, func(i, j int) bool { return dirs[i].Name() < dirs[j].Name() })
sort.Slice(regularFiles, func(i, j int) bool { return regularFiles[i].Name() < regularFiles[j].Name() })
// Merge directories and files
sortedFiles := append(dirs, regularFiles...)
// Sort the merged list to ensure directories are always at the top
sort.Slice(sortedFiles, func(i, j int) bool {
if sortedFiles[i].IsDir() && !sortedFiles[j].IsDir() {
return true
}
if !sortedFiles[i].IsDir() && sortedFiles[j].IsDir() {
return false
}
return sortedFiles[i].Name() < sortedFiles[j].Name()
})
// Compute the maximum filename length
maxNameLen := 0
for _, file := range sortedFiles {
if len(file.Name()) > maxNameLen {
maxNameLen = len(file.Name())
}
}
// Channel to collect file names and their CIDs
results := make(chan string, len(sortedFiles))
// Use a WaitGroup to wait for all goroutines
var wg sync.WaitGroup
colors := getLsColors()
resetColor := "\033[0m"
// Start a goroutine for each file
for _, file := range sortedFiles {
wg.Add(1)
go func(file os.DirEntry) {
defer wg.Done()
formattedName := fmt.Sprintf("%-*s", maxNameLen, file.Name())
if file.Name()[0] == '.' {
// Handle dotdirectories and dotfiles
if file.IsDir() {
results <- fmt.Sprintf("%s%s%s\t", colors.DirColor, formattedName, resetColor)
return
}
cidStr, err := computeCID(dir+string(os.PathSeparator)+file.Name(), cidVersion)
if err != nil {
results <- fmt.Sprintf("%s%s%s\tERROR: %s", colors.DotFileColor, formattedName, resetColor, err)
} else {
results <- fmt.Sprintf("%s%s%s\t%s%s%s", colors.DotFileColor, formattedName, resetColor, colors.CIDColor, cidStr, resetColor)
}
return
}
if file.IsDir() {
results <- fmt.Sprintf("%s%s%s\t", colors.DirColor, formattedName, resetColor)
} else if (file.Type() & os.ModeSymlink) == os.ModeSymlink {
results <- fmt.Sprintf("%s%s%s\t", colors.SymlinkColor, formattedName, resetColor)
} else if (file.Type() & os.ModePerm) == 0111 {
results <- fmt.Sprintf("%s%s%s\t", colors.ExecutableColor, formattedName, resetColor)
} else {
// Check file readability and output any errors
cidStr, err := computeCID(dir+string(os.PathSeparator)+file.Name(), cidVersion)
if err != nil {
if err.Error() == "insufficient permissions" || err.Error() == "no such file" {
results <- fmt.Sprintf("%s\t\033[31m%s\033[0m", formattedName, err.Error())
} else {
results <- fmt.Sprintf(formattedName, err)
}
} else {
results <- fmt.Sprintf("%s\t%s%s%s", formattedName, colors.CIDColor, cidStr, resetColor)
}
}
}(file)
}
// Close the results channel after all goroutines are done
go func() {
wg.Wait()
close(results)
}()
// Print results as they come in
for result := range results {
fmt.Println(result)
}
}
func computeCID(filename string, cidVersion int) (string, error) {
// Attempt to open the file in read-only mode to check permissions
file, err := os.OpenFile(filename, os.O_RDONLY, 0)
if err != nil {
if os.IsPermission(err) {
return "", fmt.Errorf("insufficient permissions")
}
if os.IsNotExist(err) {
return "", fmt.Errorf("no such file")
}
return "", err
}
file.Close()
// Now read the file
content, err := os.ReadFile(filename)
if err != nil {
return "", err
}
hash, err := multihash.Sum(content, multihash.SHA2_256, -1)
if err != nil {
return "", err
}
var c cid.Cid
if cidVersion == 0 {
c = cid.NewCidV0(hash)
} else {
c = cid.NewCidV1(cid.Raw, hash)
}
return c.String(), nil
}