Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 0aafda3

Browse filesBrowse files
committed
Fixed initialization sequence for 'config init' command
1 parent 6aa742f commit 0aafda3
Copy full SHA for 0aafda3

File tree

Expand file treeCollapse file tree

3 files changed

+62
-26
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+62
-26
lines changed

‎internal/cli/cli.go

Copy file name to clipboardExpand all lines: internal/cli/cli.go
+2-14Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package cli
1717

1818
import (
1919
"context"
20-
"encoding/json"
2120
"fmt"
2221
"io"
2322
"os"
@@ -81,6 +80,7 @@ func NewCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command {
8180

8281
defaultLogFile := settings.GetLogging().GetFile()
8382
defaultLogFormat := settings.GetLogging().GetFormat()
83+
defaultLogLevel := settings.GetLogging().GetLevel()
8484
defaultAdditionalURLs := settings.GetBoardManager().GetAdditionalUrls()
8585
defaultOutputNoColor := settings.GetOutput().GetNoColor()
8686

@@ -92,18 +92,7 @@ func NewCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command {
9292
PersistentPreRun: func(cmd *cobra.Command, args []string) {
9393
ctx := cmd.Context()
9494

95-
// Override server settings with the flags from the command line
96-
set := func(key string, value any) {
97-
if valueJson, err := json.Marshal(value); err != nil {
98-
feedback.Fatal(tr("Error setting value %s: %v", key, err), feedback.ErrGeneric)
99-
} else if _, err := srv.SettingsSetValue(ctx, &rpc.SettingsSetValueRequest{Key: key, EncodedValue: string(valueJson)}); err != nil {
100-
feedback.Fatal(tr("Error setting value %s: %v", key, err), feedback.ErrGeneric)
101-
}
102-
}
103-
set("logging.level", logLevel)
104-
set("logging.file", logFile)
105-
set("board_manager.additional_urls", additionalUrls)
106-
set("output.no_color", noColor)
95+
config.ApplyGlobalFlagsToConfiguration(ctx, cmd, srv)
10796

10897
if jsonOutput {
10998
outputFormat = "json"
@@ -178,7 +167,6 @@ func NewCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command {
178167
cmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, tr("Print the logs on the standard output."))
179168
cmd.Flag("verbose").Hidden = true
180169
cmd.PersistentFlags().BoolVar(&verbose, "log", false, tr("Print the logs on the standard output."))
181-
defaultLogLevel := settings.GetLogging().GetLevel()
182170
validLogLevels := []string{"trace", "debug", "info", "warn", "error", "fatal", "panic"}
183171
cmd.PersistentFlags().StringVar(&logLevel, "log-level", defaultLogLevel, tr("Messages with this level and above will be logged. Valid levels are: %s", strings.Join(validLogLevels, ", ")))
184172
cmd.RegisterFlagCompletionFunc("log-level", cobra.FixedCompletions(validLogLevels, cobra.ShellCompDirectiveDefault))

‎internal/cli/config/config.go

Copy file name to clipboardExpand all lines: internal/cli/config/config.go
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func NewCommand(srv rpc.ArduinoCoreServiceServer, settings *rpc.Configuration) *
4242
configCommand.AddCommand(initDeleteCommand(srv))
4343
configCommand.AddCommand(initDumpCommand(srv))
4444
configCommand.AddCommand(initGetCommand(srv))
45-
configCommand.AddCommand(initInitCommand(srv))
45+
configCommand.AddCommand(initInitCommand())
4646
configCommand.AddCommand(initRemoveCommand(srv))
4747
configCommand.AddCommand(initSetCommand(srv))
4848

‎internal/cli/config/init.go

Copy file name to clipboardExpand all lines: internal/cli/config/init.go
+59-11Lines changed: 59 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@ package config
1717

1818
import (
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"
@@ -35,7 +38,7 @@ var (
3538

3639
const 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
121169
type initResult struct {

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.