/
prefs.go
148 lines (124 loc) · 3.15 KB
/
prefs.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package cmd
import (
"fmt"
"path/filepath"
"strings"
"github.com/go-ini/ini"
"github.com/urfave/cli"
"github.com/manifoldco/torus-cli/errs"
"github.com/manifoldco/torus-cli/prefs"
"github.com/manifoldco/torus-cli/ui"
)
func init() {
prefs := cli.Command{
Name: "prefs",
Usage: "Manage tool preferences",
Category: "SYSTEM",
Subcommands: []cli.Command{
{
Name: "set",
Usage: "Set the preference key to the value. If value is omitted, then it sets it to true",
ArgsUsage: "<key> <value>",
Action: func(ctx *cli.Context) error {
return setPref(ctx)
},
},
{
Name: "list",
Usage: "Show your account preferences",
Action: func(ctx *cli.Context) error {
return listPref(ctx)
},
},
},
}
Cmds = append(Cmds, prefs)
}
func listSection(name string, count int, section interface{}) error {
if count <= 0 {
return nil
}
fmt.Println(name)
fc := ini.Empty()
err := ini.ReflectFrom(fc, section)
if err != nil {
return err
}
_, err = fc.WriteTo(ui.Child(4))
return err
}
func listPref(ctx *cli.Context) error {
const loadErr = "Failed to load prefs."
preferences, err := prefs.NewPreferences()
if err != nil {
return errs.NewErrorExitError(loadErr, err)
}
filepath, _ := prefs.RcPath()
fmt.Println("\n" + filepath + "\n")
coreCount := preferences.CountFields("Core")
err = listSection("[core]", coreCount, &preferences.Core)
if err != nil {
return errs.NewErrorExitError(loadErr, err)
}
defaultsCount := preferences.CountFields("Defaults")
err = listSection("[defaults]", defaultsCount, &preferences.Defaults)
if err != nil {
return errs.NewErrorExitError(loadErr, err)
}
if defaultsCount < 1 && coreCount < 1 {
fmt.Println("No preferences set. Use 'torus prefs set' to update.")
fmt.Println("")
}
return nil
}
func setPref(ctx *cli.Context) error {
args := ctx.Args()
key := args.Get(0)
value := args.Get(1)
if len(key) < 1 || len(value) < 1 {
return errs.NewUsageExitError("Must supply a key and value", ctx)
}
if len(strings.Split(key, ".")) < 2 {
return errs.NewExitError("Key must be have at least two dot delimited segments.")
}
return setPrefByName(key, value)
}
func setPrefByName(key, value string) error {
preferences, err := prefs.NewPreferences()
if err != nil {
return errs.NewErrorExitError("Failed to load prefs.", err)
}
// Validate public key file
if key == "core.public_key_file" {
err := prefs.ValidatePublicKey(value)
if err != nil {
return err
}
value, err = filepath.Abs(value)
if err != nil {
return err
}
}
// Set value inside prefs struct
result, err := preferences.SetValue(key, value)
if err != nil {
return err
}
// Reflect struct to ini format
cfg := ini.Empty()
err = ini.ReflectFrom(cfg, &result)
if err != nil {
return errs.NewErrorExitError("Failed to save preferences.", err)
}
// Save updated ini to filePath
rcPath, err := prefs.RcPath()
if err != nil {
return errs.NewErrorExitError("Faled to save preferences.", err)
}
err = cfg.SaveTo(rcPath)
if err != nil {
return errs.NewErrorExitError("Failed to save preferences.", err)
}
fmt.Println("Preferences updated.")
return nil
}