1616package inventory
1717
1818import (
19+ "fmt"
1920 "os"
2021 "path/filepath"
2122
22- "github.com/arduino/arduino-cli/cli/feedback"
2323 "github.com/gofrs/uuid"
2424 "github.com/spf13/viper"
2525)
3535)
3636
3737// Init configures the Read Only config storage
38- func Init (configPath string ) {
38+ func Init (configPath string ) error {
3939 configFilePath := filepath .Join (configPath , Name )
4040 Store .SetConfigName (Name )
4141 Store .SetConfigType (Type )
@@ -45,40 +45,49 @@ func Init(configPath string) {
4545 // ConfigFileNotFoundError is acceptable, anything else
4646 // should be reported to the user
4747 if _ , ok := err .(viper.ConfigFileNotFoundError ); ok {
48- generateInstallationData ()
49- writeStore (configFilePath )
48+ if err := generateInstallationData (); err != nil {
49+ return err
50+ }
51+ if err := writeStore (configFilePath ); err != nil {
52+ return err
53+ }
5054 } else {
51- feedback .Errorf ("Error reading inventory file: %v " , err )
55+ return fmt .Errorf ("reading inventory file: %w " , err )
5256 }
5357 }
58+
59+ return nil
5460}
5561
56- func generateInstallationData () {
62+ func generateInstallationData () error {
5763 installationID , err := uuid .NewV4 ()
5864 if err != nil {
59- feedback .Errorf ("Error generating installation.id: %v " , err )
65+ return fmt .Errorf ("generating installation.id: %w " , err )
6066 }
6167 Store .Set ("installation.id" , installationID .String ())
6268
6369 installationSecret , err := uuid .NewV4 ()
6470 if err != nil {
65- feedback .Errorf ("Error generating installation.secret: %v " , err )
71+ return fmt .Errorf ("generating installation.secret: %w " , err )
6672 }
6773 Store .Set ("installation.secret" , installationSecret .String ())
74+ return nil
6875}
6976
70- func writeStore (configFilePath string ) {
77+ func writeStore (configFilePath string ) error {
7178 configPath := filepath .Dir (configFilePath )
7279
7380 // Create config dir if not present,
7481 // MkdirAll will retrun no error if the path already exists
7582 if err := os .MkdirAll (configPath , os .FileMode (0755 )); err != nil {
76- feedback .Errorf ("Error creating inventory dir: %v" , err )
83+ return fmt .Errorf ("invalid path creating config dir: %s error: %w" , configPath , err )
7784 }
7885
7986 // Create file if not present
8087 err := Store .WriteConfigAs (configFilePath )
8188 if err != nil {
82- feedback .Errorf ("Error writing inventory file: %v" , err )
89+ return fmt .Errorf ("invalid path writing inventory file: %s error: %w" , configFilePath , err )
8390 }
91+
92+ return nil
8493}
0 commit comments