Commit 04536776 authored by Cristian Maglie's avatar Cristian Maglie

Use custom type to avoid context-values collisions

parent 4c50868a
......@@ -117,7 +117,7 @@ func NewCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command {
preRun(verbose, outputFormat, logLevel, logFile, logFormat, noColor, settings)
// Log the configuration file used
if configFile := ctx.Value("config_file").(string); configFile != "" {
if configFile := config.GetConfigFile(ctx); configFile != "" {
logrus.Infof("Using config file: %s", configFile)
} else {
logrus.Info("Config file not found, using default values")
......
......@@ -64,6 +64,22 @@ func getAllArraySettingsKeys(ctx context.Context, srv rpc.ArduinoCoreServiceServ
return arrayKeys
}
type ctxValue string
// GetConfigFile returns the configuration file path from the context
func GetConfigFile(ctx context.Context) string {
res := ctx.Value(ctxValue("config_file"))
if res == nil {
return ""
}
return res.(string)
}
// SetConfigFile sets the configuration file path in the context
func SetConfigFile(ctx context.Context, configFile string) context.Context {
return context.WithValue(ctx, ctxValue("config_file"), configFile)
}
func saveConfiguration(ctx context.Context, srv rpc.ArduinoCoreServiceServer) {
var outConfig []byte
if res, err := srv.ConfigurationSave(ctx, &rpc.ConfigurationSaveRequest{SettingsFormat: "yaml"}); err != nil {
......@@ -72,7 +88,7 @@ func saveConfiguration(ctx context.Context, srv rpc.ArduinoCoreServiceServer) {
outConfig = []byte(res.GetEncodedSettings())
}
configFile := ctx.Value("config_file").(string)
configFile := GetConfigFile(ctx)
if err := paths.New(configFile).WriteFile(outConfig); err != nil {
feedback.Fatal(tr("Error writing to file: %v", err), feedback.ErrGeneric)
}
......
......@@ -83,7 +83,7 @@ func runInitCommand(srv rpc.ArduinoCoreServiceServer) {
configFileAbsPath = configFileDir.Join(defaultFileName)
default:
configFileAbsPath = paths.New(ctx.Value("config_file").(string))
configFileAbsPath = paths.New(GetConfigFile(ctx))
configFileDir = configFileAbsPath.Parent()
}
......
......@@ -22,6 +22,7 @@ import (
"github.com/arduino/arduino-cli/commands"
"github.com/arduino/arduino-cli/internal/cli"
"github.com/arduino/arduino-cli/internal/cli/config"
"github.com/arduino/arduino-cli/internal/cli/configuration"
"github.com/arduino/arduino-cli/internal/cli/feedback"
"github.com/arduino/arduino-cli/internal/i18n"
......@@ -35,8 +36,7 @@ func main() {
// Search for the configuration file in the command line arguments and in the environment
configFile := configuration.FindConfigFileInArgsFallbackOnEnv(os.Args)
ctx := context.Background()
ctx = context.WithValue(ctx, "config_file", configFile)
ctx := config.SetConfigFile(context.Background(), configFile)
// Read the settings from the configuration file
openReq := &rpc.ConfigurationOpenRequest{SettingsFormat: "yaml"}
......
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