-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
198 lines (185 loc) · 5.21 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
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
package main
import (
"bytes"
"encoding/json"
"errors"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
)
type configProperties struct {
ServerAdress string
JWT string
}
type runtimeProperties struct {
config configProperties
action string
userActionProperties userActionProperties
}
type userActionProperties struct {
userName string
userPassword string
}
func containsErrorCode(a string, list []error) bool {
for _, b := range list {
if b.Error() == a {
return true
}
}
return false
}
func exitWithError(err string) {
fmt.Println(err)
os.Exit(1)
}
func readConfiguration() configProperties {
open, err := ioutil.ReadFile("config.json")
// Default configuration values
unmarshaled := configProperties{
ServerAdress: "http://127.0.0.1:5000",
}
// If the configuration file doesn't exist, return the default values
if os.IsNotExist(err) {
return unmarshaled
}
if err != nil {
panic(err)
}
err = json.Unmarshal(open, &unmarshaled)
if err != nil {
panic(err)
}
return unmarshaled
}
func getProperties() runtimeProperties {
readConfig := readConfiguration()
// Values passed via command line args take precedence over values passed via config.json
serverAdress := flag.String("s", readConfig.ServerAdress, "Specify the server adress")
jwt := flag.String("j", readConfig.JWT, "Specify the bearer token")
action := flag.String("a", "checkConnection", "Action (login, register, authInfo, checkConnection)")
userName := flag.String("u", "", "Username (for registration / login)")
userPassword := flag.String("p", "", "Password (for registration / login)")
flag.Parse()
// Check whether the given combination of arguments is valid
*action = strings.ToLower(*action)
actionIsValid := false
for _, v := range []string{"login", "register", "checkConnection", "authinfo"} {
if *action == v {
actionIsValid = true
}
}
if !actionIsValid {
exitWithError("Unknown action")
}
if *action == "login" || *action == "register" {
if *userName == "" || *userPassword == "" {
exitWithError("Please set a username and a password")
}
} else if *jwt == "" {
exitWithError("Please set a jwt using the config.json or the command line argument -j")
}
// Return the valid conclusion
return runtimeProperties{
config: configProperties{
ServerAdress: *serverAdress,
JWT: *jwt,
},
action: *action,
userActionProperties: userActionProperties{
userName: *userName,
userPassword: *userPassword,
},
}
}
type erroredReturnStruct struct {
Error bool `json:"error"`
Errors []string `json:"errors"`
}
func sendAPIRequest(serverAdress string, apiCall string, request interface{}, method string, jwt *string) ([]byte, []error) {
var requestToSend *bytes.Buffer = new(bytes.Buffer)
if request != nil {
marshaledRequest, err := json.Marshal(request)
if err != nil {
panic(err)
}
requestToSend = bytes.NewBuffer(marshaledRequest)
}
req, err := http.NewRequest(method, fmt.Sprintf("%v/%v", serverAdress, apiCall), requestToSend)
if jwt != nil {
req.Header.Add("Authorization", fmt.Sprintf("Bearer JWT %v", *jwt))
}
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, []error{errors.New("Connection failed")}
}
defer res.Body.Close()
response, err := ioutil.ReadAll(res.Body)
if err != nil {
panic(err)
}
if res.StatusCode != 200 {
var errorStruct erroredReturnStruct
err = json.Unmarshal(response, &errorStruct)
if err != nil {
panic(err)
}
var errorList []error
for _, v := range errorStruct.Errors {
errorList = append(errorList, errors.New(v))
}
return response, errorList
}
return response, nil
}
func writeToConfiguration(properties configProperties) {
marshaled, err := json.MarshalIndent(properties, "", " ")
if err != nil {
panic(err)
}
err = ioutil.WriteFile("config.json", marshaled, 0660)
if err != nil {
panic(err)
}
}
func main() {
properties := getProperties()
// Check whether the management api is reachable
isReachable, err := http.Get(fmt.Sprintf("%v/v1/reachable", properties.config.ServerAdress))
if err != nil || isReachable.StatusCode != 200 {
panic(fmt.Sprintf("Management API not reachable: %v", err))
}
defer isReachable.Body.Close()
switch properties.action {
case "checkConnection":
{
fmt.Println("Connection successful")
}
case "login":
{
loggedIn := loginUser(properties.config.ServerAdress, properties.userActionProperties.userName, properties.userActionProperties.userPassword)
properties.config.JWT = loggedIn.Jwt
writeToConfiguration(properties.config)
fmt.Printf(
"Logged in as %v (%v)\n"+
"JWT: %v \n"+
"SessionToken: %v\n"+
"The login expires on %v\n"+
"(login saved to config.json)\n",
loggedIn.UserName, loggedIn.UserID, loggedIn.Jwt, loggedIn.SessionToken, loggedIn.ValidUntil,
)
}
case "register":
{
createdUserName := registerUser(properties.config.ServerAdress, properties.userActionProperties.userName, properties.userActionProperties.userPassword)
fmt.Printf("Created user with name %v\n", createdUserName)
}
case "authinfo":
{
authInfo := getAuthInfo(properties.config.ServerAdress, properties.config.JWT)
fmt.Printf("Logged in as %v (%v)\n", authInfo.Username, authInfo.ID)
}
}
}