Skip to content

Commit 106bd10

Browse files
author
Mehdi Cheracher
committed
add config command
1 parent bdb62b1 commit 106bd10

File tree

2 files changed

+143
-0
lines changed

2 files changed

+143
-0
lines changed

commands/config.go

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
}

commands/config_test.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package commands
2+
3+
import (
4+
"testing"
5+
6+
"github.com/chermehdi/egor/config"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
// TODO(chermehdi): This probably should be a test fixture.
11+
func getDefaultConfiguration() *config.Config {
12+
return &config.Config{
13+
Server: struct {
14+
Port int `yaml:"port"`
15+
}{Port: 1200},
16+
Lang: struct {
17+
Default string `yaml:"default"`
18+
}{Default: "cpp"},
19+
}
20+
}
21+
22+
func TestSetConfiguration(t *testing.T) {
23+
config := getDefaultConfiguration()
24+
25+
UpdateConfiguration(config, "server.port", "1245")
26+
27+
assert.Equal(t, config.Server.Port, 1245)
28+
assert.Equal(t, config.Lang.Default, "cpp")
29+
30+
UpdateConfiguration(config, "lang.default", "java")
31+
assert.Equal(t, config.Lang.Default, "java")
32+
}
33+
34+
func TestSetConfigurationUnknownKey(t *testing.T) {
35+
config := getDefaultConfiguration()
36+
err := UpdateConfiguration(config, "unkown.key", "123")
37+
38+
assert.Error(t, err, "Error should be returned if the key is unknown")
39+
}

0 commit comments

Comments
 (0)