Commit 0aafda3a authored by Cristian Maglie's avatar Cristian Maglie

Fixed initialization sequence for 'config init' command

parent 6aa742ff
......@@ -17,7 +17,6 @@ package cli
import (
"context"
"encoding/json"
"fmt"
"io"
"os"
......@@ -81,6 +80,7 @@ func NewCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command {
defaultLogFile := settings.GetLogging().GetFile()
defaultLogFormat := settings.GetLogging().GetFormat()
defaultLogLevel := settings.GetLogging().GetLevel()
defaultAdditionalURLs := settings.GetBoardManager().GetAdditionalUrls()
defaultOutputNoColor := settings.GetOutput().GetNoColor()
......@@ -92,18 +92,7 @@ func NewCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command {
PersistentPreRun: func(cmd *cobra.Command, args []string) {
ctx := cmd.Context()
// Override server settings with the flags from the command line
set := func(key string, value any) {
if valueJson, err := json.Marshal(value); err != nil {
feedback.Fatal(tr("Error setting value %s: %v", key, err), feedback.ErrGeneric)
} else if _, err := srv.SettingsSetValue(ctx, &rpc.SettingsSetValueRequest{Key: key, EncodedValue: string(valueJson)}); err != nil {
feedback.Fatal(tr("Error setting value %s: %v", key, err), feedback.ErrGeneric)
}
}
set("logging.level", logLevel)
set("logging.file", logFile)
set("board_manager.additional_urls", additionalUrls)
set("output.no_color", noColor)
config.ApplyGlobalFlagsToConfiguration(ctx, cmd, srv)
if jsonOutput {
outputFormat = "json"
......@@ -178,7 +167,6 @@ func NewCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command {
cmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, tr("Print the logs on the standard output."))
cmd.Flag("verbose").Hidden = true
cmd.PersistentFlags().BoolVar(&verbose, "log", false, tr("Print the logs on the standard output."))
defaultLogLevel := settings.GetLogging().GetLevel()
validLogLevels := []string{"trace", "debug", "info", "warn", "error", "fatal", "panic"}
cmd.PersistentFlags().StringVar(&logLevel, "log-level", defaultLogLevel, tr("Messages with this level and above will be logged. Valid levels are: %s", strings.Join(validLogLevels, ", ")))
cmd.RegisterFlagCompletionFunc("log-level", cobra.FixedCompletions(validLogLevels, cobra.ShellCompDirectiveDefault))
......
......@@ -42,7 +42,7 @@ func NewCommand(srv rpc.ArduinoCoreServiceServer, settings *rpc.Configuration) *
configCommand.AddCommand(initDeleteCommand(srv))
configCommand.AddCommand(initDumpCommand(srv))
configCommand.AddCommand(initGetCommand(srv))
configCommand.AddCommand(initInitCommand(srv))
configCommand.AddCommand(initInitCommand())
configCommand.AddCommand(initRemoveCommand(srv))
configCommand.AddCommand(initSetCommand(srv))
......
......@@ -17,8 +17,11 @@ package config
import (
"context"
"encoding/json"
"os"
"strings"
"github.com/arduino/arduino-cli/commands"
"github.com/arduino/arduino-cli/internal/cli/arguments"
"github.com/arduino/arduino-cli/internal/cli/feedback"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
......@@ -35,7 +38,7 @@ var (
const defaultFileName = "arduino-cli.yaml"
func initInitCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command {
func initInitCommand() *cobra.Command {
initCommand := &cobra.Command{
Use: "init",
Short: tr("Writes current configuration to a configuration file."),
......@@ -50,7 +53,7 @@ func initInitCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command {
arguments.CheckFlagsConflicts(cmd, "dest-file", "dest-dir")
},
Run: func(cmd *cobra.Command, args []string) {
runInitCommand(srv)
runInitCommand(cmd.Context(), cmd)
},
}
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 {
return initCommand
}
func runInitCommand(srv rpc.ArduinoCoreServiceServer) {
func runInitCommand(ctx context.Context, cmd *cobra.Command) {
logrus.Info("Executing `arduino-cli config init`")
ctx := context.Background()
var configFileAbsPath *paths.Path
var configFileDir *paths.Path
......@@ -97,14 +99,22 @@ func runInitCommand(srv rpc.ArduinoCoreServiceServer) {
feedback.Fatal(tr("Cannot create config file directory: %v", err), feedback.ErrGeneric)
}
// for _, url := range newSettings.GetStringSlice("board_manager.additional_urls") {
// if strings.Contains(url, ",") {
// feedback.Fatal(tr("Urls cannot contain commas. Separate multiple urls exported as env var with a space:\n%s", url),
// feedback.ErrGeneric)
// }
// }
tmpSrv := commands.NewArduinoCoreServer()
resp, err := srv.ConfigurationSave(ctx, &rpc.ConfigurationSaveRequest{SettingsFormat: "yaml"})
if _, err := tmpSrv.ConfigurationOpen(ctx, &rpc.ConfigurationOpenRequest{SettingsFormat: "yaml", EncodedSettings: ""}); err != nil {
feedback.Fatal(tr("Error creating configuration: %v", err), feedback.ErrGeneric)
}
// Ensure to always output an empty array for additional urls
if _, err := tmpSrv.SettingsSetValue(ctx, &rpc.SettingsSetValueRequest{
Key: "board_manager.additional_urls", EncodedValue: "[]",
}); err != nil {
feedback.Fatal(tr("Error creating configuration: %v", err), feedback.ErrGeneric)
}
ApplyGlobalFlagsToConfiguration(ctx, cmd, tmpSrv)
resp, err := tmpSrv.ConfigurationSave(ctx, &rpc.ConfigurationSaveRequest{SettingsFormat: "yaml"})
if err != nil {
feedback.Fatal(tr("Error creating configuration: %v", err), feedback.ErrGeneric)
}
......@@ -116,6 +126,44 @@ func runInitCommand(srv rpc.ArduinoCoreServiceServer) {
feedback.PrintResult(initResult{ConfigFileAbsPath: configFileAbsPath})
}
// ApplyGlobalFlagsToConfiguration overrides server settings with the flags from the command line
func ApplyGlobalFlagsToConfiguration(ctx context.Context, cmd *cobra.Command, srv rpc.ArduinoCoreServiceServer) {
set := func(k string, v any) {
if jsonValue, err := json.Marshal(v); err != nil {
feedback.Fatal(tr("Error creating configuration: %v", err), feedback.ErrGeneric)
} else if _, err := srv.SettingsSetValue(ctx, &rpc.SettingsSetValueRequest{
Key: k, EncodedValue: string(jsonValue),
}); err != nil {
feedback.Fatal(tr("Error creating configuration: %v", err), feedback.ErrGeneric)
}
}
if f := cmd.Flags().Lookup("log-level"); f.Changed {
logLevel, _ := cmd.Flags().GetString("log-level")
set("logging.level", logLevel)
}
if f := cmd.Flags().Lookup("log-file"); f.Changed {
logFile, _ := cmd.Flags().GetString("log-file")
set("logging.file", logFile)
}
if f := cmd.Flags().Lookup("no-color"); f.Changed {
noColor, _ := cmd.Flags().GetBool("no-color")
set("output.no_color", noColor)
}
if f := cmd.Flags().Lookup("additional-urls"); f.Changed {
urls, _ := cmd.Flags().GetStringSlice("additional-urls")
for _, url := range urls {
if strings.Contains(url, ",") {
feedback.Fatal(
tr("Urls cannot contain commas. Separate multiple urls exported as env var with a space:\n%s", url),
feedback.ErrBadArgument)
}
}
set("board_manager.additional_urls", urls)
}
}
// output from this command requires special formatting, let's create a dedicated
// feedback.Result implementation
type initResult struct {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment