Skip to content

Commit 37ba57e

Browse files
authored
Revert "Add --dest-file flag to config init command (#957)" (#1026)
This reverts commit 900654f.
1 parent ba13ab4 commit 37ba57e

File tree

6 files changed

+41
-230
lines changed

6 files changed

+41
-230
lines changed

Diff for: cli/config/init.go

+12-43
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,16 @@ package config
1717

1818
import (
1919
"os"
20+
"path/filepath"
2021

2122
"github.com/arduino/arduino-cli/cli/errorcodes"
2223
"github.com/arduino/arduino-cli/cli/feedback"
23-
paths "github.com/arduino/go-paths-helper"
2424
"github.com/sirupsen/logrus"
2525
"github.com/spf13/cobra"
2626
"github.com/spf13/viper"
2727
)
2828

29-
var (
30-
destDir string
31-
destFile string
32-
overwrite bool
33-
)
29+
var destDir string
3430

3531
const defaultFileName = "arduino-cli.yaml"
3632

@@ -41,66 +37,39 @@ func initInitCommand() *cobra.Command {
4137
Long: "Creates or updates the configuration file in the data directory or custom directory with the current configuration settings.",
4238
Example: "" +
4339
" # Writes current configuration to the configuration file in the data directory.\n" +
44-
" " + os.Args[0] + " config init" +
45-
" " + os.Args[0] + " config init --dest-dir /home/user/MyDirectory" +
46-
" " + os.Args[0] + " config init --dest-file /home/user/MyDirectory/my_settings.yaml",
40+
" " + os.Args[0] + " config init",
4741
Args: cobra.NoArgs,
4842
Run: runInitCommand,
4943
}
5044
initCommand.Flags().StringVar(&destDir, "dest-dir", "", "Sets where to save the configuration file.")
51-
initCommand.Flags().StringVar(&destFile, "dest-file", "", "Sets where to save the configuration file.")
52-
initCommand.Flags().BoolVar(&overwrite, "overwrite", false, "Overwrite existing config file.")
5345
return initCommand
5446
}
5547

