-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli.go
296 lines (267 loc) · 6.82 KB
/
cli.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
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
package main
import (
"fmt"
"os"
"strconv"
"github.com/spf13/cobra"
)
const pvDirectory = "/data"
const appDirectory = "/app" // `app` means `volume-toolkit` in this context.
var rootCmd = &cobra.Command{
Use: "volume-toolkit",
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}
var filePath string
func init() {
rootCmd.Flags().SortFlags = false
rootCmd.AddCommand(importCmd)
rootCmd.AddCommand(exportCmd)
rootCmd.AddCommand(sizeCmd)
rootCmd.AddCommand(destroyCmd)
rootCmd.AddCommand(fileOpCmd)
importCmd.Flags().String("path", "/app/backup.tar.gz", "Path of tar to import")
importCmd.Flags().String("http-method", "GET", "HTTP method to use")
exportCmd.Flags().String("path", "/app/backup.tar.gz", "Path of tar to export") // Path or URL where the backup will be stored.
exportCmd.Flags().String("http-method", "PUT", "HTTP method to use")
fileOpCmd.PersistentFlags().StringVar(&filePath, "path", "", "Path of the file")
fileOpCmd.MarkPersistentFlagRequired("path")
fileOpCmd.Flags().SortFlags = false
fileOpCmd.AddCommand(lsCmd)
fileOpCmd.AddCommand(catCmd)
fileOpCmd.AddCommand(cpCmd)
fileOpCmd.AddCommand(mvCmd)
fileOpCmd.AddCommand(rmCmd)
fileOpCmd.AddCommand(mkdirCmd)
fileOpCmd.AddCommand(chmodCmd)
chownCmd.Flags().String("uid", "", "User ID")
chownCmd.Flags().String("gid", "", "Group ID")
chownCmd.MarkFlagRequired("uid")
chownCmd.MarkFlagRequired("gid")
fileOpCmd.AddCommand(chownCmd)
fileOpCmd.AddCommand(downloadCmd)
}
func main() {
// Ensure /data directory exists.
if _, err := os.Stat(pvDirectory); os.IsNotExist(err) {
fmt.Printf("Directory %s does not exist", pvDirectory)
os.Exit(1)
}
// Create /app directory if it does not exist. Will act as a temporary directory.
if _, err := os.Stat(appDirectory); os.IsNotExist(err) {
os.Mkdir(appDirectory, 0777)
}
rootCmd.Execute()
}
var importCmd = &cobra.Command{
Use: "import",
Run: func(cmd *cobra.Command, args []string) {
// If the path is started with http , assume it is a URL and download the file.
path := cmd.Flag("path").Value.String()
var err error
if len(path) > 4 && path[:4] == "http" {
method := cmd.Flag("http-method").Value.String()
err = RestoreFromURL(path, method)
} else {
err = RestoreFromFile(path)
}
if err != nil {
PrintError(err.Error())
}
PrintData("Data has been imported")
},
}
var exportCmd = &cobra.Command{
Use: "export",
Run: func(cmd *cobra.Command, args []string) {
path := cmd.Flag("path").Value.String()
var err error
// If the path is started with http , assume it is a URL and upload the file.
if len(path) > 4 && path[:4] == "http" {
method := cmd.Flag("http-method").Value.String()
err = BackupToURL(path, method)
} else {
err = BackupToFile(path)
}
if err != nil {
PrintError(err.Error())
}
PrintData("Data has been exported")
},
}
var sizeCmd = &cobra.Command{
Use: "size",
Run: func(cmd *cobra.Command, args []string) {
size, err := FetchFileSize(pvDirectory)
if err != nil {
size = 0
}
// Write size to /app/size.txt for backward compatibility.
file := "/app/size.txt"
f, err := os.Create(file)
if err != nil {
PrintError(err.Error())
}
defer f.Close()
_, err = f.WriteString(fmt.Sprintf("%d", size))
if err != nil {
PrintError(err.Error())
}
PrintData(size)
},
}
var destroyCmd = &cobra.Command{
Use: "destroy",
Run: func(cmd *cobra.Command, args []string) {
// Destroy the backup.
_ = os.RemoveAll(pvDirectory)
PrintData("All data has been destroyed")
},
}
/*
file-op command
- ls <full_path>
- cat <full_path>
- cp <full_path> <full_path_dest>
- mv <full_path> <full_path_dest>
- rm <full_path> [Do recursive delete anyway]
- mkdir <full_path> [Will create all the directories in the path]
- chmod <mode> <full_path>
- chown <uid> <gid> <full_path>
- download <url> <full_path>
*/
var fileOpCmd = &cobra.Command{
Use: "file-op",
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}
var lsCmd = &cobra.Command{
Use: "ls",
Run: func(cmd *cobra.Command, args []string) {
path := filePath
files, err := ListFiles(path)
if err != nil {
PrintError(err.Error())
}
PrintData(files)
},
}
var catCmd = &cobra.Command{
Use: "cat",
Run: func(cmd *cobra.Command, args []string) {
path := filePath
data, err := os.ReadFile(path)
if err != nil {
PrintError(err.Error())
}
// Stream the bytes to the stdout.
os.Stdout.Write(data)
},
}
var cpCmd = &cobra.Command{
Use: "cp",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
PrintError("Invalid number of arguments")
}
src := filePath
dest := args[0]
// copy file without changing the ownership and permissions.
err := CopyFile(src, dest)
if err != nil {
PrintError(err.Error())
}
PrintData("file has been copied")
},
}
var mvCmd = &cobra.Command{
Use: "mv",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
PrintError("Invalid number of arguments")
}
src := filePath
dest := args[0]
// move file without changing the ownership and permissions.
err := MoveFile(src, dest)
if err != nil {
PrintError(err.Error())
}
PrintData("file has been moved")
},
}
var rmCmd = &cobra.Command{
Use: "rm",
Run: func(cmd *cobra.Command, args []string) {
path := filePath
err := os.RemoveAll(path)
if err != nil {
PrintError(err.Error())
}
PrintData("file has been removed")
},
}
var mkdirCmd = &cobra.Command{
Use: "mkdir",
Run: func(cmd *cobra.Command, args []string) {
path := filePath
err := os.MkdirAll(path, 0777)
if err != nil {
PrintError(err.Error())
}
PrintData("directory has been created")
},
}
var chmodCmd = &cobra.Command{
Use: "chmod",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
PrintError("Invalid number of arguments")
}
mode := args[0] // mode should be in octal format like 777, 755 etc.
perm, err := strconv.ParseUint(mode, 8, 32)
if err != nil {
PrintError(err.Error())
}
path := filePath
err = os.Chmod(path, os.FileMode(perm))
if err != nil {
PrintError(err.Error())
}
PrintData("file permissions have been changed")
},
}
var chownCmd = &cobra.Command{
Use: "chown",
Run: func(cmd *cobra.Command, args []string) {
uid, err := strconv.Atoi(cmd.Flag("uid").Value.String())
if err != nil {
PrintError(err.Error())
}
gid, err := strconv.Atoi(cmd.Flag("gid").Value.String())
if err != nil {
PrintError(err.Error())
}
err = os.Chown(filePath, uid, gid)
if err != nil {
PrintError(err.Error())
}
PrintData("file ownership has been changed")
},
}
var downloadCmd = &cobra.Command{
Use: "download",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
PrintError("Invalid number of arguments")
}
url := args[0]
err := DownloadFile(url, filePath)
if err != nil {
PrintError(err.Error())
}
PrintData("file has been downloaded")
},
}