|
| 1 | +// This file is part of arduino-cli. |
| 2 | +// |
| 3 | +// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) |
| 4 | +// |
| 5 | +// This software is released under the GNU General Public License version 3, |
| 6 | +// which covers the main part of arduino-cli. |
| 7 | +// The terms of this license can be found at: |
| 8 | +// https://www.gnu.org/licenses/gpl-3.0.en.html |
| 9 | +// |
| 10 | +// You can be released from the requirements of the above licenses by purchasing |
| 11 | +// a commercial license. Buying such a license is mandatory if you want to |
| 12 | +// modify or otherwise use the software for commercial activities involving the |
| 13 | +// Arduino software without disclosing the source code of your own applications. |
| 14 | +// To purchase a commercial license, send an email to [email protected]. |
| 15 | + |
| 16 | +package config |
| 17 | + |
| 18 | +import ( |
| 19 | + "os" |
| 20 | + "reflect" |
| 21 | + |
| 22 | + "github.com/arduino/arduino-cli/cli/errorcodes" |
| 23 | + "github.com/arduino/arduino-cli/cli/feedback" |
| 24 | + "github.com/arduino/arduino-cli/configuration" |
| 25 | + "github.com/spf13/cobra" |
| 26 | +) |
| 27 | + |
| 28 | +func initRemoveCommand() *cobra.Command { |
| 29 | + addCommand := &cobra.Command{ |
| 30 | + Use: "remove", |
| 31 | + Short: "Removes one or more values from a setting.", |
| 32 | + Long: "Removes one or more values from a setting.", |
| 33 | + Example: "" + |
| 34 | + " " + os.Args[0] + " config remove board_manager.additional_urls https://example.com/package_example_index.json\n" + |
| 35 | + " " + 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", |
| 36 | + Args: cobra.MinimumNArgs(2), |
| 37 | + Run: runRemoveCommand, |
| 38 | + } |
| 39 | + return addCommand |
| 40 | +} |
| 41 | + |
| 42 | +func runRemoveCommand(cmd *cobra.Command, args []string) { |
| 43 | + key := args[0] |
| 44 | + kind, err := typeOf(key) |
| 45 | + if err != nil { |
| 46 | + feedback.Error(err) |
| 47 | + os.Exit(errorcodes.ErrGeneric) |
| 48 | + } |
| 49 | + |
| 50 | + if kind != reflect.Slice { |
| 51 | + feedback.Errorf("The key '%v' is not a list of items, can't remove from it.\nMaybe use 'config delete'?", key) |
| 52 | + os.Exit(errorcodes.ErrGeneric) |
| 53 | + } |
| 54 | + |
| 55 | + mappedValues := map[string]bool{} |
| 56 | + for _, v := range configuration.Settings.GetStringSlice(key) { |
| 57 | + mappedValues[v] = true |
| 58 | + } |
| 59 | + for _, arg := range args[1:] { |
| 60 | + delete(mappedValues, arg) |
| 61 | + } |
| 62 | + values := []string{} |
| 63 | + for k := range mappedValues { |
| 64 | + values = append(values, k) |
| 65 | + } |
| 66 | + configuration.Settings.Set(key, values) |
| 67 | + |
| 68 | + if err := configuration.Settings.WriteConfig(); err != nil { |
| 69 | + feedback.Errorf("Can't write config file: %v", err) |
| 70 | + os.Exit(errorcodes.ErrGeneric) |
| 71 | + } |
| 72 | +} |
0 commit comments