-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathconfig.go
106 lines (86 loc) · 2.24 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"bytes"
"fmt"
"os"
"sort"
"strings"
"text/template"
"gopkg.in/yaml.v2"
)
// CommandVariables respresents the config items for a command.
type CommandVariables struct {
EnvironmentVariables string
InitArguments string
ExecuteArguments string
}
// Config represents the configuration.
type Config struct {
Version string
Commands map[string]map[string]string `yaml:"commands"`
}
func loadConfig(configFilePath string) (*Config, error) {
f, err := os.ReadFile(configFilePath)
if err != nil {
return nil, err
}
var config Config
err = yaml.Unmarshal(f, &config)
if err != nil {
return nil, err
}
return &config, nil
}
func (c Config) getCommandDefinition(framework, commandType string) (string, error) {
if _, ok := c.Commands[framework]; !ok {
return "", fmt.Errorf("unknown framework '%s'", framework)
}
if _, ok := c.Commands[framework][commandType]; !ok {
return "", fmt.Errorf("unknown command type '%s'", commandType)
}
return c.Commands[framework][commandType], nil
}
func (c Config) listFrameworks() []string {
keys := []string{}
for k := range c.Commands {
keys = append(keys, k)
}
sort.Strings(keys)
return keys
}
func (c Config) listTypes(framework string) ([]string, error) {
if _, ok := c.Commands[framework]; !ok {
return []string{}, fmt.Errorf("unknown framework '%s'", framework)
}
keys := []string{}
for k := range c.Commands[framework] {
keys = append(keys, k)
}
sort.Strings(keys)
return keys, nil
}
func generateExecuteCommand(commandDefinition, environmentVariablesString string, arguments map[string][]string) (string, error) {
initArguments := ""
if _, ok := arguments["init"]; ok {
initArguments = strings.Join(arguments["init"], " ")
}
executeArguments := ""
if _, ok := arguments["execute"]; ok {
executeArguments = strings.Join(arguments["execute"], " ")
}
commandVariables := CommandVariables{
EnvironmentVariables: environmentVariablesString,
InitArguments: initArguments,
ExecuteArguments: executeArguments,
}
var buf bytes.Buffer
t, err := template.New("text").Parse(commandDefinition)
if err != nil {
return "", err
}
err = t.Execute(&buf, commandVariables)
if err != nil {
return "", err
}
return buf.String(), nil
}