Skip to content

Add new commands to edit config files #1082

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions cli/config/add.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// This file is part of arduino-cli.
//
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-cli.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to [email protected].

package config

import (
"os"
"reflect"

"github.com/arduino/arduino-cli/cli/errorcodes"
"github.com/arduino/arduino-cli/cli/feedback"
"github.com/arduino/arduino-cli/configuration"
"github.com/spf13/cobra"
)

func initAddCommand() *cobra.Command {
addCommand := &cobra.Command{
Use: "add",
Short: "Adds one or more values to a setting.",
Long: "Adds one or more values to a setting.",
Example: "" +
" " + os.Args[0] + " config add board_manager.additional_urls https://example.com/package_example_index.json\n" +
" " + os.Args[0] + " config add board_manager.additional_urls https://example.com/package_example_index.json https://another-url.com/package_another_index.json\n",
Args: cobra.MinimumNArgs(2),
Run: runAddCommand,
}
return addCommand
}

func runAddCommand(cmd *cobra.Command, args []string) {
key := args[0]
kind, err := typeOf(key)
if err != nil {
feedback.Error(err)
os.Exit(errorcodes.ErrGeneric)
}

if kind != reflect.Slice {
feedback.Errorf("The key '%v' is not a list of items, can't add to it.\nMaybe use 'config set'?", key)
os.Exit(errorcodes.ErrGeneric)
}

v := configuration.Settings.GetStringSlice(key)
v = append(v, args[1:]...)
configuration.Settings.Set(key, v)

if err := configuration.Settings.WriteConfig(); err != nil {
feedback.Errorf("Can't write config file: %v", err)
os.Exit(errorcodes.ErrGeneric)
}
}
4 changes: 4 additions & 0 deletions cli/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ func NewCommand() *cobra.Command {
Example: " " + os.Args[0] + " config init",
}

configCommand.AddCommand(initAddCommand())
configCommand.AddCommand(initDeleteCommand())
configCommand.AddCommand(initDumpCmd())
configCommand.AddCommand(initInitCommand())
configCommand.AddCommand(initRemoveCommand())
configCommand.AddCommand(initSetCommand())

return configCommand
}
70 changes: 70 additions & 0 deletions cli/config/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// This file is part of arduino-cli.
//
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-cli.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to [email protected].

package config

