Skip to content

Commit 50eef88

Browse files
authored
Enhance error messages if config paths are not valid (arduino#1130)
1 parent d8fc095 commit 50eef88

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

cli/cli.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,11 @@ func preRun(cmd *cobra.Command, args []string) {
136136
configFile := configuration.Settings.ConfigFileUsed()
137137

138138
// initialize inventory
139-
inventory.Init(configuration.Settings.GetString("directories.Data"))
139+
err := inventory.Init(configuration.Settings.GetString("directories.Data"))
140+
if err != nil {
141+
feedback.Errorf("Error: %v", err)
142+
os.Exit(errorcodes.ErrBadArgument)
143+
}
140144

141145
//
142146
// Prepare logging

inventory/inventory.go

+20-11
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
package inventory
1717

1818
import (
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
)
@@ -35,7 +35,7 @@ var (
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

Comments
 (0)