@@ -38,8 +38,8 @@ const (
38
38
CredentialsFilename = "arduino-cloud-credentials"
39
39
)
40
40
41
- // SetDefaultCredentials sets the default credentials values.
42
- func SetDefaultCredentials (settings * viper.Viper ) {
41
+ // SetEmptyCredentials sets the default credentials values to empty strings .
42
+ func SetEmptyCredentials (settings * viper.Viper ) {
43
43
// Client ID
44
44
settings .SetDefault ("client" , "" )
45
45
// Secret
@@ -79,57 +79,62 @@ func (c *Credentials) IsEmpty() bool {
79
79
80
80
// RetrieveCredentials looks for credentials in
81
81
// environment variables or in credentials file.
82
- // Returns error if no credentials are found.
82
+ // Returns error if no credentials are found or
83
+ // if found credentials are invalid.
83
84
func RetrieveCredentials () (* Credentials , error ) {
84
85
// Credentials extracted from environment has highest priority
85
86
logrus .Info ("Looking for credentials in environment variables" )
86
87
c , err := fromEnv ()
87
88
if err != nil {
88
89
return nil , fmt .Errorf ("reading credentials from environment variables: %w" , err )
89
90
}
90
- // Return credentials only if found
91
- if c != nil {
92
- logrus .Info ("Credentials found in environment variables" )
91
+ // Return credentials if found in env
92
+ if ! c .IsEmpty () {
93
+ // Return error if credentials are found but are not valid
94
+ if err := c .Validate (); err != nil {
95
+ return nil , fmt .Errorf (
96
+ "credentials retrieved from environment variables with prefix '%s' are not valid: %w" , EnvPrefix , err ,
97
+ )
98
+ }
99
+ logrus .Infof ("Credentials found in environment variables with prefix '%s'" , EnvPrefix )
93
100
return c , nil
94
101
}
95
102
96
103
logrus .Info ("Looking for credentials in file system" )
97
- c , err = fromFile ( )
104
+ filepath , found , err := searchConfigDir ( CredentialsFilename )
98
105
if err != nil {
99
- return nil , fmt .Errorf ("reading credentials from file : %w" , err )
106
+ return nil , fmt .Errorf ("can't get credentials directory : %w" , err )
100
107
}
101
- if c != nil {
102
- return c , nil
108
+ if ! found {
109
+ return nil , fmt .Errorf (
110
+ "credentials have not been found neither in environment variables " +
111
+ "nor in the current directory, its parents or in arduino15" ,
112
+ )
103
113
}
104
114
105
- return nil , fmt .Errorf (
106
- "credentials have not been found neither in environment variables " +
107
- "nor in the current directory, its parents or in arduino15" ,
108
- )
109
- }
110
-
111
- // fromFile looks for a credentials file.
112
- // If a credentials file is not found, it returns nil credentials without raising errors.
113
- // If invalid credentials file is found, it returns an error.
114
- func fromFile () (* Credentials , error ) {
115
- // Looks for a credentials file
116
- configDir , err := searchConfigDir (CredentialsFilename )
115
+ c , err = fromFile (filepath )
117
116
if err != nil {
118
- return nil , fmt .Errorf ("can't get credentials directory : %w" , err )
117
+ return nil , fmt .Errorf ("reading credentials from file %s : %w" , filepath , err )
119
118
}
120
- // Return nil credentials if no config file is found
121
- if configDir == nil {
122
- return nil , nil
119
+ // Return error if credentials are not valid
120
+ if err := c .Validate (); err != nil {
121
+ return nil , fmt .Errorf (
122
+ "credentials retrieved from file %s are not valid: %w" , filepath , err ,
123
+ )
123
124
}
125
+ return c , nil
126
+ }
124
127
128
+ // fromFile looks for a credentials file.
129
+ func fromFile (filepath string ) (* Credentials , error ) {
125
130
v := viper .New ()
126
131
v .SetConfigName (CredentialsFilename )
127
- v .AddConfigPath (* configDir )
128
- err = v .ReadInConfig ()
132
+ v .AddConfigPath (filepath )
133
+ err : = v .ReadInConfig ()
129
134
if err != nil {
130
135
err = fmt .Errorf (
131
136
"credentials file found at %s but cannot read its content: %w" ,
132
- * configDir ,
137
+ filepath ,
133
138
err ,
134
139
)
135
140
return nil , err
@@ -140,26 +145,18 @@ func fromFile() (*Credentials, error) {
140
145
if err != nil {
141
146
return nil , fmt .Errorf (
142
147
"credentials file found at %s but cannot unmarshal it: %w" ,
143
- * configDir ,
144
- err ,
145
- )
146
- }
147
- if err = cred .Validate (); err != nil {
148
- return nil , fmt .Errorf (
149
- "credentials file found at %s but is not valid: %w" ,
150
- * configDir ,
148
+ filepath ,
151
149
err ,
152
150
)
153
151
}
154
152
return cred , nil
155
153
}
156
154
157
- // fromEnv looks for credentials in environment variables.
158
- // If credentials are not found, it returns nil credentials without raising errors.
159
- // If invalid credentials are found, it returns an error.
155
+ // fromEnv retrieves credentials from environment variables.
156
+ // Returns empty credentials if not found.
160
157
func fromEnv () (* Credentials , error ) {
161
158
v := viper .New ()
162
- SetDefaultCredentials (v )
159
+ SetEmptyCredentials (v )
163
160
v .SetEnvPrefix (EnvPrefix )
164
161
v .AutomaticEnv ()
165
162
@@ -168,17 +165,5 @@ func fromEnv() (*Credentials, error) {
168
165
if err != nil {
169
166
return nil , fmt .Errorf ("cannot unmarshal credentials from environment variables: %w" , err )
170
167
}
171
-
172
- if cred .IsEmpty () {
173
- return nil , nil
174
- }
175
-
176
- if err = cred .Validate (); err != nil {
177
- return nil , fmt .Errorf (
178
- "credentials retrieved from environment variables with prefix '%s' are not valid: %w" ,
179
- EnvPrefix ,
180
- err ,
181
- )
182
- }
183
168
return cred , nil
184
169
}
0 commit comments