@@ -17,8 +17,11 @@ package config
1717
1818import (
1919 "context"
20+ "encoding/json"
2021 "os"
22+ "strings"
2123
24+ "github.com/arduino/arduino-cli/commands"
2225 "github.com/arduino/arduino-cli/internal/cli/arguments"
2326 "github.com/arduino/arduino-cli/internal/cli/feedback"
2427 rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
3538
3639const defaultFileName = "arduino-cli.yaml"
3740
38- func initInitCommand (srv rpc. ArduinoCoreServiceServer ) * cobra.Command {
41+ func initInitCommand () * cobra.Command {
3942 initCommand := & cobra.Command {
4043 Use : "init" ,
4144 Short : tr ("Writes current configuration to a configuration file." ),
@@ -50,7 +53,7 @@ func initInitCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command {
5053 arguments .CheckFlagsConflicts (cmd , "dest-file" , "dest-dir" )
5154 },
5255 Run : func (cmd * cobra.Command , args []string ) {
53- runInitCommand (srv )
56+ runInitCommand (cmd . Context (), cmd )
5457 },
5558 }
5659 initCommand .Flags ().StringVar (& destDir , "dest-dir" , "" , tr ("Sets where to save the configuration file." ))
@@ -59,9 +62,8 @@ func initInitCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command {
5962 return initCommand
6063}
6164
62- func runInitCommand (srv rpc. ArduinoCoreServiceServer ) {
65+ func runInitCommand (ctx context. Context , cmd * cobra. Command ) {
6366 logrus .Info ("Executing `arduino-cli config init`" )
64- ctx := context .Background ()
6567
6668 var configFileAbsPath * paths.Path
6769 var configFileDir * paths.Path
@@ -97,14 +99,22 @@ func runInitCommand(srv rpc.ArduinoCoreServiceServer) {
9799 feedback .Fatal (tr ("Cannot create config file directory: %v" , err ), feedback .ErrGeneric )
98100 }
99101
100- // for _, url := range newSettings.GetStringSlice("board_manager.additional_urls") {
101- // if strings.Contains(url, ",") {
102- // feedback.Fatal(tr("Urls cannot contain commas. Separate multiple urls exported as env var with a space:\n%s", url),
103- // feedback.ErrGeneric)
104- // }
105- // }
102+ tmpSrv := commands .NewArduinoCoreServer ()
106103
107- resp , err := srv .ConfigurationSave (ctx , & rpc.ConfigurationSaveRequest {SettingsFormat : "yaml" })
104+ if _ , err := tmpSrv .ConfigurationOpen (ctx , & rpc.ConfigurationOpenRequest {SettingsFormat : "yaml" , EncodedSettings : "" }); err != nil {
105+ feedback .Fatal (tr ("Error creating configuration: %v" , err ), feedback .ErrGeneric )
106+ }
107+
108+ // Ensure to always output an empty array for additional urls
109+ if _ , err := tmpSrv .SettingsSetValue (ctx , & rpc.SettingsSetValueRequest {
110+ Key : "board_manager.additional_urls" , EncodedValue : "[]" ,
111+ }); err != nil {
112+ feedback .Fatal (tr ("Error creating configuration: %v" , err ), feedback .ErrGeneric )
113+ }
114+
115+ ApplyGlobalFlagsToConfiguration (ctx , cmd , tmpSrv )
116+
117+ resp , err := tmpSrv .ConfigurationSave (ctx , & rpc.ConfigurationSaveRequest {SettingsFormat : "yaml" })
108118 if err != nil {
109119 feedback .Fatal (tr ("Error creating configuration: %v" , err ), feedback .ErrGeneric )
110120 }
@@ -116,6 +126,44 @@ func runInitCommand(srv rpc.ArduinoCoreServiceServer) {
116126 feedback .PrintResult (initResult {ConfigFileAbsPath : configFileAbsPath })
117127}
118128
129+ // ApplyGlobalFlagsToConfiguration overrides server settings with the flags from the command line
130+ func ApplyGlobalFlagsToConfiguration (ctx context.Context , cmd * cobra.Command , srv rpc.ArduinoCoreServiceServer ) {
131+ set := func (k string , v any ) {
132+ if jsonValue , err := json .Marshal (v ); err != nil {
133+ feedback .Fatal (tr ("Error creating configuration: %v" , err ), feedback .ErrGeneric )
134+ } else if _ , err := srv .SettingsSetValue (ctx , & rpc.SettingsSetValueRequest {
135+ Key : k , EncodedValue : string (jsonValue ),
136+ }); err != nil {
137+ feedback .Fatal (tr ("Error creating configuration: %v" , err ), feedback .ErrGeneric )
138+ }
139+
140+ }
141+
142+ if f := cmd .Flags ().Lookup ("log-level" ); f .Changed {
143+ logLevel , _ := cmd .Flags ().GetString ("log-level" )
144+ set ("logging.level" , logLevel )
145+ }
146+ if f := cmd .Flags ().Lookup ("log-file" ); f .Changed {
147+ logFile , _ := cmd .Flags ().GetString ("log-file" )
148+ set ("logging.file" , logFile )
149+ }
150+ if f := cmd .Flags ().Lookup ("no-color" ); f .Changed {
151+ noColor , _ := cmd .Flags ().GetBool ("no-color" )
152+ set ("output.no_color" , noColor )
153+ }
154+ if f := cmd .Flags ().Lookup ("additional-urls" ); f .Changed {
155+ urls , _ := cmd .Flags ().GetStringSlice ("additional-urls" )
156+ for _ , url := range urls {
157+ if strings .Contains (url , "," ) {
158+ feedback .Fatal (
159+ tr ("Urls cannot contain commas. Separate multiple urls exported as env var with a space:\n %s" , url ),
160+ feedback .ErrBadArgument )
161+ }
162+ }
163+ set ("board_manager.additional_urls" , urls )
164+ }
165+ }
166+
119167// output from this command requires special formatting, let's create a dedicated
120168// feedback.Result implementation
121169type initResult struct {
0 commit comments