-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
67 lines (54 loc) · 1.34 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
package main
import (
"flag"
"fmt"
"io/fs"
"log"
"os"
"slices"
)
const (
Reset = "\033[0m"
Blue = "\033[34m"
)
func getCurrentWorkingDir() string {
dir, err := os.Getwd()
if err != nil {
log.Fatalf("Error occured while getting cwd: %v", err)
}
return dir
}
func openCurrentWorkingDir(currentWorkingDir string) []fs.DirEntry {
dir, err := os.ReadDir(currentWorkingDir)
if err != nil {
log.Fatalf("Error opening current working directory: %v", err)
}
return dir
}
func main() {
var err error
dir := getCurrentWorkingDir()
longListFlag := flag.Bool("l", false, "use a long listing format")
allListFlag := flag.Bool("a", false, "do not ignore entries starting with .")
humanReadableFlag := flag.Bool("h", false, "with -l and -s, print sizes like 1K 234M 2G etc.")
listOneFilePerLine := flag.Bool(
"1",
false,
"list one file per line. Avoid '\\n' with -q or -b",
)
// Preprocess flags to support combined flags like -la
args := preprocessArgs(os.Args[1:])
flag.CommandLine.Parse(args)
if flag.NFlag() == 0 ||
(containsAny(args, []string{"-a", "-h", "-1"}) && !slices.Contains(args, "-l")) {
ListFiles(dir, *allListFlag, *listOneFilePerLine)
}
switch *longListFlag {
case true:
err = LongListFiles(dir, *allListFlag, *humanReadableFlag)
}
if err != nil {
fmt.Printf("An error occured: %v\n", err)
os.Exit(0)
}
}