forked from jordan0day/envver
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
143 lines (123 loc) · 4.03 KB
/
main.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
package main
import (
"github.com/openaperture/envver/auth"
"github.com/openaperture/envver/environment"
"fmt"
"os"
"github.com/codegangsta/cli"
)
type Arguments struct {
ClientId string
ClientSecret string
TokenUrl string
ManagerUrl string
ProductName string
EnvironmentName string
}
func main() {
app := cli.NewApp()
app.Name = "Envver"
app.Usage = "Retrieve product environment settings from OpenAperture and echoes them to the console ."
app.Version = "0.0.3"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "id, i",
Usage: "The OAuth Client ID used to talk to the OpenAperture manager.",
EnvVar: "OA_CLIENT_ID",
},
cli.StringFlag{
Name: "secret, s",
Usage: "The OAuth Client Secret used to talk to the OpenAperture manager.",
EnvVar: "OA_CLIENT_SECRET",
},
cli.StringFlag{
Name: "auth_url, a",
Usage: "OAuth token URL used to talk to retreive an OAuth token for the OpenAperture manager.",
EnvVar: "OA_AUTH_TOKEN_URL",
},
cli.StringFlag{
Name: "url, u",
Usage: "Base URL of the OpenAperture Manager service.",
EnvVar: "OA_URL",
},
cli.StringFlag{
Name: "product, p",
Usage: "The OpenAperture Product Name of the product for which you're retrieving environment settings.",
EnvVar: "OA_PRODUCT_NAME",
},
cli.StringFlag{
Name: "environment, e",
Usage: "The OpenAperture Product Environment name for which you're retrieving environment settings.",
EnvVar: "OA_PRODUCT_ENVIRONMENT_NAME",
},
}
app.Action = func(c *cli.Context) {
args := getArguments(c)
if validateArguments(args) != true {
cli.ShowAppHelp(c)
os.Exit(1)
}
fetchVariables(args)
}
app.RunAndExitOnError()
}
func getArguments(c *cli.Context) (*Arguments) {
var args = Arguments{
ClientId: c.GlobalString("id"),
ClientSecret: c.GlobalString("secret"),
TokenUrl: c.GlobalString("auth_url"),
ManagerUrl: c.GlobalString("url"),
ProductName: c.String("product"),
EnvironmentName: c.String("environment"),
}
return &args
}
func validateArguments(a *Arguments) (bool) {
if a == nil {
return false
}
if len(a.ClientId) <= 0 {
fmt.Println("Please specify the OAuth Client ID, either via the --id flag or by setting the 'OA_CLIENT_ID' environmental variable.")
return false
}
if len(a.ClientSecret) <= 0 {
fmt.Println("Please specify the OAuth Client Secret, either via the --secret flag or by setting the 'OA_CLIENT_SECRET' environmental variable.")
return false
}
if len(a.TokenUrl) <= 0 {
fmt.Println("Please specify the OAUth Access Token URL, either via the --auth_url flag or by setting the 'OA_AUTH_TOKEN_URL' environmental variable.")
return false
}
if len(a.ManagerUrl) <= 0 {
fmt.Println("Please specify the URL of the OpenAperture Manager service, either via the --url flag or by setting the 'OA_URL' environment variable.")
}
if len(a.ProductName) <= 0 {
fmt.Println("Please specify the Product Name, either via the --product flag or by setting the 'OA_PRODUCT_NAME' environmental variable.")
return false
}
return true
}
func fetchVariables(args *Arguments) {
var variables []environment.EnvironmentVariable
credentials := auth.ClientCredentials{
ClientId: args.ClientId,
ClientSecret: args.ClientSecret,
}
authResponse, err := auth.GetAuthToken(credentials, args.TokenUrl)
if err != nil {
fmt.Printf("Couldn't retrieve auth token: %v\n", err)
os.Exit(2)
}
if len(args.EnvironmentName) > 0 {
variables, err = environment.GetEnvironmentVariables(args.ProductName, args.EnvironmentName, authResponse.AccessToken, args.ManagerUrl)
} else {
variables, err = environment.GetProductEnvironmentVariables(args.ProductName, authResponse.AccessToken, args.ManagerUrl)
}
if err != nil {
fmt.Printf("Error retrieving variables: %v\n", err)
os.Exit(16)
}
for _, v := range variables {
fmt.Printf("%s=%s\n", v.Name, v.Value)
}
}