@@ -19,11 +19,12 @@ package pkg
19
19
import (
20
20
"encoding/json"
21
21
"fmt"
22
- "github.com/rsds143/astra-devops-sdk-go/astraops"
23
22
"io"
24
23
"os"
25
24
"path"
26
25
"strings"
26
+
27
+ "github.com/rsds143/astra-devops-sdk-go/astraops"
27
28
)
28
29
29
30
//ConfFiles supports both formats of credentials and will say if the token one is present
@@ -56,11 +57,11 @@ func (c ConfFiles) HasToken() (bool, error) {
56
57
57
58
// GetHome returns the configuration directory and file
58
59
// error will return if there is no user home folder
59
- func GetHome () (confDir string , confFiles ConfFiles , err error ) {
60
+ func GetHome (getHome func () ( string , error ) ) (confDir string , confFiles ConfFiles , err error ) {
60
61
var home string
61
- home , err = os . UserHomeDir ()
62
+ home , err = getHome ()
62
63
if err != nil {
63
- return "" , ConfFiles {}, fmt .Errorf ("unable to get user home directory with error %s " , err )
64
+ return "" , ConfFiles {}, fmt .Errorf ("unable to get user home directory with error '%s' " , err )
64
65
}
65
66
confDir = path .Join (home , ".config" , "astra" )
66
67
@@ -78,21 +79,24 @@ func ReadToken(tokenFile string) (string, error) {
78
79
if err != nil {
79
80
return "" , & FileNotFoundError {
80
81
Path : tokenFile ,
81
- Err : fmt .Errorf ("unable to read login file with error %w " , err ),
82
+ Err : fmt .Errorf ("unable to read login file with error '%w' " , err ),
82
83
}
83
84
}
84
85
defer func () {
85
86
if err := f .Close (); err != nil {
86
- fmt .Printf ("warning unable to close %v with error %v " , tokenFile , err )
87
+ fmt .Printf ("warning unable to close %v with error '%v' " , tokenFile , err )
87
88
}
88
89
}()
89
90
b , err := io .ReadAll (f )
90
91
if err != nil {
91
- return "" , fmt .Errorf ("unable to read login file %s with error %w" , tokenFile , err )
92
+ return "" , fmt .Errorf ("unable to read login file '%s' with error '%w'" , tokenFile , err )
93
+ }
94
+ if len (b ) == 0 {
95
+ return "" , fmt .Errorf ("token file '%s' is emtpy" , tokenFile )
92
96
}
93
97
token := strings .Trim (string (b ), "\n " )
94
98
if ! strings .HasPrefix (token , "AstraCS" ) {
95
- return "" , fmt .Errorf ("invalid token in login file %s with error %s " , tokenFile , err )
99
+ return "" , fmt .Errorf ("missing prefix 'AstraCS' in token file '%s' " , tokenFile )
96
100
}
97
101
return token , nil
98
102
}
@@ -123,5 +127,14 @@ func ReadLogin(saJSONFile string) (astraops.ClientInfo, error) {
123
127
Err : fmt .Errorf ("unable to parse json from login file %s with error %s" , saJSONFile , err ),
124
128
}
125
129
}
130
+ if clientInfo .ClientID == "" {
131
+ return astraops.ClientInfo {}, fmt .Errorf ("Invalid service account: Client ID for service account is emtpy for file '%v'" , saJSONFile )
132
+ }
133
+ if clientInfo .ClientName == "" {
134
+ return astraops.ClientInfo {}, fmt .Errorf ("Invalid service account: Client name for service account is emtpy for file '%v'" , saJSONFile )
135
+ }
136
+ if clientInfo .ClientSecret == "" {
137
+ return astraops.ClientInfo {}, fmt .Errorf ("Invalid service account: Client secret for service account is emtpy for file '%v'" , saJSONFile )
138
+ }
126
139
return clientInfo , err
127
140
}
0 commit comments