-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete_files.go
More file actions
90 lines (83 loc) · 1.84 KB
/
delete_files.go
File metadata and controls
90 lines (83 loc) · 1.84 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
package main
import (
"bufio"
"fmt"
"io"
"log"
"os"
"strings"
)
func Delete(input string,mode string,reader io.Reader, writer io.Writer){
switch mode {
case "delete":
fmt.Println("Are you sure you want to delete the directory/file")
fmt.Println("Press Y/N")
reader:=bufio.NewScanner(reader)
reader.Scan()
choice:=reader.Text()
switch strings.ToLower(choice){
case "y":
DeleteDirectory(input,mode,writer)
case "n":
fmt.Fprint(writer,"Delete operation cancelled")
default:
fmt.Fprint(writer,"Invalid input please enter Y or N according to your needs")
}
case "move":
DeleteDirectory(input,mode,writer)
}
}
func DeleteDirectory(input string,mode string,writer io.Writer){
switch mode{
case "delete":
inputs:=strings.Split(input, " ")
length:=len(inputs)
if length==2{
deleteAllContentsOfDirectory(inputs[1],writer)
}else{
fmt.Fprintln(writer,"Invalid number of arguments")
}
case "move":
DeleteForMove(input,writer)
}
}
func IsDirectoryEmpty(directory string)(bool,error){
f, err := os.Open(directory)
if err != nil {
return false, err
}
defer f.Close()
_, err = f.Readdirnames(1)
if err == io.EOF {
return true, nil
}
return false, err
}
func deleteAllContentsOfDirectory(directory string,writer io.Writer){
result:=IsDirectory(directory)
if result{
files,err:=os.ReadDir(directory)
if err!=nil{
fmt.Println("Error occured : ",err)
}
for _,file:=range files{
path:=directory+"/"+file.Name()
isDirectory:=IsDirectory(path)
if isDirectory{
deleteAllContentsOfDirectory(path,writer)
}
os.Remove(path)
}
}
os.Remove(directory)
}
func IsDirectory(directory string)bool{
info,err:=os.Stat(directory)
if err!=nil{
log.Fatal(err)
}
return info.IsDir()
}
func DeleteForMove(dir string,writer io.Writer){
deleteAllContentsOfDirectory(dir,os.Stdout)
}