@@ -17,20 +17,16 @@ package config
1717
1818import  (
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
3531const  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
5648func  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}
0 commit comments