@@ -51,59 +51,62 @@ var loginCmd = &cobra.Command{
51
51
Short : "Stores credentials for the cli to use in other commands to operate on the Astra DevOps API" ,
52
52
Long : `Token or service account is saved in .config/astra/ for use by the other commands` ,
53
53
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
+ })
55
57
if err != nil {
56
58
fmt .Printf ("%v\n " , err )
57
- os .Exit (CannotFindHome )
58
59
}
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" ),
92
85
}
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" ),
96
91
}
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" ),
103
97
}
104
- return
105
98
}
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
+ }
107
110
}
108
111
109
112
func makeConf (confDir , confFile , content string ) error {
0 commit comments