@@ -198,43 +198,59 @@ Use "coscale-cli [object] <help>" for more information about a command.
198198`
199199
200200// GetConfigPath is used to return the absolut path of the api configuration file
201- func GetConfigPath () (string , error ) {
202- var carriageReturn string
203- configFile := "/api.conf"
204- dir , err := filepath .Abs (filepath .Dir (os .Args [0 ]))
205- if err != nil {
206- os .Exit (EXIT_FLAG_ERROR )
207- }
208- configPath := dir + configFile
209- if _ , err := os .Stat (configPath ); err == nil {
210- return configPath , nil
211- }
212- var cmdName string
201+ func GetConfigPath () (c string , e error ) {
202+ command := os .Args [0 ]
203+ var configFile , backwardsConfigFile , dir , cmdName , carriageReturn string
204+ var err error
213205 if runtime .GOOS == "windows" {
206+ configFile = "api.conf"
207+ backwardsConfigFile = "api.conf"
214208 cmdName = "where"
215209 carriageReturn = "\r \n "
216210 } else {
211+ configFile = filepath .Join ("etc" , "api.conf" )
212+ backwardsConfigFile = "api.conf" // once we had api.conf in the same folder as cli executable
217213 cmdName = "which"
218214 carriageReturn = "\n "
219215 }
220- response , err := GetCommandOutput (cmdName , 2 * time .Second , os .Args [0 ])
221- path := string (bytes .Split (response , []byte (carriageReturn ))[0 ])
222- if err != nil {
223- return "" , err
216+ // we calculate folder where the command is
217+
218+ // 1. check if command is in PATH
219+ // if we check a command that doesn't exist on linux we get error
220+ response , err := GetCommandOutput (cmdName , 2 * time .Second , command )
221+ if err == nil {
222+ command = string (bytes .Split (response , []byte (carriageReturn ))[0 ])
224223 }
225- // check if is a symlink
226- file , err := os .Lstat (path )
224+ err = nil
225+ // 2. check if it is a symlink
226+ file , err := os .Lstat (command )
227227 if err != nil {
228228 return "" , err
229229 }
230230 if file .Mode ()& os .ModeSymlink == os .ModeSymlink {
231231 // This is a symlink
232- path , err = filepath .EvalSymlinks (path )
232+ command , err = filepath .EvalSymlinks (command )
233233 if err != nil {
234234 return "" , err
235235 }
236236 }
237- return filepath .Dir (path ) + configFile , nil
237+ // check if config file is in dir
238+ dir , err = filepath .Abs (filepath .Dir (command ))
239+ if err != nil {
240+ os .Exit (EXIT_FLAG_ERROR )
241+ }
242+ configPath := filepath .Join (dir , configFile )
243+ _ , err = os .Stat (configPath )
244+ if err == nil {
245+ return configPath , nil
246+ }
247+ // try the backwards compatible approach
248+ configPath = filepath .Join (dir , backwardsConfigFile )
249+ _ , err = os .Stat (configPath )
250+ if err == nil {
251+ return configPath , nil
252+ }
253+ return "" , err
238254}
239255
240256// GetCommandOutput returns stdout of command as a string
0 commit comments