-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
133 lines (109 loc) · 2.26 KB
/
command.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
package main
import (
"encoding/json"
"log"
"os"
"github.com/urfave/cli"
"github.com/thirdmartini/go-synology"
)
const (
FormatHuman = "human"
FormatJSON = "json"
)
var (
addressFlag = cli.StringFlag{
Name: "address",
Value: "",
Usage: "Address of the Synology NAS",
}
userFlag = cli.StringFlag{
Name: "auth.user",
Value: "",
Usage: "Username to login",
}
passwordFlag = cli.StringFlag{
Name: "auth.password",
Value: "",
Usage: "Password",
}
debugEnabledFlag = cli.BoolFlag{
Name: "debug.enabled",
Usage: "Enable debug logging",
}
pathFlag = cli.StringFlag{
Name: "path",
Value: "",
Usage: "Path to list",
}
formatFlag = cli.StringFlag{
Name: "format",
Value: "human",
Usage: "Output display format (json|human)",
}
destFlag = cli.StringFlag{
Name: "dest",
Value: "",
Usage: "Destination path",
}
)
var globalFlags = []cli.Flag{
addressFlag,
userFlag,
passwordFlag,
debugEnabledFlag,
}
// Commands are the top-level commands for rachio-cli
var commands = []cli.Command{
{
Name: "api",
ShortName: "api",
Usage: "API info Commands",
Description: "API info Commands",
Subcommands: apiCommands,
},
{
Name: "filestation",
ShortName: "fs",
Usage: "FileStation commands",
Description: "FileStation commands",
Subcommands: fileStationCommands,
},
}
func mustGetSyno(ctx *cli.Context) *synology.Synology {
addr := ctx.GlobalString(addressFlag.Name)
user := ctx.GlobalString(userFlag.Name)
pass := ctx.GlobalString(passwordFlag.Name)
if addr == "" {
log.Panic("--address required. (EI: https://10.0.0.10:5001)")
}
if user == "" {
log.Panic("--auth.user required")
}
if pass == "" {
log.Panic("--auth.password required")
}
syno, err := synology.Login(addr, user, pass)
if err != nil {
log.Panic(err)
}
if ctx.GlobalBool(debugEnabledFlag.Name) {
logger := &log.Logger{}
logger.SetOutput(os.Stdout)
syno.WithLogger(logger)
}
return syno
}
func mustGetDisplayFormat(ctx *cli.Context) string {
switch ctx.GlobalString(formatFlag.Name) {
case "json":
return "json"
case "human":
return "human"
}
return "human"
}
func prettyPrintJSON(obj interface{}) {
marsh := json.NewEncoder(os.Stdout)
marsh.SetIndent("", "\t")
marsh.Encode(obj)
}