-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.go
126 lines (102 loc) · 2.62 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
package scv
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"os/user"
"path/filepath"
"strings"
)
const Help = `
Usage: scv [options] [zone name] [server name]
Options:
-h, --help print help message and exit
--init create $HOME/.scv.json if it does not exist
-v, --verbose print debug log
-V, --version print version and exit
`
var Version string
type CLI struct {
Logger Logger
}
func (cli *CLI) Run(args []string) (result string, err error) {
f := flag.NewFlagSet("scv", flag.ContinueOnError)
var optHelp bool
var optLongHelp bool
f.BoolVar(&optHelp, "h", false, "")
f.BoolVar(&optLongHelp, "help", false, "")
var optInit bool
f.BoolVar(&optInit, "init", false, "")
var optVersion bool
var optLongVersion bool
f.BoolVar(&optVersion, "V", false, "")
f.BoolVar(&optLongVersion, "version", false, "")
var optVerbose bool
var optLongVerbose bool
f.BoolVar(&optVerbose, "v", false, "")
f.BoolVar(&optLongVerbose, "verbose", false, "")
err = f.Parse(args)
if err != nil {
return result, err
}
if optVersion || optLongVersion {
result, err = cli.Versionf(Version)
return result, err
}
if optInit {
current, _ := user.Current()
dir := filepath.Join(current.HomeDir, ".scv.json")
config := &Config{Servers: []Server{}}
result, err = config.CreateFile(dir)
return result, err
}
if optVerbose || optLongVerbose {
cli.Logger.Verbose = true
}
optSize := len(f.Args())
if optHelp || optLongHelp || optSize == 0 {
result = Help
return result, err
}
valid := cli.ValidateOptSize(optSize)
if !valid {
err = fmt.Errorf("Expected arguments size is 2, but given %d", optSize)
return result, err
}
zoneName := f.Args()[0]
serverName := f.Args()[1]
current, _ := user.Current()
dir := filepath.Join(current.HomeDir, ".scv.json")
config := &Config{}
err = config.LoadFile(dir)
if err != nil {
return result, err
}
serverID, err := config.ServerID(zoneName, serverName)
if err != nil {
return result, err
}
cli.Logger.Debug("ServerID: " + serverID)
api := &API{}
url := api.URL(zoneName, serverID)
cli.Logger.Debug("URL: " + url)
body, err := api.GET(url, config.AccessToken, config.AccessTokenSecret)
if err != nil {
return result, err
}
vnc := &VNC{}
json.NewDecoder(bytes.NewReader(body)).Decode(vnc)
result = vnc.Path()
return result, err
}
func (cli *CLI) ValidateOptSize(optSize int) bool {
return optSize == 2
}
func (cli *CLI) Versionf(version string) (string, error) {
a := strings.Split(version, "-")
if len(a) > 2 {
return fmt.Sprintf("scv version %s, build %s", a[0][1:], a[2]), nil
}
return "", fmt.Errorf("version: %s", version)
}