This repository has been archived by the owner on Jul 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_config.go
87 lines (67 loc) · 1.74 KB
/
cmd_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
package main
import (
"encoding/json"
"fmt"
"os"
"github.com/exograd/go-program"
)
func addConfigCommands() {
var c *program.Command
// show-config
c = p.AddCommand("show-config", "print the configuration",
cmdShowConfig)
c.AddFlag("e", "entries",
"show a list of entries instead of the entire configuration")
// get-config
c = p.AddCommand("get-config",
"extract a value from the configuration and print it",
cmdGetConfig)
c.AddArgument("name", "the name of the entry")
// set-config
c = p.AddCommand("set-config", "set a value in the configuration",
cmdSetConfig)
c.AddArgument("name", "the name of the entry")
c.AddArgument("value", "the value of the entry")
}
func cmdShowConfig(p *program.Program) {
if p.IsOptionSet("entries") {
var names []string
for _, e := range ConfigEntries {
names = append(names, e.Name)
}
table := NewTable([]string{"name", "value"})
for _, name := range names {
value, err := app.Config.GetEntry(name)
if err != nil {
p.Error("cannot read entry %q: %v", name, err)
continue
}
table.AddRow([]interface{}{name, value})
}
table.Write()
} else {
encoder := json.NewEncoder(os.Stdout)
encoder.SetIndent("", " ")
if err := encoder.Encode(app.Config); err != nil {
p.Fatal("cannot encode configuration: %v", err)
}
}
}
func cmdGetConfig(p *program.Program) {
name := p.ArgumentValue("name")
value, err := app.Config.GetEntry(name)
if err != nil {
p.Fatal("%v", err)
}
fmt.Printf("%s\n", value)
}
func cmdSetConfig(p *program.Program) {
name := p.ArgumentValue("name")
value := p.ArgumentValue("value")
if err := app.Config.SetEntry(name, value); err != nil {
p.Fatal("%v", err)
}
if err := app.Config.Write(); err != nil {
p.Fatal("%v", err)
}
}