-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
314 lines (237 loc) · 4.66 KB
/
Copy pathmain.go
File metadata and controls
314 lines (237 loc) · 4.66 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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package main
import ("fmt"
"os"
"strings"
"log"
"encoding/csv"
"encoding/json"
"bufio"
)
func Readfile(){
Reader:= bufio.NewReader(os.Stdin)
fmt.Println("Include file to read?")
input, err := Reader.ReadString('\n')
if err != nil{
fmt.Println(err)
}
input = strings.TrimSpace(input)
file, err := os.ReadFile(input)
if err != nil{
if os.IsNotExist(err){
fmt.Println("File Does not exist")
} else{
fmt.Println("Error reading", err)
}
return
}
fmt.Println(string(file))
}
func WriteFile(){
reader:= bufio.NewReader(os.Stdin)
fmt.Println("Create New file")
input, err := reader.ReadString('\n')
if err != nil{
fmt.Println(err)
}
input = strings.TrimSpace(input)
Add :=bufio.NewReader(os.Stdin)
fmt.Println("Add your words here")
write, err := Add.ReadString('\n')
if err != nil {
fmt.Println(err)
}
write = strings.TrimSpace(write)
file, err:= os.OpenFile(input, os.O_WRONLY| os.O_CREATE, 0644)
if err != nil{
fmt.Println(err)
}
defer file.Close()
data:=write
_,err = file.WriteString(data)
if err != nil{
fmt.Println(err)
}
fmt.Println("Success")
}
func AppendText(){
Reader:=bufio.NewReader(os.Stdin)
fmt.Println("Type your file to Append")
input, err := Reader.ReadString('\n')
if err != nil{
fmt.Println(err)
}
input = strings.TrimSpace(input)
Usertext:= bufio.NewReader(os.Stdin)
fmt.Println("Add text to file")
text,err := Usertext.ReadString('\n')
if err != nil{
fmt.Println("Here is the error",err)
}
text =strings.TrimSpace(text)
file, err:= os.OpenFile(input, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
if err != nil{
fmt.Println("Encountering error ",err)
}
defer file.Close()
//write inside file
app:=(text + "\n")
_,err = file.WriteString(app + "\n")
if err != nil {
fmt.Println("Encountering eror in filewrite",err)
}
fmt.Println("Successfully Appended")
}
type Config struct {
DBHost string `json:"database_host"`
DBPort int `json:"database_port"`
DBUsername string `json:"database_username"`
DBPassword string `json:"database_password"`
ServerPort int `json:"server_port"`
ServerDebug bool `json:"server_debug"`
ServerTimeout int `json:"server_timeout"`
}
func ReadJson(){
Reader := bufio.NewReader(os.Stdin)
fmt.Println("Specify file to read")
input, err := Reader.ReadString('\n')
if err != nil{
log.Fatal(err)
}
input = strings.TrimSpace(input)
filepath:=input
file, err := os.Open(filepath)
var config Config
if err != nil {
if os.IsNotExist(err){
fmt.Println("File not found")
} else {
log.Fatal(err)
}
}
defer file.Close()
Jsondecode:= json.NewDecoder(file)
err = Jsondecode.Decode(&config)
if err != nil{
log.Fatal(err)
}
fmt.Println(config)
}
func csvRead(){
Reader:= bufio.NewReader(os.Stdin)
fmt.Println("Specify csv file to read")
input, err := Reader.ReadString('\n')
if err != nil{
fmt.Println(err)
}
input = strings.TrimSpace(input)
filepath:= input
file, err := os.Open(filepath)
if err != nil{
if os.IsNotExist(err){
fmt.Println("File not found")
} else {
fmt.Println(err)
}
return
}
defer file.Close()
read:= csv.NewReader(file)
csvfile, err := read.ReadAll()
if err != nil{
fmt.Println(err)
}
fmt.Println(csvfile)
}
func CreateDir() {
Reader:= bufio.NewReader(os.Stdin)
fmt.Println("Folder name")
input, err := Reader.ReadString('\n')
if err != nil{
fmt.Println(err)
}
input = strings.TrimSpace(input)
err = os.Mkdir(input, 0755)
if err != nil{
fmt.Println(err)
}
fmt.Println("Folder Created")
}
func ListFile(){
Reader:= bufio.NewReader(os.Stdin)
fmt.Println("List file in this folder")
input, err:=Reader.ReadString('\n')
if err != nil{
fmt.Println(err)
}
input = strings.TrimSpace(input)
dirls, err := os.ReadDir(input)
if err != nil{
if os.IsNotExist(err){
fmt.Println("file not Found ")
} else {
fmt.Println(err)
}
}
for _, entry := range dirls{
fmt.Println(entry.Name())
}
}
func Delete() {
Reader:= bufio.NewReader(os.Stdin)
fmt.Println("Delete file")
del, err := Reader.ReadString('\n')
if err != nil{
fmt.Println(err)
}
del = strings.TrimSpace(del)
err = os.Remove(del)
if err != nil {
fmt.Println(err)
}
fmt.Println("Deleted")
}
func main(){
for{
var Choice int
fmt. Println("1. Read File")
fmt. Println("2. Create New file")
fmt. Println("3. Append File")
fmt. Println("4. Read Json")
fmt. Println("5. Read Csv")
fmt. Println("6. Create Directory")
fmt. Println("7. List File")
fmt. Println("8. Delet File")
fmt. Println("9. Exit")
fmt.Scanln(&Choice)
switch Choice {
case 1:
Readfile()
return
case 2:
WriteFile()
return
case 3:
AppendText()
return
case 4:
ReadJson()
return
case 5:
csvRead()
return
case 6:
CreateDir()
return
case 7:
ListFile()
return
case 8:
Delete()
return
case 9:
return
default:
fmt.Println("Unknow error")
}
}
}