|
| 1 | +package commands |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/chermehdi/egor/config" |
| 10 | + "github.com/fatih/color" |
| 11 | + "github.com/urfave/cli/v2" |
| 12 | +) |
| 13 | + |
| 14 | +func UpdateConfiguration(config *config.Config, key, value string) error { |
| 15 | + lowerKey := strings.ToLower(key) |
| 16 | + if lowerKey == "server.port" { |
| 17 | + port, err := strconv.Atoi(value) |
| 18 | + if err != nil { |
| 19 | + return err |
| 20 | + } |
| 21 | + config.Server.Port = port |
| 22 | + } else if lowerKey == "lang.default" { |
| 23 | + // suppose that the language is valid (cpp, python, java ...) |
| 24 | + config.Lang.Default = value |
| 25 | + } else if lowerKey == "author" { |
| 26 | + config.Author = value |
| 27 | + } else { |
| 28 | + // Unknow key |
| 29 | + return errors.New(fmt.Sprintf("Unknown configuration property %s", key)) |
| 30 | + } |
| 31 | + return nil |
| 32 | +} |
| 33 | + |
| 34 | +// Sets the configuration property identified by the first argument |
| 35 | +// To the value identified by the second argument. |
| 36 | +// Note: The configuration keys are not case sensitive, if a configuration key provided |
| 37 | +// is not recognized, an error is thrown |
| 38 | +func SetAction(context *cli.Context) error { |
| 39 | + argLen := context.Args().Len() |
| 40 | + if argLen != 2 { |
| 41 | + color.Red(fmt.Sprintln("Usage egor config set key value")) |
| 42 | + return errors.New(fmt.Sprintf("Expected 2 parameters, got %d", argLen)) |
| 43 | + } |
| 44 | + key, value := context.Args().Get(0), context.Args().Get(1) |
| 45 | + |
| 46 | + configuration, err := config.LoadDefaultConfiguration() |
| 47 | + if err != nil { |
| 48 | + return err |
| 49 | + } |
| 50 | + if err := UpdateConfiguration(configuration, key, value); err != nil { |
| 51 | + return err |
| 52 | + } |
| 53 | + |
| 54 | + return config.SaveConfiguration(configuration) |
| 55 | +} |
| 56 | + |
| 57 | +// Gets and prints the current configuration associted to the first argument, |
| 58 | +// or Prints all if no argument is specified |
| 59 | +func GetAction(context *cli.Context) error { |
| 60 | + argLen := context.Args().Len() |
| 61 | + if argLen > 1 { |
| 62 | + color.Red(fmt.Sprintln("Usage egor config get <key?>")) |
| 63 | + return errors.New(fmt.Sprintf("Expected at most 1 parameter, got %d", argLen)) |
| 64 | + } |
| 65 | + configuration, err := config.LoadDefaultConfiguration() |
| 66 | + if err != nil { |
| 67 | + return err |
| 68 | + } |
| 69 | + if argLen == 0 { |
| 70 | + fmt.Println("Current configuration: ") |
| 71 | + color.Green("server.port\t\t %d\n", configuration.Server.Port) |
| 72 | + color.Green("lang.default\t\t %s\n", configuration.Lang.Default) |
| 73 | + color.Green("author\t\t %s\n", configuration.Author) |
| 74 | + return nil |
| 75 | + } else { |
| 76 | + value, err := config.GetConfigurationValue(configuration, context.Args().First()) |
| 77 | + if err != nil { |
| 78 | + return err |
| 79 | + } |
| 80 | + color.Green("%s\t\t %s\n", context.Args().First(), value) |
| 81 | + return nil |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +var ConfigCommand = cli.Command{ |
| 86 | + Name: "config", |
| 87 | + Aliases: []string{"c"}, |
| 88 | + Usage: "Read/Change global configuration parameters", |
| 89 | + UsageText: "Read/Change global configuraiton parameters", |
| 90 | + Subcommands: []*cli.Command{ |
| 91 | + { |
| 92 | + Name: "set", |
| 93 | + Usage: "set config parameter", |
| 94 | + UsageText: "Sets a configuration parameter", |
| 95 | + Action: SetAction, |
| 96 | + }, |
| 97 | + { |
| 98 | + Name: "get", |
| 99 | + Usage: "get config parameter", |
| 100 | + UsageText: "Gets a configuration parameter", |
| 101 | + Action: GetAction, |
| 102 | + }, |
| 103 | + }, |
| 104 | +} |
0 commit comments