@@ -17,16 +17,20 @@ package config
1717
1818import (
1919 "os"
20- "path/filepath"
2120
2221 "github.com/arduino/arduino-cli/cli/errorcodes"
2322 "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 destDir string
29+ var (
30+ destDir string
31+ destFile string
32+ overwrite bool
33+ )
3034
3135const defaultFileName = "arduino-cli.yaml"
3236
@@ -37,39 +41,66 @@ func initInitCommand() *cobra.Command {
3741 Long : "Creates or updates the configuration file in the data directory or custom directory with the current configuration settings." ,
3842 Example : "" +
3943 " # Writes current configuration to the configuration file in the data directory.\n " +
40- " " + os .Args [0 ] + " config init" ,
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" ,
4147 Args : cobra .NoArgs ,
4248 Run : runInitCommand ,
4349 }
4450 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." )
4553 return initCommand
4654}
4755
4856func runInitCommand (cmd * cobra.Command , args []string ) {
49- if destDir == "" {
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 == "" :
5076 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 )
5185 }
5286
53- absPath , err := filepath .Abs (destDir )
54- if err != nil {
55- feedback .Errorf ("Cannot find absolute path: %v" , err )
87+ if ! overwrite && configFileAbsPath .Exist () {
88+ feedback .Error ("Config file already exists, use --overwrite to discard the existing one." )
5689 os .Exit (errorcodes .ErrGeneric )
5790 }
58- configFileAbsPath := filepath .Join (absPath , defaultFileName )
5991
6092 logrus .Infof ("Writing config file to: %s" , absPath )
61-
62- if err := os .MkdirAll (absPath , os .FileMode (0755 )); err != nil {
93+ if err := absPath .MkdirAll (); err != nil {
6394 feedback .Errorf ("Cannot create config file directory: %v" , err )
6495 os .Exit (errorcodes .ErrGeneric )
6596 }
6697
67- if err := viper .WriteConfigAs (configFileAbsPath ); err != nil {
98+ if err := viper .WriteConfigAs (configFileAbsPath . String () ); err != nil {
6899 feedback .Errorf ("Cannot create config file: %v" , err )
69100 os .Exit (errorcodes .ErrGeneric )
70101 }
71102
72- msg := "Config file written to: " + configFileAbsPath
103+ msg := "Config file written to: " + configFileAbsPath . String ()
73104 logrus .Info (msg )
74105 feedback .Print (msg )
75106}
0 commit comments