16
16
package inventory
17
17
18
18
import (
19
+ "fmt"
19
20
"os"
20
21
"path/filepath"
21
22
22
- "github.com/arduino/arduino-cli/cli/feedback"
23
23
"github.com/gofrs/uuid"
24
24
"github.com/spf13/viper"
25
25
)
35
35
)
36
36
37
37
// Init configures the Read Only config storage
38
- func Init (configPath string ) {
38
+ func Init (configPath string ) error {
39
39
configFilePath := filepath .Join (configPath , Name )
40
40
Store .SetConfigName (Name )
41
41
Store .SetConfigType (Type )
@@ -45,40 +45,49 @@ func Init(configPath string) {
45
45
// ConfigFileNotFoundError is acceptable, anything else
46
46
// should be reported to the user
47
47
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
+ }
50
54
} else {
51
- feedback .Errorf ("Error reading inventory file: %v " , err )
55
+ return fmt .Errorf ("reading inventory file: %w " , err )
52
56
}
53
57
}
58
+
59
+ return nil
54
60
}
55
61
56
- func generateInstallationData () {
62
+ func generateInstallationData () error {
57
63
installationID , err := uuid .NewV4 ()
58
64
if err != nil {
59
- feedback .Errorf ("Error generating installation.id: %v " , err )
65
+ return fmt .Errorf ("generating installation.id: %w " , err )
60
66
}
61
67
Store .Set ("installation.id" , installationID .String ())
62
68
63
69
installationSecret , err := uuid .NewV4 ()
64
70
if err != nil {
65
- feedback .Errorf ("Error generating installation.secret: %v " , err )
71
+ return fmt .Errorf ("generating installation.secret: %w " , err )
66
72
}
67
73
Store .Set ("installation.secret" , installationSecret .String ())
74
+ return nil
68
75
}
69
76
70
- func writeStore (configFilePath string ) {
77
+ func writeStore (configFilePath string ) error {
71
78
configPath := filepath .Dir (configFilePath )
72
79
73
80
// Create config dir if not present,
74
81
// MkdirAll will retrun no error if the path already exists
75
82
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 )
77
84
}
78
85
79
86
// Create file if not present
80
87
err := Store .WriteConfigAs (configFilePath )
81
88
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 )
83
90
}
91
+
92
+ return nil
84
93
}
0 commit comments