-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.go
170 lines (140 loc) · 3 KB
/
utils.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
package main
import (
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"runtime"
)
func createFile(filename string) {
if _, err := os.Stat(filename); err == nil {
// File exists, prompt user to overwrite
answer := ""
for answer != "y" && answer != "n" {
fmt.Print("File already exists. Do you want to overwrite it? (y/n): ")
_, err := fmt.Scanln(&answer)
if err != nil {
fmt.Println(err)
return
}
}
if answer == "n" {
return
}
}
// Create the file
file, err := os.Create(filename)
if err != nil {
fmt.Println("Error creating file:", err)
return
}
defer file.Close()
fmt.Println("File created:", filename)
}
func readFile(filename string) {
file, err := os.Open(filename)
if err != nil {
fmt.Println("Error opening file:", err)
return
}
defer file.Close()
io.Copy(os.Stdout, file)
}
func writeFile(filename, content string) {
file, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
fmt.Println("Error opening file:", err)
return
}
defer file.Close()
_, err = file.WriteString(content)
if err != nil {
fmt.Println("Error writing to file:", err)
return
}
fmt.Println("Content written to file:", filename)
}
func deleteFile(filename string) {
err := os.Remove(filename)
if err != nil {
fmt.Println("Error deleting file:", err)
return
}
fmt.Println("File deleted:", filename)
}
func listFiles(directory string) {
filepath.Walk(directory, func(path string, info os.FileInfo, err error) error {
if err != nil {
fmt.Println("Error accessing path:", path)
return nil
}
if !info.IsDir() {
fmt.Println(path)
}
return nil
})
}
func copyFile(src, dest string) {
sourceFile, err := os.Open(src)
if err != nil {
fmt.Println("Error opening source file:", err)
return
}
defer sourceFile.Close()
destFile, err := os.Create(dest)
if err != nil {
fmt.Println("Error creating destination file:", err)
return
}
defer destFile.Close()
_, err = io.Copy(destFile, sourceFile)
if err != nil {
fmt.Println("Error copying file:", err)
return
}
fmt.Println("File copied from", src, "to", dest)
}
func moveFile(src, dest string) {
err := os.Rename(src, dest)
if err != nil {
fmt.Println("Error moving file:", err)
return
}
fmt.Println("File moved from", src, "to", dest)
}
func openFile(filename string) error {
var cmd *exec.Cmd
if runtime.GOOS == "windows" {
// On Windows, use "code.cmd"
cmd = exec.Command("code.cmd", filename)
handleCmdOs(cmd)
err := cmd.Run()
if err != nil {
cmd = exec.Command("notepad", filename)
handleCmdOs(cmd)
runErr := cmd.Run()
if runErr != nil {
fmt.Println(runErr)
}
}
} else {
// implement such that the default editor is opened
cmd = exec.Command("editor", filename)
handleCmdOs(cmd)
runErr := cmd.Run()
if runErr != nil {
fmt.Println(runErr)
}
}
err := cmd.Start()
if err != nil {
return err
}
return nil
}
func handleCmdOs(cmd *exec.Cmd) {
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
}