1414package utils
1515
1616import (
17- "errors"
1817 "fmt"
1918 "os"
2019 "path/filepath"
2120 "sync"
2221
2322 "github.com/goharbor/go-client/pkg/sdk/v2.0/models"
23+ "github.com/goharbor/harbor-cli/pkg/errors"
2424 log "github.com/sirupsen/logrus"
2525
2626 "github.com/spf13/viper"
@@ -75,7 +75,7 @@ func InitConfig(cfgFile string, userSpecifiedConfig bool) {
7575
7676 // Ensure data directory exists
7777 if err := os .MkdirAll (harborDataDir , os .ModePerm ); err != nil {
78- configInitError = fmt . Errorf ( "failed to create data directory: %w" , err )
78+ configInitError = errors . NewWithCause ( err , "failed to create data directory" )
7979 log .Fatalf ("%v" , configInitError )
8080 }
8181
@@ -100,7 +100,7 @@ func InitConfig(cfgFile string, userSpecifiedConfig bool) {
100100
101101 var harborConfig HarborConfig
102102 if err := viper .Unmarshal (& harborConfig ); err != nil {
103- configInitError = fmt . Errorf ( "failed to unmarshal config file: %w" , err )
103+ configInitError = errors . NewWithCause ( err , "failed to unmarshal config file" )
104104 log .Fatalf ("%v" , configInitError )
105105 }
106106
@@ -135,7 +135,7 @@ func DetermineConfigPath(cfgFile string, userSpecifiedConfig bool) (string, erro
135135 if userSpecifiedConfig && cfgFile != "" {
136136 harborConfigPath , err = filepath .Abs (cfgFile )
137137 if err != nil {
138- return "" , fmt . Errorf ( "failed to resolve absolute path for config file: %w" , err )
138+ return "" , errors . NewWithCause ( err , "failed to resolve absolute path for config file" )
139139 }
140140 return harborConfigPath , nil
141141 }
@@ -144,7 +144,7 @@ func DetermineConfigPath(cfgFile string, userSpecifiedConfig bool) (string, erro
144144 if harborEnvVar != "" {
145145 harborConfigPath , err = filepath .Abs (harborEnvVar )
146146 if err != nil {
147- return "" , fmt . Errorf ( "failed to resolve absolute path for config file from HARBOR_CLI_CONFIG: %w" , err )
147+ return "" , errors . NewWithCause ( err , "failed to resolve absolute path for config file from HARBOR_CLI_CONFIG" )
148148 }
149149 return harborConfigPath , nil
150150 }
@@ -154,13 +154,13 @@ func DetermineConfigPath(cfgFile string, userSpecifiedConfig bool) (string, erro
154154 if xdgConfigHome == "" {
155155 home , err := os .UserHomeDir ()
156156 if err != nil {
157- return "" , fmt . Errorf ( "unable to determine user home directory: %w" , err )
157+ return "" , errors . NewWithCause ( err , "unable to determine user home directory" )
158158 }
159159 xdgConfigHome = filepath .Join (home , ".config" )
160160 }
161161 harborConfigPath , err = filepath .Abs (filepath .Join (xdgConfigHome , "harbor-cli" , "config.yaml" ))
162162 if err != nil {
163- return "" , fmt . Errorf ( "failed to resolve absolute path for default config file: %w" , err )
163+ return "" , errors . NewWithCause ( err , "failed to resolve absolute path for default config file" )
164164 }
165165 return harborConfigPath , nil
166166}
@@ -170,22 +170,22 @@ func EnsureConfigFileExists(harborConfigPath string) error {
170170 // Ensure parent directory exists
171171 configDir := filepath .Dir (harborConfigPath )
172172 if err := os .MkdirAll (configDir , os .ModePerm ); err != nil {
173- return fmt . Errorf ( "failed to create config directory: %w" , err )
173+ return errors . NewWithCause ( err , "failed to create config directory" )
174174 }
175175
176176 if fileInfo , err := os .Stat (harborConfigPath ); err != nil {
177177 // No need to throw error if config file does not exist
178178 if ! os .IsNotExist (err ) {
179- return fmt . Errorf ( "error while checking config file: %w" , err )
179+ return errors . NewWithCause ( err , "error while checking config file" )
180180 }
181181 } else if fileInfo .IsDir () {
182- return fmt . Errorf ("expected a file but found a directory at path: %s" , harborConfigPath )
182+ return errors . Newf ("expected a file but found a directory at path: %s" , harborConfigPath )
183183 }
184184
185185 // Create config file if it doesn't exist
186186 if _ , err := os .Stat (harborConfigPath ); os .IsNotExist (err ) {
187187 if err := CreateConfigFile (harborConfigPath ); err != nil {
188- return fmt . Errorf ( "failed to create config file: %w" , err )
188+ return errors . NewWithCause ( err , "failed to create config file" )
189189 }
190190 }
191191 return nil
@@ -196,7 +196,7 @@ func ReadConfig(harborConfigPath string) error {
196196 viper .SetConfigFile (harborConfigPath )
197197 viper .SetConfigType ("yaml" )
198198 if err := viper .ReadInConfig (); err != nil {
199- return fmt . Errorf ( "error reading config file: %w. Please ensure the config file exists" , err )
199+ return errors . NewWithCause ( err , "error reading config file" , " Please ensure the config file exists" )
200200 }
201201 return nil
202202}
@@ -207,7 +207,7 @@ func GetCurrentHarborConfig() (*HarborConfig, error) {
207207 })
208208
209209 if configInitError != nil {
210- return nil , fmt . Errorf ( "initialization error: %w" , configInitError )
210+ return nil , errors . NewWithCause ( configInitError , "initialization error" )
211211 }
212212
213213 configMutex .RLock ()
@@ -267,7 +267,7 @@ func GetCurrentHarborData() (*HarborData, error) {
267267 })
268268
269269 if configInitError != nil {
270- return nil , fmt . Errorf ( "initialization error: %w" , configInitError )
270+ return nil , errors . NewWithCause ( configInitError , "initialization error" )
271271 }
272272
273273 configMutex .RLock ()
@@ -320,7 +320,7 @@ func ReadDataFile(dataFilePath string) (HarborData, error) {
320320 v .SetConfigFile (dataFilePath )
321321
322322 if err := v .ReadInConfig (); err != nil {
323- return dataFile , fmt . Errorf ( "failed to read data file: %v" , err )
323+ return dataFile , errors . NewWithCause ( err , "failed to read data file" )
324324 }
325325
326326 dataFile .ConfigPath = v .GetString ("configPath" )
@@ -334,19 +334,19 @@ func ApplyDataFile(harborDataPath, harborConfigPath string) error {
334334 if err == nil {
335335 if dataFileContent .ConfigPath != harborConfigPath {
336336 if err := UpdateDataFile (harborDataPath , harborConfigPath ); err != nil {
337- return fmt . Errorf ( "failed to update data file: %w" , err )
337+ return errors . NewWithCause ( err , "failed to update data file" )
338338 }
339339 } else if dataFileContent .ConfigPath == "" {
340340 if err := CreateDataFile (harborDataPath , harborConfigPath ); err != nil {
341- return fmt . Errorf ( "failed to create data file: %w" , err )
341+ return errors . NewWithCause ( err , "failed to create data file" )
342342 }
343343 } else {
344344 log .Debugf ("Data file already exists with the same config path: %s" , harborConfigPath )
345345 }
346346 } else {
347347 // Data file does not exist, create it
348348 if err := CreateDataFile (harborDataPath , harborConfigPath ); err != nil {
349- return fmt . Errorf ( "failed to create data file: %w" , err )
349+ return errors . NewWithCause ( err , "failed to create data file" )
350350 }
351351 }
352352 return nil
@@ -459,7 +459,7 @@ func UpdateConfigFile(config *HarborConfig) error {
459459func GetCredentials (credentialName string ) (Credential , error ) {
460460 currentConfig , err := GetCurrentHarborConfig ()
461461 if err != nil {
462- return Credential {}, fmt . Errorf ( "failed to get current Harbor configuration: %w" , err )
462+ return Credential {}, errors . AsError ( err ). WithMessage ( "failed to get current Harbor configuration" )
463463 }
464464
465465 if currentConfig == nil || currentConfig .Credentials == nil {
@@ -472,7 +472,7 @@ func GetCredentials(credentialName string) (Credential, error) {
472472 }
473473 }
474474
475- return Credential {}, fmt . Errorf ("credential with name '%s' not found" , credentialName )
475+ return Credential {}, errors . Newf ("credential with name '%s' not found" , credentialName )
476476}
477477
478478func AddCredentialsToConfigFile (credential Credential , configPath string ) error {
0 commit comments