5648
func runInitCommand(cmd *cobra.Command, args []string) {
57-
if destFile != "" && destDir != "" {
58-
feedback.Errorf("Can't use both --dest-file and --dest-dir flags at the same time.")
59-
os.Exit(errorcodes.ErrGeneric)
60-
}
61-
62-
var configFileAbsPath *paths.Path
63-
var absPath *paths.Path
64-
var err error
65-
66-
switch {
67-
case destFile != "":
68-
configFileAbsPath, err = paths.New(destFile).Abs()
69-
if err != nil {
70-
feedback.Errorf("Cannot find absolute path: %v", err)
71-
os.Exit(errorcodes.ErrGeneric)
72-
}
73-
74-
absPath = configFileAbsPath.Parent()
75-
case destDir == "":
49+
if destDir == "" {
7650
destDir = viper.GetString("directories.Data")
77-
fallthrough
78-
default:
79-
absPath, err = paths.New(destDir).Abs()
80-
if err != nil {
81-
feedback.Errorf("Cannot find absolute path: %v", err)
82-
os.Exit(errorcodes.ErrGeneric)
83-
}
84-
configFileAbsPath = absPath.Join(defaultFileName)
8551
}
8652

87-
if !overwrite && configFileAbsPath.Exist() {
88-
feedback.Error("Config file already exists, use --overwrite to discard the existing one.")
53+
absPath, err := filepath.Abs(destDir)
54+
if err != nil {
55+
feedback.Errorf("Cannot find absolute path: %v", err)
8956
os.Exit(errorcodes.ErrGeneric)
9057
}
58+
configFileAbsPath := filepath.Join(absPath, defaultFileName)
9159

9260
logrus.Infof("Writing config file to: %s", absPath)
93-
if err := absPath.MkdirAll(); err != nil {
61+
62+
if err := os.MkdirAll(absPath, os.FileMode(0755)); err != nil {
9463
feedback.Errorf("Cannot create config file directory: %v", err)
9564
os.Exit(errorcodes.ErrGeneric)
9665
}
9766

98-
if err := viper.WriteConfigAs(configFileAbsPath.String()); err != nil {
67+
if err := viper.WriteConfigAs(configFileAbsPath); err != nil {
9968
feedback.Errorf("Cannot create config file: %v", err)
10069
os.Exit(errorcodes.ErrGeneric)
10170
}
10271

103-
msg := "Config file written to: " + configFileAbsPath.String()
72+
msg := "Config file written to: " + configFileAbsPath
10473
logrus.Info(msg)
10574
feedback.Print(msg)
10675
}

Diff for: commands/daemon/settings_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package daemon
1818
import (
1919
"context"
2020
"encoding/json"
21-
"path/filepath"
2221
"testing"
2322

2423
"github.com/spf13/viper"
@@ -31,12 +30,12 @@ import (
3130
var svc = SettingsService{}
3231

3332
func init() {
34-
configuration.Init(filepath.Join("testdata", "arduino-cli.yaml"))
33+
configuration.Init("testdata")
3534
}
3635

3736
func reset() {
3837
viper.Reset()
39-
configuration.Init(filepath.Join("testdata", "arduino-cli.yaml"))
38+
configuration.Init("testdata")
4039
}
4140

4241
func TestGetAll(t *testing.T) {

Diff for: configuration/configuration.go

+23-28
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,15 @@ import (
3535
func Init(configPath string) {
3636
// Config file metadata
3737
jww.SetStdoutThreshold(jww.LevelFatal)
38-
39-
configDir := paths.New(configPath)
40-
if configDir != nil && !configDir.IsDir() {
41-
viper.SetConfigName(strings.TrimSuffix(configDir.Base(), configDir.Ext()))
42-
} else {
43-
viper.SetConfigName("arduino-cli")
44-
}
38+
viper.SetConfigName("arduino-cli")
4539

4640
// Get default data path if none was provided
4741
if configPath == "" {
4842
configPath = getDefaultArduinoDataDir()
4943
}
5044

5145
// Add paths where to search for a config file
52-
viper.AddConfigPath(filepath.Dir(configPath))
46+
viper.AddConfigPath(configPath)
5347

5448
// Bind env vars
5549
viper.SetEnvPrefix("ARDUINO")
@@ -191,39 +185,30 @@ func IsBundledInDesktopIDE() bool {
191185
}
192186

193187
// FindConfigFile returns the config file path using the argument '--config-file' if specified or via the current working dir
194-
func FindConfigFile(args []string) string {
188+
func FindConfigFile() string {
189+
195190
configFile := ""
196-
for i, arg := range args {
191+
for i, arg := range os.Args {
197192
// 0 --config-file ss
198193
if arg == "--config-file" {
199-
if len(args) > i+1 {
200-
configFile = args[i+1]
194+
if len(os.Args) > i+1 {
195+
configFile = os.Args[i+1]
201196
}
202197
}
203198
}
204199

205200
if configFile != "" {
206-
return configFile
201+
if fi, err := os.Stat(configFile); err == nil {
202+
if fi.IsDir() {
203+
return configFile
204+
}
205+
return filepath.Dir(configFile)
206+
}
207207
}
208208

209209
return searchCwdForConfig()
210210
}
211211

212-
func searchCwdForConfig() string {
213-
cwd, err := os.Getwd()
214-
215-
if err != nil {
216-
return ""
217-
}
218-
219-
configFile := searchConfigTree(cwd)
220-
if configFile == "" {
221-
return configFile
222-
}
223-
224-
return configFile + string(os.PathSeparator) + "arduino-cli.yaml"
225-
}
226-
227212
func searchConfigTree(cwd string) string {
228213

229214
// go back up to root and search for the config file
@@ -245,3 +230,13 @@ func searchConfigTree(cwd string) string {
245230
}
246231

247232
}
233+
234+
func searchCwdForConfig() string {
235+
cwd, err := os.Getwd()
236+
237+
if err != nil {
238+
return ""
239+
}
240+
241+
return searchConfigTree(cwd)
242+
}

Diff for: configuration/configuration_test.go

-43
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,6 @@ func tmpDirOrDie() string {
3030
if err != nil {
3131
panic(fmt.Sprintf("error creating tmp dir: %v", err))
3232
}
33-
// Symlinks are evaluated becase the temp folder on Mac OS is inside /var, it's not writable
34-
// and is a symlink to /private/var, we want the full path so we do this
35-
dir, err = filepath.EvalSymlinks(dir)
36-
if err != nil {
37-
panic(fmt.Sprintf("error evaluating tmp dir symlink: %v", err))
38-
}
3933
return dir
4034
}
4135

@@ -77,40 +71,3 @@ func BenchmarkSearchConfigTree(b *testing.B) {
7771
}
7872
result = s
7973
}
80-
81-
func TestFindConfigFile(t *testing.T) {
82-
configFile := FindConfigFile([]string{"--config-file"})
83-
require.Equal(t, "", configFile)
84-
85-
configFile = FindConfigFile([]string{"--config-file", "some/path/to/config"})
86-
require.Equal(t, "some/path/to/config", configFile)
87-
88-
configFile = FindConfigFile([]string{"--config-file", "some/path/to/config/arduino-cli.yaml"})
89-
require.Equal(t, "some/path/to/config/arduino-cli.yaml", configFile)
90-
91-
configFile = FindConfigFile([]string{})
92-
require.Equal(t, "", configFile)
93-
94-
// Create temporary directories
95-
tmp := tmpDirOrDie()
96-
defer os.RemoveAll(tmp)
97-
target := filepath.Join(tmp, "foo", "bar", "baz")
98-
os.MkdirAll(target, os.ModePerm)
99-
require.Nil(t, os.Chdir(target))
100-
101-
// Create a config file
102-
f, err := os.Create(filepath.Join(target, "..", "..", "arduino-cli.yaml"))
103-
require.Nil(t, err)
104-
f.Close()
105-
106-
configFile = FindConfigFile([]string{})
107-
require.Equal(t, filepath.Join(tmp, "foo", "arduino-cli.yaml"), configFile)
108-
109-
// Create another config file
110-
f, err = os.Create(filepath.Join(target, "arduino-cli.yaml"))
111-
require.Nil(t, err)
112-
f.Close()
113-
114-
configFile = FindConfigFile([]string{})
115-
require.Equal(t, filepath.Join(target, "arduino-cli.yaml"), configFile)
116-
}

Diff for: main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
)
2626

2727
func main() {
28-
configuration.Init(configuration.FindConfigFile(os.Args))
28+
configuration.Init(configuration.FindConfigFile())
2929
i18n.Init()
3030
arduinoCmd := cli.NewCommand()
3131
if err := arduinoCmd.Execute(); err != nil {

0 commit comments

Comments
 (0)