@@ -108,7 +108,10 @@ func setConfigPropertyQuiet(propName, propValue string) {
108
108
// SafeWriteConfig() will not update files but it will create them, combined
109
109
// this code will successfully update files.
110
110
if viperErr := viper .SafeWriteConfig (); viperErr != nil {
111
- _ = viper .WriteConfig ()
111
+ err := viper .WriteConfig ()
112
+ if err != nil {
113
+ fmt .Println ("Error writing config file" , err )
114
+ }
112
115
}
113
116
}
114
117
@@ -117,18 +120,54 @@ func SetConfigProperty(propName, propValue string) {
117
120
setConfigPropertyQuiet (propName , propValue )
118
121
}
119
122
120
- func LoadConfiguration () {
121
- usr , err := user .Current ()
123
+ func LoadConfiguration () error {
124
+ configFilePath := viper .GetString (params .ConfigFilePathKey )
125
+
126
+ if configFilePath != "" {
127
+ err := validateConfigFile (configFilePath )
128
+ if err != nil {
129
+ return err
130
+ }
131
+ viper .SetConfigFile (configFilePath )
132
+ if err = viper .ReadInConfig (); err != nil {
133
+ return errors .New ("An error occurred while accessing the file or environment variable. Please verify the CLI configuration file" )
134
+ }
135
+ } else {
136
+ usr , err := user .Current ()
137
+ if err != nil {
138
+ log .Fatal ("Cannot file home directory." , err )
139
+ }
140
+ fullPath := usr .HomeDir + configDirName
141
+ verifyConfigDir (fullPath )
142
+ viper .AddConfigPath (fullPath )
143
+ configFile := "checkmarxcli"
144
+ viper .SetConfigName (configFile )
145
+ viper .SetConfigType ("yaml" )
146
+ _ = viper .ReadInConfig ()
147
+ }
148
+ return nil
149
+ }
150
+
151
+ func validateConfigFile (configFilePath string ) error {
152
+ info , err := os .Stat (configFilePath )
122
153
if err != nil {
123
- log .Fatal ("Cannot file home directory." , err )
154
+ if os .IsNotExist (err ) {
155
+ return fmt .Errorf ("The specified file does not exist. Please check the path and ensure the CLI configuration file is available." )
156
+ }
157
+ return fmt .Errorf ("An error occurred while accessing the file or environment variable. Please verify the CLI configuration file" )
158
+ }
159
+
160
+ if info .IsDir () {
161
+ return fmt .Errorf ("The specified path points to a directory, not a file. Please provide a valid CLI configuration file path." )
162
+ }
163
+
164
+ file , err := os .OpenFile (configFilePath , os .O_RDONLY , 0644 )
165
+ if err != nil {
166
+ return fmt .Errorf ("Access to the specified file is restricted. Please ensure you have the necessary permissions to access the CLI configuration file" )
124
167
}
125
- fullPath := usr .HomeDir + configDirName
126
- verifyConfigDir (fullPath )
127
- viper .AddConfigPath (fullPath )
128
- configFile := "checkmarxcli"
129
- viper .SetConfigName (configFile )
130
- viper .SetConfigType ("yaml" )
131
- _ = viper .ReadInConfig ()
168
+ defer file .Close ()
169
+
170
+ return nil
132
171
}
133
172
134
173
func SafeWriteSingleConfigKey (configFilePath , key string , value int ) error {
@@ -231,11 +270,16 @@ func SaveConfig(path string, config map[string]interface{}) error {
231
270
}
232
271
233
272
func GetConfigFilePath () (string , error ) {
234
- usr , err := user .Current ()
235
- if err != nil {
236
- return "" , fmt .Errorf ("error getting current user: %w" , err )
273
+ configFilePath := viper .GetString (params .ConfigFilePathKey )
274
+
275
+ if configFilePath == "" {
276
+ usr , err := user .Current ()
277
+ if err != nil {
278
+ return "" , fmt .Errorf ("error getting current user: %w" , err )
279
+ }
280
+ configFilePath = usr .HomeDir + configDirName + "/checkmarxcli.yaml"
237
281
}
238
- return usr . HomeDir + configDirName + "/checkmarxcli.yaml" , nil
282
+ return configFilePath , nil
239
283
}
240
284
241
285
func verifyConfigDir (fullPath string ) {
0 commit comments