import (
"os"
"strings"

"github.com/arduino/arduino-cli/cli/errorcodes"
"github.com/arduino/arduino-cli/cli/feedback"
"github.com/arduino/arduino-cli/configuration"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func initDeleteCommand() *cobra.Command {
addCommand := &cobra.Command{
Use: "delete",
Short: "Deletes a settings key and all its sub keys.",
Long: "Deletes a settings key and all its sub keys.",
Example: "" +
" " + os.Args[0] + " config delete board_manager\n" +
" " + os.Args[0] + " config delete board_manager.additional_urls",
Args: cobra.ExactArgs(1),
Run: runDeleteCommand,
}
return addCommand
}

func runDeleteCommand(cmd *cobra.Command, args []string) {
toDelete := args[0]

keys := []string{}
exists := false
for _, v := range configuration.Settings.AllKeys() {
if !strings.HasPrefix(v, toDelete) {
keys = append(keys, v)
continue
}
exists = true
}

if !exists {
feedback.Errorf("Settings key doesn't exist")
os.Exit(errorcodes.ErrGeneric)
}

updatedSettings := viper.New()
for _, k := range keys {
updatedSettings.Set(k, configuration.Settings.Get(k))
}

if err := updatedSettings.WriteConfigAs(configuration.Settings.ConfigFileUsed()); err != nil {
feedback.Errorf("Can't write config file: %v", err)
os.Exit(errorcodes.ErrGeneric)
}
}
4 changes: 2 additions & 2 deletions cli/config/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ func initInitCommand() *cobra.Command {
Long: "Creates or updates the configuration file in the data directory or custom directory with the current configuration settings.",
Example: "" +
" # Writes current configuration to the configuration file in the data directory.\n" +
" " + os.Args[0] + " config init" +
" " + os.Args[0] + " config init --dest-dir /home/user/MyDirectory" +
" " + os.Args[0] + " config init\n" +
" " + os.Args[0] + " config init --dest-dir /home/user/MyDirectory\n" +
" " + os.Args[0] + " config init --dest-file /home/user/MyDirectory/my_settings.yaml",
Args: cobra.NoArgs,
Run: runInitCommand,
Expand Down
72 changes: 72 additions & 0 deletions cli/config/remove.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// This file is part of arduino-cli.
//
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-cli.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to [email protected].

package config

import (
"os"
"reflect"

"github.com/arduino/arduino-cli/cli/errorcodes"
"github.com/arduino/arduino-cli/cli/feedback"
"github.com/arduino/arduino-cli/configuration"
"github.com/spf13/cobra"
)

func initRemoveCommand() *cobra.Command {
addCommand := &cobra.Command{
Use: "remove",
Short: "Removes one or more values from a setting.",
Long: "Removes one or more values from a setting.",
Example: "" +
" " + os.Args[0] + " config remove board_manager.additional_urls https://example.com/package_example_index.json\n" +
" " + os.Args[0] + " config remove board_manager.additional_urls https://example.com/package_example_index.json https://another-url.com/package_another_index.json\n",
Args: cobra.MinimumNArgs(2),
Run: runRemoveCommand,
}
return addCommand
}

func runRemoveCommand(cmd *cobra.Command, args []string) {
key := args[0]
kind, err := typeOf(key)
if err != nil {
feedback.Error(err)
os.Exit(errorcodes.ErrGeneric)
}

if kind != reflect.Slice {
feedback.Errorf("The key '%v' is not a list of items, can't remove from it.\nMaybe use 'config delete'?", key)
os.Exit(errorcodes.ErrGeneric)
}

mappedValues := map[string]bool{}
for _, v := range configuration.Settings.GetStringSlice(key) {
mappedValues[v] = true
}
for _, arg := range args[1:] {
delete(mappedValues, arg)
}
values := []string{}
for k := range mappedValues {
values = append(values, k)
}
configuration.Settings.Set(key, values)

if err := configuration.Settings.WriteConfig(); err != nil {
feedback.Errorf("Can't write config file: %v", err)
os.Exit(errorcodes.ErrGeneric)
}
}
79 changes: 79 additions & 0 deletions cli/config/set.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// This file is part of arduino-cli.
//
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-cli.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to [email protected].

package config

import (
"os"
"reflect"
"strconv"

"github.com/arduino/arduino-cli/cli/errorcodes"
"github.com/arduino/arduino-cli/cli/feedback"
"github.com/arduino/arduino-cli/configuration"
"github.com/spf13/cobra"
)

func initSetCommand() *cobra.Command {
addCommand := &cobra.Command{
Use: "set",
Short: "Sets a setting value.",
Long: "Sets a setting value.",
Example: "" +
" " + os.Args[0] + " config set logging.level trace\n" +
" " + os.Args[0] + " config set logging.file my-log.txt\n" +
" " + os.Args[0] + " config set sketch.always_export_binaries true\n" +
" " + os.Args[0] + " config set board_manager.additional_urls https://example.com/package_example_index.json https://another-url.com/package_another_index.json",
Args: cobra.MinimumNArgs(2),
Run: runSetCommand,
}
return addCommand
}

func runSetCommand(cmd *cobra.Command, args []string) {
key := args[0]
kind, err := typeOf(key)
if err != nil {
feedback.Error(err)
os.Exit(errorcodes.ErrGeneric)
}

if kind != reflect.Slice && len(args) > 2 {
feedback.Errorf("Can't set multiple values in key %v", key)
os.Exit(errorcodes.ErrGeneric)
}

var value interface{}
switch kind {
case reflect.Slice:
value = args[1:]
case reflect.String:
value = args[1]
case reflect.Bool:
var err error
value, err = strconv.ParseBool(args[1])
if err != nil {
feedback.Errorf("error parsing value: %v", err)
os.Exit(errorcodes.ErrGeneric)
}
}

configuration.Settings.Set(key, value)

if err := configuration.Settings.WriteConfig(); err != nil {
feedback.Errorf("Writing config file: %v", err)
os.Exit(errorcodes.ErrGeneric)
}
}
44 changes: 44 additions & 0 deletions cli/config/validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// This file is part of arduino-cli.
//
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-cli.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to [email protected].

package config

import (
"fmt"
"reflect"
)

var validMap = map[string]reflect.Kind{
"board_manager.additional_urls": reflect.Slice,
"daemon.port": reflect.String,
"directories.data": reflect.String,
"directories.downloads": reflect.String,
"directories.user": reflect.String,
"library.enable_unsafe_install": reflect.Bool,
"logging.file": reflect.String,
"logging.format": reflect.String,
"logging.level": reflect.String,
"sketch.always_export_binaries": reflect.Bool,
"telemetry.addr": reflect.String,
"telemetry.enabled": reflect.Bool,
}

func typeOf(key string) (reflect.Kind, error) {
t, ok := validMap[key]
if !ok {
return reflect.Invalid, fmt.Errorf("Settings key doesn't exist")
}
return t, nil
}
Loading