This repository has been archived by the owner on May 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
/
varstore.go
267 lines (243 loc) · 7.55 KB
/
varstore.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// Copyright © 2021 AVA Labs, Inc.
// All rights reserved.
package cmd
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/ava-labs/avash/cfg"
"github.com/spf13/cobra"
"github.com/yourbasic/radix"
)
// VarScope is a scope of the variable
type VarScope struct {
Name string
Variables map[string]string
}
// List lists the variables in the scope
func (v *VarScope) List() []string {
results := []string{}
for k := range v.Variables {
results = append(results, k)
}
return results
}
// Get gets the variable by name in the scope
func (v *VarScope) Get(varname string) (string, error) {
if variable, ok := v.Variables[varname]; ok {
return variable, nil
}
return "", fmt.Errorf("variable not found: %s", varname)
}
// Set sets the variable at a name to a value
func (v *VarScope) Set(varname string, value string) {
v.Variables[varname] = value
}
// JSON returns the json representation of the variable scope
func (v *VarScope) JSON() ([]byte, error) {
return json.MarshalIndent(v, "", " ")
}
// VarStore stores scopes of variables to store
type VarStore struct {
Stores map[string]VarScope
}
// Create will make a new variable scope
func (v *VarStore) Create(store string) error {
if _, ok := v.Stores[store]; ok {
return fmt.Errorf("store exists: %s", store)
}
v.Stores[store] = VarScope{
Name: store,
Variables: map[string]string{},
}
return nil
}
// List lists the scopes available
func (v *VarStore) List() []string {
results := []string{}
for k := range v.Stores {
results = append(results, k)
}
return results
}
// Get will retrieve the scope defined at the name passed in
func (v *VarStore) Get(store string) (VarScope, error) {
if variable, ok := v.Stores[store]; ok {
return variable, nil
}
return VarScope{}, fmt.Errorf("store not found: %s", store)
}
// VarStoreCmd represents the vars command
var VarStoreCmd = &cobra.Command{
Use: "varstore",
Short: "Tools for creating variable stores and printing variables within them.",
Long: `Tools for creating variable stores and printing variables within them. Using this
command you can create variable stores, list all variables they store, and print data
placed into these stores. Variable assigment and update is often managed by avash commands.`,
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}
// VarStoreCreateCmd will attempt to get a genesis key and send a transaction
var VarStoreCreateCmd = &cobra.Command{
Use: "create [store name]",
Short: "Creates a variable store.",
Long: `Creates a variable store. If it exists, it prints "name conflict" otherwise
it prints "store created".`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) >= 1 {
log := cfg.Config.Log
store := args[0]
if err := AvashVars.Create(store); err == nil {
log.Info("store created: " + store)
} else {
log.Error("name conflict: " + store)
}
} else {
cmd.Help()
}
},
}
// VarStoreListCmd will attempt to get a genesis key and send a transaction
var VarStoreListCmd = &cobra.Command{
Use: "list [store name]",
Short: "Lists all stores. If store provided, lists all variables in the store.",
Long: `Lists all stores. If store provided, lists all variables in the store.
If the store exists, it will print a new-line separated string of variables in
this store. If the store does not exist, it will print "store not found".`,
Run: func(cmd *cobra.Command, args []string) {
log := cfg.Config.Log
results := []string{}
if len(args) >= 1 {
if store, err := AvashVars.Get(args[0]); err == nil {
results = store.List()
} else {
log.Error("store not found:" + args[0])
}
} else {
results = AvashVars.List()
}
radix.Sort(results)
for _, v := range results {
log.Info(v)
}
},
}
// VarStorePrintCmd will attempt to get a genesis key and send a transaction
var VarStorePrintCmd = &cobra.Command{
Use: "print [store] [variable]",
Short: "Prints a variable that is within the store.",
Long: `Prints a variable that is within the store. If it doesn't exist, it prints the default JSON string "{}".`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) >= 2 {
log := cfg.Config.Log
if store, err := AvashVars.Get(args[0]); err == nil {
if v, e := store.Get(args[1]); e == nil {
log.Info(v)
} else {
log.Info("{}")
}
} else {
log.Info("{}")
}
} else {
cmd.Help()
}
},
}
// VarStoreSetCmd will attempt to get a genesis key and send a transaction
var VarStoreSetCmd = &cobra.Command{
Use: "set [store] [variable] [value]",
Short: "Sets a simple variable that within the store.",
Long: `Sets a simple variable that within the store. Store must exist. May not have spaces, even quoted. Existing values are overwritten.`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) >= 3 {
log := cfg.Config.Log
if store, err := AvashVars.Get(args[0]); err == nil {
store.Set(args[1], args[2])
log.Info("variable set: %q.%q=%q", args[0], args[1], args[2])
} else {
log.Error("store not found: " + args[0])
}
} else {
cmd.Help()
}
},
}
// VarStoreStoreDumpCmd writes the store to the filename specified in the stash
var VarStoreStoreDumpCmd = &cobra.Command{
Use: "storedump [store] [filename]",
Short: "Writes the store to a file.",
Long: `Writes the store to a file.`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) >= 2 {
log := cfg.Config.Log
if store, err := AvashVars.Get(args[0]); err == nil {
stashdir := cfg.Config.DataDir
basename := filepath.Base(args[1])
basedir := filepath.Dir(stashdir + "/" + args[1])
os.MkdirAll(basedir, os.ModePerm)
outputfile := basedir + "/" + basename
if marshalled, err := store.JSON(); err == nil {
if err := ioutil.WriteFile(outputfile, marshalled, 0755); err != nil {
log.Error("unable to write file: %s - %s", string(outputfile), err.Error())
} else {
log.Info("VarStore written to: %s", outputfile)
}
} else {
log.Error("unable to marshal: %s", err.Error())
}
} else {
log.Error("store not found: %s", args[0])
}
} else {
cmd.Help()
}
},
}
// VarStoreVarDumpCmd writes the variable to the filename specified in the stash
var VarStoreVarDumpCmd = &cobra.Command{
Use: "vardump [store] [variable] [filename]",
Short: "Writes the variable to a file.",
Long: `Writes the variable set to a file.`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) >= 3 {
log := cfg.Config.Log
if store, err := AvashVars.Get(args[0]); err == nil {
if variable, e := store.Get(args[1]); e == nil {
stashdir := cfg.Config.DataDir
basename := filepath.Base(args[2])
basedir := filepath.Dir(stashdir + "/" + args[2])
os.MkdirAll(basedir, os.ModePerm)
outputfile := basedir + "/" + basename
if err := ioutil.WriteFile(outputfile, []byte(variable), 0755); err != nil {
log.Error("unable to write file: %s - %s", string(outputfile), err.Error())
} else {
log.Info("VarStore written to: %s", outputfile)
}
} else {
log.Error("variable not found: %s -> %s", args[0], args[1])
}
} else {
log.Error("store not found: %s", args[0])
}
} else {
cmd.Help()
}
},
}
// AvashVars is the variable store.
var AvashVars VarStore
func init() {
VarStoreCmd.AddCommand(VarStoreCreateCmd)
VarStoreCmd.AddCommand(VarStoreStoreDumpCmd)
VarStoreCmd.AddCommand(VarStoreListCmd)
VarStoreCmd.AddCommand(VarStorePrintCmd)
VarStoreCmd.AddCommand(VarStoreSetCmd)
VarStoreCmd.AddCommand(VarStoreVarDumpCmd)
AvashVars = VarStore{
Stores: map[string]VarScope{},
}
}