-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.go
104 lines (91 loc) · 2.7 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
package main
import (
"fmt"
"github.com/docopt/docopt-go"
"github.com/howeyc/gopass"
"golang.org/x/crypto/ssh/terminal"
"os"
"runtime"
"strings"
)
type Params struct {
Url, User, Password, Dir, File string
}
func getParams(defaults Defaults) Params {
/* Fill defaults */
p := Params{}
/* Parse the arguments */
args := docoptArgs(defaults)
/* Fill the parameters */
// Mandatory
p.Url = args["<URL>"].(string)
p.User = args["--user"].(string)
// Mandatory + OR
switch {
case args["--prompt"].(bool):
p.Password = promptForPassword()
default:
p.Password = args["--pass"].(string)
}
// Optional + OR
switch {
case args["--file"] != nil:
p.File = args["--file"].(string)
case args["--dir"] != nil:
p.Dir = args["--dir"].(string)
default:
dir, err := os.Getwd()
if err != nil {
dir = os.TempDir()
}
p.Dir = dir
}
return p
}
func docoptArgs(defaults Defaults) map[string]interface{} {
versionMsg := "shibdl " + defaults.Version + "."
usage := versionMsg + "\n" +
`Download files secured by a Shibboleth IdP.
Code, bugs and feature requests: ` + defaults.Repo + `.
Author: ` + defaults.Author + `.
_ _ _ _ _ _ _ _
_-(_)- _-(_)- _-(_)- _-(")- _-(_)- _-(_)- _-(_)- _-(_)-
*(___) *(___) *(___) *%%%%% *(___) *(___) *(___) *(___)
// \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\
Usage:
shibdl <URL> -u user [-p password | -P ] [-f file | -d directory]
shibdl -h | --help
shibdl -v | --version
Options:
<URL> URL to download
-u <user>, --user <user> Username
-p <password>, --pass <password> Password
-P, --prompt Prompt for password
-d <directory>, --dir <directory> Directory to safe file (optional)
-f <file>, --file <file> Full path of filename (optional)
-h, --help Show this help screen
-v, --version Show the version message
`
args, _ := docopt.Parse(usage, nil, true, versionMsg, false)
return args
}
func promptForPassword() string {
fmt.Print("Enter Password: ")
var password string
var bytePassword []byte
var err error
for {
switch {
case runtime.GOOS == "windows":
bytePassword, err = gopass.GetPasswd()
default:
bytePassword, err = terminal.ReadPassword(0)
}
if err == nil {
password = string(bytePassword)
fmt.Println()
break
}
}
return strings.TrimSpace(password)
}