forked from apiheat/akamai-cli-netstorage
/
main.go
198 lines (178 loc) · 4.08 KB
/
main.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
package main
import (
"encoding/xml"
"os"
"sort"
"github.com/fatih/color"
"github.com/mitchellh/go-homedir"
"github.com/urfave/cli"
)
// StatNS output structure for stat command
type StatNS struct {
XMLName xml.Name `xml:"stat"`
Directory string `xml:"directory,attr"`
Files []FileNS `xml:"file"`
}
// FileNS output structure for file stat
type FileNS struct {
XMLName xml.Name `xml:"file"`
Type string `xml:"type,attr"`
Name string `xml:"name,attr"`
Mtime string `xml:"mtime,attr"`
Size string `xml:"size,attr"`
MD5 string `xml:"md5,attr"`
}
// duNS output structure for du command
type duNS struct {
XMLName xml.Name `xml:"du"`
Directory string `xml:"directory,attr"`
Info duInfoNS `xml:"du-info"`
}
// duInfoNS output structure for DU info
type duInfoNS struct {
XMLName xml.Name `xml:"du-info"`
Files string `xml:"files,attr"`
Bytes string `xml:"bytes,attr"`
}
var (
version string
configSection, configFile, configCpcode, appName string
nsHostname, nsKeyname, nsKey, nsCpcode, nsPath string
colorOn bool
)
// VERSION
const (
padding = 3
)
func main() {
_, inCLI := os.LookupEnv("AKAMAI_CLI")
appName := "akamai-netstorage"
if inCLI {
appName = "akamai netstorage"
}
app := cli.NewApp()
app.Name = appName
app.HelpName = appName
app.Usage = "A CLI to interact with Akamai NetStorage"
app.Version = version
app.Copyright = ""
app.Authors = []cli.Author{
{
Name: "Petr Artamonov",
},
{
Name: "Rafal Pieniazek",
},
}
dir, _ := homedir.Dir()
dir += string(os.PathSeparator) + ".edgerc"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "section, s",
Value: "netstorage",
Usage: "`NAME` of section to use from credentials file",
Destination: &configSection,
EnvVar: "AKAMAI_EDGERC_NETSTORAGE_SECTION",
},
cli.StringFlag{
Name: "cpcode",
Value: "",
Usage: "`CP CODE` to use",
Destination: &configCpcode,
},
cli.StringFlag{
Name: "config, c",
Value: dir,
Usage: "Location of the credentials `FILE`",
Destination: &configFile,
EnvVar: "AKAMAI_EDGERC",
},
cli.BoolFlag{
Name: "no-color",
Usage: "Disable color output",
Destination: &colorOn,
},
}
app.Commands = []cli.Command{
{
Name: "put",
Usage: "Upload files from `DIRECTORY`",
ArgsUsage: "--from /local/path [DIR]",
Flags: []cli.Flag{
cli.StringFlag{
Name: "from",
Value: "",
Usage: "Upload files from `DIRECTORY`",
},
},
Action: cmdPut,
},
{
Name: "get",
Aliases: []string{"g"},
Usage: "Download from `OBJECT`",
ArgsUsage: "--to /local/path [OBJECT]",
Flags: []cli.Flag{
cli.StringFlag{
Name: "to",
Value: "",
Usage: "Download files to `DIRECTORY`",
},
},
Action: cmdGet,
},
{
Name: "rm",
Aliases: []string{"delete"},
Usage: "Delete `FILE`",
Action: cmdRm,
},
{
Name: "empty-directory",
Aliases: []string{"e"},
Usage: "Erase all files from `DIRECTORY`, non empty directories inside target `DIRECTORY` will be ignored",
Action: cmdErase,
},
{
Name: "list",
Aliases: []string{"ls"},
Usage: "List `OBJECT` content in NetStorage",
Action: cmdList,
},
{
Name: "mkdir",
Aliases: []string{"md"},
Usage: "Create `DIRECTORY` recursively",
Action: cmdMkdir,
},
{
Name: "rmdir",
Usage: "Delete `DIRECTORY`",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "recursively",
Usage: "Delete `DIRECTORY` recursively",
},
},
Action: cmdRmdir,
},
{
Name: "du",
Usage: "Show disk usage of `DIRECTORY`",
Action: cmdDu,
},
}
sort.Sort(cli.FlagsByName(app.Flags))
sort.Sort(cli.CommandsByName(app.Commands))
app.Before = func(c *cli.Context) error {
config(configFile, configSection)
if c.String("cpcode") != "" {
nsCpcode = c.String("cpcode")
}
if c.Bool("no-color") {
color.NoColor = true
}
return nil
}
app.Run(os.Args)
}