-
Notifications
You must be signed in to change notification settings - Fork 11
/
ovh.go
203 lines (178 loc) · 4.73 KB
/
ovh.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
199
200
201
202
203
package main
import (
//"flag"
"bufio"
"fmt"
"github.com/toorop/govh"
//"github.com/Toorop/govh/ip"
"os"
"runtime"
"github.com/codegangsta/cli"
"github.com/toqueteos/webbrowser"
"github.com/wsxiaoys/terminal"
//"strings"
)
const (
NL = "\r\n"
TAB = " "
NLTAB = NL + TAB
VERSION = "2.0.1"
)
var (
// ck represents the consumer key
ck string
// region represents API region.
// EU (default) or CA
region string
)
func init() {
// region
region = os.Getenv("OVH_REGION")
// Consumer key
ck = os.Getenv("OVH_CONSUMER_KEY")
// if No ConsumerKey, request one
if len(ck) == 0 {
var r []byte
if runtime.GOOS == "windows" {
fmt.Println(NL, "No consumer key found in environment vars !", NL)
} else {
terminal.Stdout.Clear().Move(0, 0).Color("r").
Print("No consumer key found in environment vars !").Nl().Nl().Reset()
}
for {
fmt.Print("Have you a valid Consumer Key for that app ? (y/n) : ")
r, _, _ = bufio.NewReader(os.Stdin).ReadLine()
if r[0] == 110 || r[0] == 121 {
break
}
}
// Yes
if r[0] == 121 {
fmt.Println("\r\nRun the following command :", NL)
if runtime.GOOS == "windows" {
fmt.Println("SET OVH_CONSUMER_KEY=your_consumer_key", NL)
} else {
fmt.Println("export OVH_CONSUMER_KEY=your_consumer_key", NL)
}
fmt.Println("and restart ovh CLI application.", NL)
os.Exit(0)
}
ck, link, err := govh.AuthGetConsumerKey(getAppKey(region), region)
if err != nil {
dieError(err)
}
fmt.Print("\r\nYour consumer key is : ")
if runtime.GOOS != "windows" {
terminal.Stdout.Color("g").Print(ck).Nl().Reset().Nl()
} else {
fmt.Print(ck)
}
fmt.Println("Now you need to validate it :")
if runtime.GOOS != "windows" {
fmt.Printf("\t- If you have a browser available on this machine it will open to the validation page.\n\t- If not, copy and paste the link below in a browser to validate your key :\r\n\r\n%s\r\n", link)
webbrowser.Open(link)
} else {
fmt.Printf("To do it just copy and paste the link below in a browser and follow instructions on OVH website :\r\n\r\n%s\r\n", link)
}
fmt.Println("\r\nWhen it will be done run the following command :")
if runtime.GOOS == "windows" {
fmt.Printf("SET OVH_CONSUMER_KEY=%s%s%s", ck, NL, NL)
} else {
fmt.Printf("export OVH_CONSUMER_KEY=%s%s%s", ck, NL, NL)
}
fmt.Println("and restart ovh CLI application.")
os.Exit(0)
}
}
func main() {
app := cli.NewApp()
app.Name = "ovh"
app.Usage = "ovh-cli brings OVH services to the command line."
app.Version = VERSION
app.Author = "Stèphane Depierrepont aka Toorop"
app.Email = "[email protected]"
cli.AppHelpTemplate = `NAME:
{{.Name}} - {{.Usage}}
SECTIONS:
{{range .Commands}}{{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Description}}
{{end}}
OPTIONS:
{{range .Flags}}{{.}}
{{end}}
`
cli.CommandHelpTemplate = `
{{.Name}} - {{.Description}}
USAGE:
{{.Usage}}
OPTIONS:
{{range .Flags}}{{.}}
{{end}}
`
cli.SubcommandHelpTemplate = `
{{.Name}} - {{.Usage}}
SUBS:
{{range .Commands}}{{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Description}}
{{end}}
OPTIONS:
{{range .Flags}}{{.}}
{{end}}
`
// New govh client
client := govh.New(getAppKey(region), getAppSecret(region), ck, region)
// default action: help
app.Action = func(c *cli.Context) {
cli.ShowAppHelp(c)
}
// Main getFwCmds
app.Commands = []cli.Command{
{
Name: "me",
Usage: "me subsection",
Description: "Commands about me",
Subcommands: getMeCmds(client),
},
{
Name: "domain",
Usage: "domain subsection",
Description: "Commands about domains",
Subcommands: getDomainCmds(client),
},
{
Name: "ip",
Usage: "IP subsection",
Description: "Commands about IP",
Subcommands: getIPCmds(client),
}, {
Name: "fw",
Usage: "Firewall subsection",
Description: "Commands OVH firewall",
// Subcommands: getFwCmds(client),
}, {
Name: "server",
Usage: "Server subsection",
Description: "Commands about OVH server",
Subcommands: getServerCmds(client),
}, {
Name: "sms",
Usage: "Sms subsection",
Description: "Commands about OVH SMS",
Subcommands: getSmsCmds(client),
}, {
Name: "spam",
Usage: "Spam subsection",
Description: "Commands about OVH antispam protection",
Subcommands: getSpamCmds(client),
}, {
Name: "cloud",
Usage: "Cloud subsection",
Description: "Commands about OVH cloud",
Subcommands: getCloudCmds(client),
}, {
Name: "dedicatedcloud",
Usage: "Dedicated Cloud subsection",
Description: "Commands about OVH Dedicated Cloud",
Subcommands: getDedicatedCloudCmds(client),
},
}
app.Run(os.Args)
}