Skip to content

Commit 328ce98

Browse files
committed
Added short flags to CLI #289
1 parent 5aa57c9 commit 328ce98

File tree

1 file changed

+55
-21
lines changed

1 file changed

+55
-21
lines changed

cmd/cli-uploader/cliflags/cliflags.go

Lines changed: 55 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func Init(dockerConfigPath, dockerUploadPath string) {
3434

3535
func Parse() int {
3636
if len(os.Args) < 2 {
37-
printUsage()
37+
printUsage(3)
3838
return ModeInvalid
3939
}
4040
switch os.Args[1] {
@@ -44,40 +44,58 @@ func Parse() int {
4444
return ModeLogout
4545
case "upload":
4646
return ModeUpload
47+
case "help":
48+
printUsage(0)
4749
default:
48-
printUsage()
49-
return ModeInvalid
50+
printUsage(3)
5051
}
52+
return ModeInvalid
5153
}
5254

5355
func GetUploadParameters() UploadConfig {
5456
result := UploadConfig{}
5557
for i := 2; i < len(os.Args); i++ {
5658
switch os.Args[i] {
59+
case "-j":
60+
fallthrough
5761
case "--json":
5862
result.JsonOutput = true
63+
case "-n":
64+
fallthrough
5965
case "--disable-e2e":
6066
result.DisableE2e = true
6167
case "-f":
68+
fallthrough
69+
case "--file":
6270
result.File = getParameter(&i)
71+
case "-e":
72+
fallthrough
6373
case "--expiry-days":
6474
result.ExpiryDays = requireInt(getParameter(&i))
75+
case "-d":
76+
fallthrough
6577
case "--expiry-downloads":
6678
result.ExpiryDownloads = requireInt(getParameter(&i))
79+
case "-p":
80+
fallthrough
6781
case "--password":
6882
result.Password = getParameter(&i)
83+
case "-h":
84+
fallthrough
85+
case "--help":
86+
printUsage(0)
6987
}
7088
}
7189
if result.File == "" {
7290
if environment.IsDockerInstance() {
7391
ok, dockerFile := getDockerUpload()
7492
if !ok {
75-
fmt.Println("ERROR: Missing parameter -f and no file or more than one file found in " + dockerUploadFolder)
93+
fmt.Println("ERROR: Missing parameter --file and no file or more than one file found in " + dockerUploadFolder)
7694
os.Exit(2)
7795
}
7896
result.File = dockerFile
7997
} else {
80-
fmt.Println("ERROR: Missing parameter -f")
98+
fmt.Println("ERROR: Missing parameter --file")
8199
os.Exit(2)
82100
}
83101
}
@@ -124,6 +142,8 @@ func GetConfigLocation() string {
124142
for i := 2; i < len(os.Args); i++ {
125143
switch os.Args[i] {
126144
case "-c":
145+
fallthrough
146+
case "--configuration":
127147
return getParameter(&i)
128148
}
129149
}
@@ -134,8 +154,7 @@ func getParameter(position *int) string {
134154
newPosition := *position + 1
135155
position = &newPosition
136156
if newPosition >= len(os.Args) {
137-
printUsage()
138-
os.Exit(3)
157+
printUsage(3)
139158
}
140159
return os.Args[newPosition]
141160
}
@@ -149,21 +168,36 @@ func requireInt(input string) int {
149168
return result
150169
}
151170

152-
func printUsage() {
171+
func printUsage(exitCode int) {
153172
fmt.Println("Gokapi CLI v1.0")
154173
fmt.Println()
155-
fmt.Println("Valid options are:")
156-
fmt.Println(" gokapi-cli login [-c /path/to/config]")
157-
fmt.Println(" gokapi-cli logout [-c /path/to/config]")
158-
fmt.Println(" gokapi-cli upload --f /file/to/upload [--json] [--disable-e2e]\n" +
159-
" [--expiry-days INT] [--expiry-downloads INT]\n" +
160-
" [--password STRING] [-c /path/to/config]")
174+
fmt.Println("Usage:")
175+
fmt.Println(" gokapi-cli [command] [options]")
176+
fmt.Println()
177+
178+
fmt.Println("Commands:")
179+
fmt.Println(" login Save login credentials")
180+
fmt.Println(" upload Upload a file to Gokapi instance")
181+
fmt.Println(" logout Delete login credentials")
182+
fmt.Println()
183+
184+
fmt.Println("Options:")
185+
fmt.Println(" -f, --file <path> File to upload")
186+
if !environment.IsDockerInstance() {
187+
fmt.Println(" -c, --configuration <path> Path to configuration file (default: gokapi-cli.json)")
188+
}
189+
fmt.Println(" -j, --json Output the result in JSON only")
190+
fmt.Println(" -n, --disable-e2e Disable end-to-end encryption")
191+
fmt.Println(" -e, --expiry-days <int> Set file expiry in days (default: unlimited)")
192+
fmt.Println(" -d, --expiry-downloads <int> Set max allowed downloads (default: unlimited)")
193+
fmt.Println(" -p, --password <string> Set a password for the file")
194+
fmt.Println(" -h, --help Show this help message")
195+
fmt.Println()
196+
197+
fmt.Println("Examples:")
198+
fmt.Println(" gokapi-cli login")
199+
fmt.Println(" gokapi-cli logout -c /path/to/config")
200+
fmt.Println(" gokapi-cli upload -f /file/to/upload --expiry-days 7 --json")
161201
fmt.Println()
162-
fmt.Println("gokapi-cli upload:")
163-
fmt.Println("--json Outputs the result as JSON only")
164-
fmt.Println("--disable-e2e Disables end-to-end encryption")
165-
fmt.Println("--expiry-days Sets the expiry date of the file in days, otherwise unlimited")
166-
fmt.Println("--expiry-downloads Sets the allowed downloads, otherwise unlimited")
167-
fmt.Println("--password Sets a password")
168-
os.Exit(3)
202+
os.Exit(exitCode)
169203
}

0 commit comments

Comments
 (0)