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 0453677

Browse filesBrowse files
committed
Use custom type to avoid context-values collisions
1 parent 4c50868 commit 0453677
Copy full SHA for 0453677

File tree

Expand file treeCollapse file tree

4 files changed

+21
-5
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+21
-5
lines changed

‎internal/cli/cli.go

Copy file name to clipboardExpand all lines: internal/cli/cli.go
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func NewCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command {
117117
preRun(verbose, outputFormat, logLevel, logFile, logFormat, noColor, settings)
118118

119119
// Log the configuration file used
120-
if configFile := ctx.Value("config_file").(string); configFile != "" {
120+
if configFile := config.GetConfigFile(ctx); configFile != "" {
121121
logrus.Infof("Using config file: %s", configFile)
122122
} else {
123123
logrus.Info("Config file not found, using default values")

‎internal/cli/config/config.go

Copy file name to clipboardExpand all lines: internal/cli/config/config.go
+17-1Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,22 @@ func getAllArraySettingsKeys(ctx context.Context, srv rpc.ArduinoCoreServiceServ
6464
return arrayKeys
6565
}
6666

67+
type ctxValue string
68+
69+
// GetConfigFile returns the configuration file path from the context
70+
func GetConfigFile(ctx context.Context) string {
71+
res := ctx.Value(ctxValue("config_file"))
72+
if res == nil {
73+
return ""
74+
}
75+
return res.(string)
76+
}
77+
78+
// SetConfigFile sets the configuration file path in the context
79+
func SetConfigFile(ctx context.Context, configFile string) context.Context {
80+
return context.WithValue(ctx, ctxValue("config_file"), configFile)
81+
}
82+
6783
func saveConfiguration(ctx context.Context, srv rpc.ArduinoCoreServiceServer) {
6884
var outConfig []byte
6985
if res, err := srv.ConfigurationSave(ctx, &rpc.ConfigurationSaveRequest{SettingsFormat: "yaml"}); err != nil {
@@ -72,7 +88,7 @@ func saveConfiguration(ctx context.Context, srv rpc.ArduinoCoreServiceServer) {
7288
outConfig = []byte(res.GetEncodedSettings())
7389
}
7490

75-
configFile := ctx.Value("config_file").(string)
91+
configFile := GetConfigFile(ctx)
7692
if err := paths.New(configFile).WriteFile(outConfig); err != nil {
7793
feedback.Fatal(tr("Error writing to file: %v", err), feedback.ErrGeneric)
7894
}

‎internal/cli/config/init.go

Copy file name to clipboardExpand all lines: internal/cli/config/init.go
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func runInitCommand(srv rpc.ArduinoCoreServiceServer) {
8383
configFileAbsPath = configFileDir.Join(defaultFileName)
8484

8585
default:
86-
configFileAbsPath = paths.New(ctx.Value("config_file").(string))
86+
configFileAbsPath = paths.New(GetConfigFile(ctx))
8787
configFileDir = configFileAbsPath.Parent()
8888
}
8989

‎main.go

Copy file name to clipboardExpand all lines: main.go
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
"github.com/arduino/arduino-cli/commands"
2424
"github.com/arduino/arduino-cli/internal/cli"
25+
"github.com/arduino/arduino-cli/internal/cli/config"
2526
"github.com/arduino/arduino-cli/internal/cli/configuration"
2627
"github.com/arduino/arduino-cli/internal/cli/feedback"
2728
"github.com/arduino/arduino-cli/internal/i18n"
@@ -35,8 +36,7 @@ func main() {
3536

3637
// Search for the configuration file in the command line arguments and in the environment
3738
configFile := configuration.FindConfigFileInArgsFallbackOnEnv(os.Args)
38-
ctx := context.Background()
39-
ctx = context.WithValue(ctx, "config_file", configFile)
39+
ctx := config.SetConfigFile(context.Background(), configFile)
4040

4141
// Read the settings from the configuration file
4242
openReq := &rpc.ConfigurationOpenRequest{SettingsFormat: "yaml"}

0 commit comments

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