Skip to content

Commit f2baa1e

Browse files
author
Ryan Svihla
committed
logging in with args for classic service account was broken
1 parent 5b0a6f7 commit f2baa1e

File tree

3 files changed

+445
-50
lines changed

3 files changed

+445
-50
lines changed

.golangci.yml

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ linters:
2020
- golint
2121
- gomnd
2222
- goprintffuncname
23-
- gosec
2423
- gosimple
2524
- govet
2625
- ineffassign

cmd/login.go

+49-46
Original file line numberDiff line numberDiff line change
@@ -51,59 +51,62 @@ var loginCmd = &cobra.Command{
5151
Short: "Stores credentials for the cli to use in other commands to operate on the Astra DevOps API",
5252
Long: `Token or service account is saved in .config/astra/ for use by the other commands`,
5353
Run: func(cobraCmd *cobra.Command, args []string) {
54-
confDir, confFiles, err := pkg.GetHome(os.UserHomeDir)
54+
exitCode, err := executeLogin(args, func() (string, pkg.ConfFiles, error) {
55+
return pkg.GetHome(os.UserHomeDir)
56+
})
5557
if err != nil {
5658
fmt.Printf("%v\n", err)
57-
os.Exit(CannotFindHome)
5859
}
59-
if authToken != "" {
60-
if err := makeConf(confDir, confFiles.TokenPath, authToken); err != nil {
61-
fmt.Printf("%v\n", err)
62-
os.Exit(WriteError)
63-
}
64-
return
65-
} else if clientJSON != "" {
66-
var clientInfo astraops.ClientInfo
67-
err := json.Unmarshal([]byte(clientJSON), &clientInfo)
68-
if err != nil {
69-
fmt.Fprintln(os.Stderr, fmt.Errorf("unable to serialize the json into a valid login due to error %s", err))
70-
os.Exit(JSONError)
71-
}
72-
if len(clientInfo.ClientName) == 0 {
73-
fmt.Fprintln(os.Stderr, pkg.ParseError{
74-
Args: args,
75-
Err: fmt.Errorf("clientName missing"),
76-
})
77-
os.Exit(JSONError)
78-
}
79-
if len(clientInfo.ClientID) == 0 {
80-
fmt.Fprintln(os.Stderr, pkg.ParseError{
81-
Args: args,
82-
Err: fmt.Errorf("clientId missing"),
83-
})
84-
os.Exit(JSONError)
85-
}
86-
if len(clientInfo.ClientSecret) == 0 {
87-
fmt.Fprintln(os.Stderr, pkg.ParseError{
88-
Args: args,
89-
Err: fmt.Errorf("clientSecret missing"),
90-
})
91-
os.Exit(JSONError)
60+
os.Exit(exitCode)
61+
},
62+
}
63+
64+
func executeLogin(args []string, getHome func() (string, pkg.ConfFiles, error)) (int, error) {
65+
confDir, confFiles, err := getHome()
66+
if err != nil {
67+
return CannotFindHome, err
68+
}
69+
switch {
70+
case authToken != "":
71+
if err := makeConf(confDir, confFiles.TokenPath, authToken); err != nil {
72+
return WriteError, err
73+
}
74+
return 0, nil
75+
case clientJSON != "":
76+
var clientInfo astraops.ClientInfo
77+
err := json.Unmarshal([]byte(clientJSON), &clientInfo)
78+
if err != nil {
79+
return JSONError, fmt.Errorf("unable to serialize the json into a valid login due to error %s", err)
80+
}
81+
if len(clientInfo.ClientName) == 0 {
82+
return JSONError, &pkg.ParseError{
83+
Args: args,
84+
Err: fmt.Errorf("clientName missing"),
9285
}
93-
if err := makeConf(confDir, confFiles.SaPath, clientJSON); err != nil {
94-
fmt.Printf("%v\n", err)
95-
os.Exit(WriteError)
86+
}
87+
if len(clientInfo.ClientID) == 0 {
88+
return JSONError, &pkg.ParseError{
89+
Args: args,
90+
Err: fmt.Errorf("clientId missing"),
9691
}
97-
return
98-
} else {
99-
clientJSON = fmt.Sprintf("{\"clientId\":\"%v\",\"clientName\":\"%v\",\"clientSecret\":\"%v:\"}", clientID, clientName, clientSecret)
100-
if err := makeConf(confDir, confFiles.SaPath, clientJSON); err != nil {
101-
fmt.Printf("%v\n", err)
102-
os.Exit(JSONError)
92+
}
93+
if len(clientInfo.ClientSecret) == 0 {
94+
return JSONError, &pkg.ParseError{
95+
Args: args,
96+
Err: fmt.Errorf("clientSecret missing"),
10397
}
104-
return
10598
}
106-
},
99+
if err := makeConf(confDir, confFiles.SaPath, clientJSON); err != nil {
100+
return WriteError, err
101+
}
102+
return 0, nil
103+
default:
104+
clientJSON = fmt.Sprintf("{\"clientId\":\"%v\",\"clientName\":\"%v\",\"clientSecret\":\"%v\"}", clientID, clientName, clientSecret)
105+
if err := makeConf(confDir, confFiles.SaPath, clientJSON); err != nil {
106+
return WriteError, err
107+
}
108+
return 0, nil
109+
}
107110
}
108111

109112
func makeConf(confDir, confFile, content string) error {

0 commit comments

Comments
 (0)