generated from muxit-studio/cmd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs.go
200 lines (164 loc) · 3.71 KB
/
fs.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
package discord
import (
"bufio"
"errors"
"fmt"
"io/fs"
"log"
"os"
"path/filepath"
"strings"
)
const (
DefaultPerms = 0600
)
func Create(path string) error {
myfile, err := os.Create(path)
if err != nil {
return err
}
if err := myfile.Close(); err != nil {
return err
}
return nil
}
// Read retrieves content from a file
func Read(filePath string) (string, error) {
bytes, err := os.ReadFile(filePath)
if err != nil {
return "", err
}
return string(bytes), nil
}
func Input(prompt string) string {
reader := bufio.NewReader(os.Stdin)
fmt.Print(prompt)
text, _ := reader.ReadString('\n')
return text
}
func InputConfirm(prompt string) bool {
answer := Input(prompt + " (y/n): ")
answer = strings.ToLower(strings.TrimSpace(answer))
if answer == "y" || answer == "yes" {
return true
}
return false
}
// ReadLines returns all lines from a file
func ReadLines(filePath string) ([]string, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
scanner := bufio.NewScanner(file)
lines := []string{}
for scanner.Scan() {
line := scanner.Text()
lines = append(lines, line)
}
// close file descriptor
if err := file.Close(); err != nil {
return nil, err
}
return lines, nil
}
// Write writes content to a file. It will overwrite to the file if it already
// exists and create the file if it does not.
func Write(path string, text string) error {
return os.WriteFile(path, []byte(text), 0644)
}
// WriteAppend writes content to a file. It will append to the file if it already
// exists and create the file if it does not.
func WriteAppend(path string, text string) error {
f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
if _, err := f.WriteString(text); err != nil {
return err
}
if err := f.Close(); err != nil {
return err
}
return nil
}
func Mkdir(path string) error {
if err := os.MkdirAll(path, 0755); err != nil {
return fmt.Errorf("failed to create directory: %s, error: %w", path, err)
}
return nil
}
// List returns all files in a directory, same as `ls`
func List(dir string) []string {
files, err := os.ReadDir(dir)
if err != nil {
log.Fatal(err)
}
var paths []string
for _, file := range files {
fullPath := filepath.Join(dir, file.Name())
paths = append(paths, fullPath)
}
return paths
}
func Remove(path string) error {
return os.Remove(path)
}
// rm -r
func RemoveAll(path string) error {
return os.RemoveAll(path)
}
func Exists(path string) bool {
_, err := os.Stat(path)
return !errors.Is(err, fs.ErrNotExist)
}
func InsertLine(path, newLine string) error {
lines, err := ReadLines(path)
if err != nil {
return err
}
fileContent := ""
for _, line := range lines {
fileContent += line
fileContent += "\n"
}
fileContent += newLine
fileContent += "\n"
return os.WriteFile(path, []byte(fileContent), 0644)
}
func InsertLineAtIndex(path, newLine string, index int) error {
lines, err := ReadLines(path)
if err != nil {
return err
}
fileContent := ""
for i, line := range lines {
if i == index {
fileContent += newLine
fileContent += "\n"
}
fileContent += line
fileContent += "\n"
}
return os.WriteFile(path, []byte(fileContent), 0644)
}
// Open opens a file with the default open command from the user system
func Open(path string) error {
cmd := fmt.Sprintf("open %s", path)
if err := Exec(cmd); err != nil {
return err
}
return nil
}
// Editor opens a file with the default $EDITOR from the user system
func Editor(path string) error {
editor := os.Getenv("EDITOR")
if editor == "" {
return errors.New("error: $EDITOR is not set")
}
cmd := fmt.Sprintf("%s %s", editor, path)
if err := Exec(cmd); err != nil {
return err
}
return nil
}