|
| 1 | +// This file is part of libraries-repository-engine. |
| 2 | +// |
| 3 | +// Copyright 2021 ARDUINO SA (http://www.arduino.cc/) |
| 4 | +// |
| 5 | +// This program is free software: you can redistribute it and/or modify |
| 6 | +// it under the terms of the GNU Affero General Public License as published |
| 7 | +// by the Free Software Foundation, either version 3 of the License, or |
| 8 | +// (at your option) any later version. |
| 9 | +// |
| 10 | +// This program is distributed in the hope that it will be useful, |
| 11 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +// GNU Affero General Public License for more details. |
| 14 | +// |
| 15 | +// You should have received a copy of the GNU Affero General Public License |
| 16 | +// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 17 | +// |
| 18 | +// You can be released from the requirements of the above licenses by purchasing |
| 19 | +// a commercial license. Buying such a license is mandatory if you want to |
| 20 | +// modify or otherwise use the software for commercial activities involving the |
| 21 | +// Arduino software without disclosing the source code of your own applications. |
| 22 | +// To purchase a commercial license, send an email to [email protected]. |
| 23 | + |
| 24 | +// Package cli defines the command line interface. |
| 25 | +package cli |
| 26 | + |
| 27 | +import ( |
| 28 | + "os" |
| 29 | + |
| 30 | + "github.com/arduino/libraries-repository-engine/internal/feedback" |
| 31 | + "github.com/spf13/cobra" |
| 32 | +) |
| 33 | + |
| 34 | +// rootCmd defines the base CLI command. |
| 35 | +var rootCmd = &cobra.Command{ |
| 36 | + Use: "libraries-repository-engine", |
| 37 | + Long: "The tool for managing the Arduino Library Manager content.", |
| 38 | + CompletionOptions: cobra.CompletionOptions{ |
| 39 | + DisableDefaultCmd: true, |
| 40 | + }, |
| 41 | + Aliases: []string{"sync"}, |
| 42 | +} |
| 43 | + |
| 44 | +func init() { |
| 45 | + rootCmd.PersistentFlags().String("config-file", "config.json", "Configuration file path") |
| 46 | +} |
| 47 | + |
| 48 | +// Execute adds all child commands to the root command and sets flags appropriately. |
| 49 | +func Execute() error { |
| 50 | + // Support the old `libraries-repository-engine [CONFIG_FILE [REGISTRY_FILE]]` interface. |
| 51 | + func() { |
| 52 | + if len(os.Args) > 1 { |
| 53 | + if os.Args[1][0] == '-' { |
| 54 | + // The argument is a flag, so assume the new interface. |
| 55 | + return |
| 56 | + } |
| 57 | + |
| 58 | + for _, command := range rootCmd.Commands() { |
| 59 | + for _, alias := range append(command.Aliases, command.Name(), "help") { // Hacky to check "help" redundantly, but whatever. |
| 60 | + if os.Args[1] == alias { |
| 61 | + // The argument is a registered subcommand, so assume the new interface. |
| 62 | + return |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + // The argument is not a registered subcommand, so assume it is the CONFIG_FILE positional argument of the old interface. |
| 68 | + rootCmd.PersistentFlags().Set("config-file", os.Args[1]) // Transfer the argument to the new interface's flag. |
| 69 | + os.Args = append([]string{os.Args[0]}, os.Args[2:]...) // Remove the argument. |
| 70 | + } |
| 71 | + |
| 72 | + feedback.Warning( |
| 73 | + `Using deprecated command line syntax. New syntax: |
| 74 | +libraries-repository-engine sync [--config-file=CONFIG_FILE] [REGISTRY_FILE] |
| 75 | +`, |
| 76 | + ) |
| 77 | + // Set the subcommand to the root's alias. |
| 78 | + os.Args = append([]string{os.Args[0], rootCmd.Aliases[0]}, os.Args[1:]...) |
| 79 | + }() |
| 80 | + |
| 81 | + return rootCmd.Execute() |
| 82 | +} |
0 commit comments