Skip to content

Commit 7445bcf

Browse files
Mehdi Cheracherchermehdi
Mehdi Cheracher
authored andcommitted
Complete configuration command, fixes 2
1 parent 106bd10 commit 7445bcf

File tree

4 files changed

+45
-11
lines changed

4 files changed

+45
-11
lines changed

commands/config.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func GetAction(context *cli.Context) error {
7070
fmt.Println("Current configuration: ")
7171
color.Green("server.port\t\t %d\n", configuration.Server.Port)
7272
color.Green("lang.default\t\t %s\n", configuration.Lang.Default)
73-
color.Green("author\t\t %s\n", configuration.Author)
73+
color.Green("author \t\t %s\n", configuration.Author)
7474
return nil
7575
} else {
7676
value, err := config.GetConfigurationValue(configuration, context.Args().First())

config/config.go

+25-5
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@ package config
22

33
import (
44
"bytes"
5-
"gopkg.in/yaml.v2"
5+
"errors"
6+
"fmt"
67
"io/ioutil"
78
"os"
89
"path"
10+
"strconv"
11+
"strings"
12+
13+
"gopkg.in/yaml.v2"
914
)
1015

1116
// The configuration of the CLI
@@ -34,7 +39,7 @@ func createDefaultConfiguration() *Config {
3439
Server: struct {
3540
Port int `yaml:"port"`
3641
}{
37-
Port: 12,
42+
Port: 1200,
3843
},
3944
Lang: struct {
4045
Default string `yaml:"default"`
@@ -49,14 +54,14 @@ func createDefaultConfiguration() *Config {
4954
// This function is called when the configuration file does not exist already
5055
// This will create the configuration file in the user config dir, with a minimalistic
5156
// default configuration
52-
func saveDefaultConfiguration() error {
57+
func SaveConfiguration(config *Config) error {
5358
location, err := getDefaultConfigLocation()
5459
if err != nil {
5560
return err
5661
}
5762
var buffer bytes.Buffer
5863
encoder := yaml.NewEncoder(&buffer)
59-
err = encoder.Encode(createDefaultConfiguration())
64+
err = encoder.Encode(config)
6065
if err != nil {
6166
return err
6267
}
@@ -89,10 +94,25 @@ func LoadDefaultConfiguration() (*Config, error) {
8994
}
9095
if _, err := os.Stat(location); err != nil {
9196
if os.IsNotExist(err) {
92-
if err := saveDefaultConfiguration(); err != nil {
97+
config := createDefaultConfiguration()
98+
if err := SaveConfiguration(config); err != nil {
9399
return nil, err
94100
}
95101
}
96102
}
97103
return LoadConfiguration(location)
98104
}
105+
106+
// Gets the configuration value associated with the given key
107+
func GetConfigurationValue(config *Config, key string) (string, error) {
108+
lowerKey := strings.ToLower(key)
109+
if lowerKey == "server.port" {
110+
return strconv.Itoa(config.Server.Port), nil
111+
} else if lowerKey == "lang.default" {
112+
return config.Lang.Default, nil
113+
} else if lowerKey == "author" {
114+
return config.Author, nil
115+
} else {
116+
return "", errors.New(fmt.Sprintf("Unknown config key %s", key))
117+
}
118+
}

config/config_test.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ package config
22

33
import (
44
"bytes"
5-
"github.com/stretchr/testify/assert"
6-
"gopkg.in/yaml.v2"
75
"io/ioutil"
86
"os"
97
"path"
108
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
"gopkg.in/yaml.v2"
1112
)
1213

1314
func getDefaultConfiguration() *Config {
@@ -59,3 +60,14 @@ func deleteFakeConfigFile() {
5960
configPath := getConfigPath()
6061
_ = os.Remove(configPath)
6162
}
63+
64+
func TestGetConfigurationValue(t *testing.T) {
65+
config := createDefaultConfiguration()
66+
67+
value, err := GetConfigurationValue(config, "server.port")
68+
assert.NoError(t, err, "No error should be thrown when getting an existing key")
69+
assert.Equal(t, value, "1200")
70+
71+
_, err = GetConfigurationValue(config, "unknown.key")
72+
assert.Error(t, err, "An error is returned if the configuration key is not known")
73+
}

main.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ package main
22

33
import (
44
"bytes"
5-
"github.com/chermehdi/egor/commands"
6-
"github.com/chermehdi/egor/config"
7-
"github.com/urfave/cli/v2"
85
"html/template"
96
"log"
107
"os"
8+
9+
"github.com/chermehdi/egor/commands"
10+
"github.com/chermehdi/egor/config"
11+
"github.com/urfave/cli/v2"
1112
)
1213

1314
const Egor = `
@@ -42,6 +43,7 @@ func main() {
4243
UsageText: "Run egor <subcommand> [--flags]*",
4344
Commands: []*cli.Command{
4445
&commands.ParseCommand,
46+
&commands.ConfigCommand,
4547
},
4648
}
4749

0 commit comments

Comments
 (0)