-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
108 lines (88 loc) · 2.24 KB
/
main.go
File metadata and controls
108 lines (88 loc) · 2.24 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
package main
import (
"bufio"
"fmt"
"os"
"path/filepath"
"strings"
)
func main() {
scanDir := getInput("Enter directory to scan (current dir if empty): ", true)
if scanDir == "." || scanDir == "./" || scanDir == "" {
scanDir, _ = os.Getwd()
}
showOnlyDirs := getYesNoInput("Show only directories? (y/n): ")
outputPath := getInput("Enter output path for output.txt (current dir if empty): ", true)
if outputPath == "." || outputPath == "./" || outputPath == "" {
outputPath, _ = os.Getwd()
}
outputFile := filepath.Join(outputPath, "output.txt")
file, err := os.Create(outputFile)
if err != nil {
fmt.Printf("Error creating file: %v\n", err)
return
}
defer file.Close()
writer := bufio.NewWriter(file)
defer writer.Flush()
walkFunc := func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
relPath, err := filepath.Rel(scanDir, path)
if err != nil {
return err
}
if relPath == "." {
return nil
}
if showOnlyDirs && !info.IsDir() {
return nil
}
depth := len(strings.Split(relPath, string(os.PathSeparator))) - 1
indent := strings.Repeat(" ", depth)
var entry string
if info.IsDir() {
entry = fmt.Sprintf("%s📁 %s\n", indent, filepath.Base(path))
} else {
entry = fmt.Sprintf("%s📄 %s\n", indent, filepath.Base(path))
}
_, err = writer.WriteString(entry)
return err
}
fmt.Println("Scanning directory...")
err = filepath.Walk(scanDir, walkFunc)
if err != nil {
fmt.Printf("Error scanning directory: %v\n", err)
return
}
fmt.Printf("Results saved to: %s\n", outputFile)
}
func getInput(prompt string, isPath bool) string {
reader := bufio.NewReader(os.Stdin)
fmt.Print(prompt)
input, _ := reader.ReadString('\n')
input = strings.TrimSpace(input)
if isPath && input != "" {
if strings.HasPrefix(input, "~") {
home, _ := os.UserHomeDir()
input = home + input[1:]
}
input = filepath.Clean(input)
}
return input
}
func getYesNoInput(prompt string) bool {
reader := bufio.NewReader(os.Stdin)
for {
fmt.Print(prompt)
input, _ := reader.ReadString('\n')
input = strings.TrimSpace(strings.ToLower(input))
if input == "y" {
return true
} else if input == "n" {
return false
}
fmt.Println("Please enter 'y' or 'n'")
}
}