/
cmdlipconfig.go
198 lines (156 loc) · 4.49 KB
/
cmdlipconfig.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package cmdlipconfig
import (
"flag"
"fmt"
"reflect"
"strconv"
"strings"
"github.com/lippkg/lip/internal/context"
"github.com/olekukonko/tablewriter"
)
type FlagDict struct {
helpFlag bool
}
const helpMessage = `
Usage:
lip config [options]
lip config [<key> [<value>]]
Description:
Manage configuration.
- If no arguments are specified, list all configuration.
- If a key is specified, print the value of the key.
- If a key and a value are specified, set the value of the key.
Options:
-h, --help Show help.
`
func Run(ctx *context.Context, args []string) error {
flagSet := flag.NewFlagSet("cache", flag.ContinueOnError)
// Rewrite the default usage message.
flagSet.Usage = func() {
// Do nothing.
}
var flagDict FlagDict
flagSet.BoolVar(&flagDict.helpFlag, "help", false, "")
flagSet.BoolVar(&flagDict.helpFlag, "h", false, "")
if err := flagSet.Parse(args); err != nil {
return fmt.Errorf("failed to parse flags\n\t%w", err)
}
// Help flag has the highest priority.
if flagDict.helpFlag {
fmt.Print(helpMessage)
return nil
}
switch flagSet.NArg() {
case 0:
showAllConfig(ctx)
case 1:
if err := showConfig(ctx, flagSet.Arg(0)); err != nil {
return fmt.Errorf("failed to show config\n\t%w", err)
}
case 2:
if err := setConfig(ctx, flagSet.Arg(0), flagSet.Arg(1)); err != nil {
return fmt.Errorf("failed to set config\n\t%w", err)
}
default:
return fmt.Errorf("too many arguments")
}
return nil
}
func convertConfigToMap(config context.Config) map[string]interface{} {
value := reflect.ValueOf(config)
typeOfValue := value.Type()
configMap := make(map[string]interface{})
for i := 0; i < value.NumField(); i++ {
configMap[typeOfValue.Field(i).Name] = value.Field(i).Interface()
}
return configMap
}
func convertStringToType(str string, targetType reflect.Type) (interface{}, error) {
switch targetType.Kind() {
case reflect.String:
return str, nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
intValue, err := strconv.ParseInt(str, 10, targetType.Bits())
if err != nil {
return nil, err
}
value := reflect.New(targetType).Elem()
value.SetInt(intValue)
return value.Interface(), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
uintValue, err := strconv.ParseUint(str, 10, targetType.Bits())
if err != nil {
return nil, err
}
value := reflect.New(targetType).Elem()
value.SetUint(uintValue)
return value.Interface(), nil
case reflect.Float32, reflect.Float64:
floatValue, err := strconv.ParseFloat(str, targetType.Bits())
if err != nil {
return nil, err
}
value := reflect.New(targetType).Elem()
value.SetFloat(floatValue)
return value.Interface(), nil
case reflect.Bool:
boolValue, err := strconv.ParseBool(str)
if err != nil {
return nil, err
}
value := reflect.New(targetType).Elem()
value.SetBool(boolValue)
return value.Interface(), nil
default:
return nil, fmt.Errorf("unsupported type: %v", targetType)
}
}
func setConfig(ctx *context.Context, key string, value string) error {
field := reflect.ValueOf(ctx.Config()).Elem().FieldByName(key)
if !field.IsValid() {
return fmt.Errorf("no such key: %v", key)
}
if !field.CanSet() {
return fmt.Errorf("cannot set key: %v", key)
}
fieldType := field.Type()
structValueInterface, err := convertStringToType(value, fieldType)
if err != nil {
return fmt.Errorf("failed to convert value to type\n\t%w", err)
}
structValue := reflect.ValueOf(structValueInterface)
if structValue.Type().ConvertibleTo(fieldType) {
field.Set(structValue.Convert(fieldType))
} else {
return fmt.Errorf("cannot convert value to type: %v", fieldType)
}
if err := ctx.SaveConfigFile(); err != nil {
return fmt.Errorf("failed to save config file\n\t%w", err)
}
return nil
}
func showAllConfig(ctx *context.Context) {
tableData := make([][]string, 0)
for key, value := range convertConfigToMap(*ctx.Config()) {
tableData = append(tableData, []string{key, fmt.Sprintf("%v", value)})
}
tableString := &strings.Builder{}
table := tablewriter.NewWriter(tableString)
table.SetHeader([]string{
"Key", "Value",
})
for _, row := range tableData {
table.Append(row)
}
table.Render()
fmt.Print(tableString.String())
}
func showConfig(ctx *context.Context, key string) error {
configMap := convertConfigToMap(*ctx.Config())
if value, ok := configMap[key]; ok {
fmt.Printf("%v\n", value)
} else {
return fmt.Errorf("no such key: %v", key)
}
return nil
}