Skip to content

Commit 030e221

Browse files
author
Roberto Sora
committed
Use viper config for repertory dir
1 parent 2185922 commit 030e221

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

cli/cli.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ func preRun(cmd *cobra.Command, args []string) {
165165
configFile := viper.ConfigFileUsed()
166166

167167
// initialize repertory
168-
repertory.Init()
168+
repertory.Init(viper.GetString("directories.Data"))
169169

170170
//
171171
// Prepare logging

repertory/repertory.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
package repertory
1717

1818
import (
19+
"os"
1920
"path/filepath"
2021

2122
"github.com/arduino/arduino-cli/cli/feedback"
22-
"github.com/arduino/arduino-cli/configuration"
2323
"github.com/gofrs/uuid"
2424
"github.com/spf13/viper"
2525
)
@@ -33,22 +33,18 @@ var (
3333
)
3434

3535
// Configure configures the Read Only config storage
36-
func Init() {
37-
configPath := configuration.GetDefaultArduinoDataDir()
36+
func Init(configPath string) {
37+
configFilePath := filepath.Join(configPath, Name)
3838
Store.SetConfigName(Name)
3939
Store.SetConfigType(Type)
4040
Store.AddConfigPath(configPath)
41-
configFilePath := filepath.Join(configPath, Name)
4241
// Attempt to read config file
4342
if err := Store.ReadInConfig(); err != nil {
4443
// ConfigFileNotFoundError is acceptable, anything else
4544
// should be reported to the user
4645
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
4746
generateInstallationData()
48-
err := Store.WriteConfigAs(configFilePath)
49-
if err != nil {
50-
feedback.Errorf("Error writing repertory file: %v", err)
51-
}
47+
writeStore(configFilePath)
5248
} else {
5349
feedback.Errorf("Error reading repertory file: %v", err)
5450
}
@@ -69,3 +65,16 @@ func generateInstallationData() {
6965
}
7066
Store.Set("installation.secret", installationSecret.String())
7167
}
68+
69+
func writeStore(configFilePath string) {
70+
configPath := filepath.Dir(configFilePath)
71+
// Create dir if not present
72+
if _, err := os.Stat(configPath); os.IsNotExist(err) {
73+
os.MkdirAll(configPath, 0755)
74+
}
75+
// Create file if not present
76+
err := Store.WriteConfigAs(configFilePath)
77+
if err != nil {
78+
feedback.Errorf("Error writing repertory file: %v", err)
79+
}
80+
}

0 commit comments

Comments
 (0)