@@ -4,48 +4,16 @@ import (
44 "context"
55 "fmt"
66 "os"
7- "path/filepath"
8- "strings"
97 "sync"
108
11- cfg "github.com/ionos-cloud/ionosctl/v6/internal/config"
129 "github.com/ionos-cloud/ionosctl/v6/internal/constants"
1310 "github.com/ionos-cloud/ionosctl/v6/pkg/die"
14- "github.com/ionos-cloud/sdk-go-bundle/shared"
15- "github.com/ionos-cloud/sdk-go-bundle/shared/fileconfiguration"
1611 "github.com/spf13/viper"
17- "gopkg.in/yaml.v3"
1812)
1913
2014var once sync.Once
2115var instance * Client
2216
23- func retrieveConfigFile () (* fileconfiguration.FileConfig , string , error ) {
24- // 1) --config flag
25- if cfg , path , err := loadFromFlag (); cfg != nil || err != nil {
26- return cfg , path , err
27- }
28-
29- // 2) SDK env var
30- if cfg , path , err := loadFromEnvVar (); cfg != nil || err != nil {
31- return cfg , path , err
32- }
33-
34- // 3) migrate old JSON if applicable
35- if cfg , path , err := loadFromJSONMigration (); cfg != nil || err != nil {
36- return cfg , path , err
37- }
38-
39- // 4) default SDK path
40- if cfg , path , err := loadFromSDKDefault (); cfg != nil || err != nil {
41- return cfg , path , err
42- }
43-
44- // note: if we reach this point, no config file was found
45- // though old CLI behaviour was to return the default config path
46- return nil , viper .GetString (constants .ArgConfig ), nil
47- }
48-
4917func Get () (* Client , error ) {
5018 var getClientErr error
5119
@@ -54,11 +22,12 @@ func Get() (*Client, error) {
5422
5523 once .Do (
5624 func () {
57- config , path , err := retrieveConfigFile ()
25+ src , err := retrieveConfigFile ()
5826 if err != nil {
5927 getClientErr = fmt .Errorf ("failed to retrieve config file: %w" , err )
6028 return
6129 }
30+ config , path := src .Config , src .Path
6231
6332 if instance == nil && os .Getenv (constants .EnvToken ) != "" {
6433 instance = newClient ("" , "" , os .Getenv (constants .EnvToken ), desiredURL )
@@ -166,71 +135,3 @@ func (c *Client) TestCreds() error {
166135func EnforceClient (user , pass , token , hostUrl string ) {
167136 instance = newClient (user , pass , token , hostUrl )
168137}
169-
170- func loadFromFlag () (* fileconfiguration.FileConfig , string , error ) {
171- path := viper .GetString (constants .ArgConfig )
172- if path == "" {
173- return nil , "" , nil
174- }
175- cfg , err := fileconfiguration .New (path )
176- if err != nil && ! strings .Contains (err .Error (), "does not exist" ) {
177- return nil , path , fmt .Errorf ("failed to load config from --config '%s': %w" , path , err )
178- }
179- return cfg , path , nil
180- }
181-
182- func loadFromEnvVar () (* fileconfiguration.FileConfig , string , error ) {
183- path := os .Getenv (shared .IonosFilePathEnvVar )
184- if path == "" {
185- return nil , "" , nil
186- }
187- cfg , err := fileconfiguration .New (path )
188- if err != nil && ! strings .Contains (err .Error (), "does not exist" ) {
189- return nil , path , fmt .Errorf (
190- "failed to load config from env var %s='%s': %w" ,
191- shared .IonosFilePathEnvVar , path , err ,
192- )
193- }
194- return cfg , path , nil
195- }
196-
197- func loadFromJSONMigration () (* fileconfiguration.FileConfig , string , error ) {
198- yamlPath := viper .GetString (constants .ArgConfig )
199- if yamlPath == "" {
200- return nil , "" , nil
201- }
202-
203- jsonPath := filepath .Join (filepath .Dir (yamlPath ), "config.json" )
204- if _ , err := os .Stat (jsonPath ); err != nil {
205- return nil , "" , nil // no JSON to migrate
206- }
207-
208- migrated , err := cfg .MigrateFromJSON (jsonPath )
209- if err != nil {
210- return nil , "" , fmt .Errorf ("failed migrating %s → YAML: %w" , jsonPath , err )
211- }
212- if migrated == nil {
213- return nil , "" , nil
214- }
215-
216- out , _ := yaml .Marshal (migrated )
217- if err := os .WriteFile (yamlPath , out , 0o600 ); err != nil {
218- fmt .Fprintf (os .Stderr ,
219- "Warning: could not write migrated config to %s: %v\n " ,
220- yamlPath , err ,
221- )
222- }
223- return migrated , yamlPath , nil
224- }
225-
226- func loadFromSDKDefault () (* fileconfiguration.FileConfig , string , error ) {
227- defaultPath , err := fileconfiguration .DefaultConfigFileName ()
228- if err != nil {
229- return nil , defaultPath , fmt .Errorf ("failed to get default config path: %w" , err )
230- }
231- cfg , err := fileconfiguration .New (defaultPath )
232- if err != nil && ! strings .Contains (err .Error (), "does not exist" ) {
233- return nil , defaultPath , fmt .Errorf ("failed to load default config '%s': %w" , defaultPath , err )
234- }
235- return cfg , defaultPath , nil
236- }
0 commit comments