Skip to content

Commit 46b3bac

Browse files
mehdi.cheracher@gmail.comMehdi Cheracher
authored and
Mehdi Cheracher
committed
add basic setup for parse command
1 parent b5681bf commit 46b3bac

File tree

5 files changed

+217
-9
lines changed

5 files changed

+217
-9
lines changed

commands/parse.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package commands
2+
3+
import (
4+
"fmt"
5+
"github.com/urfave/cli/v2"
6+
)
7+
8+
var ParseCommand cli.Command = cli.Command{
9+
Name: "parse",
10+
Aliases: []string{"p"},
11+
Usage: "parse task from navigator",
12+
UsageText: "parse task from navigator",
13+
Action: func(context *cli.Context) error {
14+
fmt.Println("Hello from parse")
15+
return nil
16+
},
17+
}

config/config.go

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package config
2+
3+
import (
4+
"bytes"
5+
"gopkg.in/yaml.v2"
6+
"io/ioutil"
7+
"os"
8+
"path"
9+
)
10+
11+
// The configuration of the CLI
12+
type Config struct {
13+
Server struct {
14+
Port int `yaml:"port"`
15+
}
16+
Lang struct {
17+
Default string `yaml:"default"`
18+
}
19+
Version string `yaml:"version"`
20+
}
21+
22+
func getDefaultConfigLocation() (string, error) {
23+
configDir, err := os.UserConfigDir()
24+
if err != nil {
25+
return "", err
26+
}
27+
return path.Join(configDir, "egor.yaml"), nil
28+
}
29+
30+
func createDefaultConfiguration() *Config {
31+
return &Config{
32+
Server: struct {
33+
Port int `yaml:"port"`
34+
}{
35+
Port: 12,
36+
},
37+
Lang: struct {
38+
Default string `yaml:"default"`
39+
}{
40+
Default: "cpp",
41+
},
42+
Version: "1.0",
43+
}
44+
}
45+
46+
// This function is called when the configuration file does not exist already
47+
// This will create the configuration file in the user config dir, with a minimalistic
48+
// default configuration
49+
func saveDefaultConfiguration() error {
50+
location, err := getDefaultConfigLocation()
51+
if err != nil {
52+
return err
53+
}
54+
var buffer bytes.Buffer
55+
encoder := yaml.NewEncoder(&buffer)
56+
err = encoder.Encode(createDefaultConfiguration())
57+
if err != nil {
58+
return err
59+
}
60+
return ioutil.WriteFile(location, buffer.Bytes(), 0644)
61+
}
62+
63+
// Returns the Configuration object associated with
64+
// the path given as a parameter
65+
func LoadConfiguration(location string) (*Config, error) {
66+
file, err := os.Open(location)
67+
if err != nil {
68+
return nil, err
69+
}
70+
defer file.Close()
71+
var config Config
72+
decoder := yaml.NewDecoder(file)
73+
err = decoder.Decode(&config)
74+
if err != nil {
75+
return nil, err
76+
}
77+
return &config, nil
78+
}
79+
80+
// Returns the Configuration object associated with
81+
// the default configuration location
82+
func LoadDefaultConfiguration() (*Config, error) {
83+
location, err := getDefaultConfigLocation()
84+
if err != nil {
85+
return nil, err
86+
}
87+
if _, err := os.Stat(location); err != nil {
88+
if os.IsNotExist(err) {
89+
saveDefaultConfiguration()
90+
}
91+
}
92+
return LoadConfiguration(location)
93+
}

config/config_test.go

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package config
2+
3+
import (
4+
"bytes"
5+
"github.com/stretchr/testify/assert"
6+
"gopkg.in/yaml.v2"
7+
"io/ioutil"
8+
"os"
9+
"path"
10+
"testing"
11+
)
12+
13+
func getDefaultConfiguration() *Config {
14+
return &Config{
15+
Server: struct {
16+
Port int `yaml:"port"`
17+
}{Port: 1200},
18+
Lang: struct {
19+
Default string `yaml:"default"`
20+
}{Default: "cpp"},
21+
}
22+
}
23+
func getConfigPath() string {
24+
tempDir := os.TempDir()
25+
return path.Join(tempDir, "config.yaml")
26+
}
27+
28+
func createFakeConfigFile() error {
29+
configPath := getConfigPath()
30+
31+
var buffer bytes.Buffer
32+
configuration := getDefaultConfiguration()
33+
34+
encoder := yaml.NewEncoder(&buffer)
35+
err := encoder.Encode(configuration)
36+
if err != nil {
37+
return err
38+
}
39+
// write the fake configuration yaml to the file
40+
err = ioutil.WriteFile(configPath, buffer.Bytes(), 0644)
41+
if err != nil {
42+
return err
43+
}
44+
return nil
45+
}
46+
47+
func TestLoadConfiguration(t *testing.T) {
48+
createFakeConfigFile()
49+
defer deleteFakeConfigFile()
50+
config, err := LoadConfiguration(getConfigPath())
51+
if err != nil {
52+
t.Error(err)
53+
}
54+
assert.Equal(t, config.Lang.Default, getDefaultConfiguration().Lang.Default)
55+
assert.Equal(t, config.Server.Port, getDefaultConfiguration().Server.Port)
56+
}
57+
58+
func deleteFakeConfigFile() error {
59+
configPath := getConfigPath()
60+
return os.Remove(configPath)
61+
}

go.mod

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/chermehdi/egor
33
go 1.13
44

55
require (
6-
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
7-
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d // indirect
8-
gopkg.in/alecthomas/kingpin.v2 v2.2.6 // indirect
6+
github.com/stretchr/testify v1.4.0
7+
github.com/urfave/cli/v2 v2.1.1
8+
gopkg.in/yaml.v2 v2.2.2
99
)

main.go

+43-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,51 @@
11
package main
22

33
import (
4-
"fmt"
4+
"bytes"
5+
"github.com/chermehdi/egor/commands"
6+
"github.com/chermehdi/egor/config"
7+
"github.com/urfave/cli/v2"
8+
"html/template"
9+
"log"
510
"os"
6-
7-
"github.com/chermehdi/egor/parse"
811
)
912

13+
const Egor = `
14+
______
15+
/ ____/___ _____ _____
16+
/ __/ / __ / __ \/ ___/
17+
/ /___/ /_/ / /_/ / /
18+
/_____/\__, /\____/_/
19+
/____/ version: {{ .Version }}
20+
|------------------------------------>>
21+
`
22+
23+
var EgorTemplate, _ = template.New("egor").Parse(Egor)
24+
1025
func main() {
11-
parser := parse.NewParser(os.Args)
12-
options := parser.Parse()
13-
fmt.Printf("%v\n", options)
26+
var egor bytes.Buffer
27+
configuration, err := config.LoadDefaultConfiguration()
28+
29+
if err != nil {
30+
log.Fatal(err)
31+
}
32+
33+
err = EgorTemplate.Execute(&egor, configuration)
34+
if err != nil {
35+
log.Fatal(err)
36+
}
37+
38+
app := &cli.App{
39+
Name: egor.String(),
40+
Description: "Competitive programming helper CLI",
41+
UsageText: "Run egor <subcommand> [--flags]*",
42+
Commands: []*cli.Command{
43+
&commands.ParseCommand,
44+
},
45+
}
46+
47+
err = app.Run(os.Args)
48+
if err != nil {
49+
log.Fatal(err)
50+
}
1451
}

0 commit comments

Comments
 (0)