@@ -17,8 +17,11 @@ package config
17
17
18
18
import (
19
19
"context"
20
+ "encoding/json"
20
21
"os"
22
+ "strings"
21
23
24
+ "github.com/arduino/arduino-cli/commands"
22
25
"github.com/arduino/arduino-cli/internal/cli/arguments"
23
26
"github.com/arduino/arduino-cli/internal/cli/feedback"
24
27
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
35
38
36
39
const defaultFileName = "arduino-cli.yaml"
37
40
38
- func initInitCommand (srv rpc. ArduinoCoreServiceServer ) * cobra.Command {
41
+ func initInitCommand () * cobra.Command {
39
42
initCommand := & cobra.Command {
40
43
Use : "init" ,
41
44
Short : tr ("Writes current configuration to a configuration file." ),
@@ -50,7 +53,7 @@ func initInitCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command {
50
53
arguments .CheckFlagsConflicts (cmd , "dest-file" , "dest-dir" )
51
54
},
52
55
Run : func (cmd * cobra.Command , args []string ) {
53
- runInitCommand (srv )
56
+ runInitCommand (cmd . Context (), cmd )
54
57
},
55
58
}
56
59
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 {
59
62
return initCommand
60
63
}
61
64
62
- func runInitCommand (srv rpc. ArduinoCoreServiceServer ) {
65
+ func runInitCommand (ctx context. Context , cmd * cobra. Command ) {
63
66
logrus .Info ("Executing `arduino-cli config init`" )
64
- ctx := context .Background ()
65
67
66
68
var configFileAbsPath * paths.Path
67
69
var configFileDir * paths.Path
@@ -97,14 +99,22 @@ func runInitCommand(srv rpc.ArduinoCoreServiceServer) {
97
99
feedback .Fatal (tr ("Cannot create config file directory: %v" , err ), feedback .ErrGeneric )
98
100
}
99
101
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 ()
106
103
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" })
108
118
if err != nil {
109
119
feedback .Fatal (tr ("Error creating configuration: %v" , err ), feedback .ErrGeneric )
110
120
}
@@ -116,6 +126,44 @@ func runInitCommand(srv rpc.ArduinoCoreServiceServer) {
116
126
feedback .PrintResult (initResult {ConfigFileAbsPath : configFileAbsPath })
117
127
}
118
128
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
+
119
167
// output from this command requires special formatting, let's create a dedicated
120
168
// feedback.Result implementation
121
169
type initResult struct {
0 commit comments