-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add command to poke the db if we need it
- Loading branch information
Showing
1 changed file
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package commands | ||
|
||
import ( | ||
"errors" | ||
"flag" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/NHAS/wag/pkg/control" | ||
"github.com/NHAS/wag/pkg/control/wagctl" | ||
) | ||
|
||
type modifyConfig struct { | ||
fs *flag.FlagSet | ||
|
||
key, value, action string | ||
|
||
socket string | ||
} | ||
|
||
func ModifyConfig() *modifyConfig { | ||
gc := &modifyConfig{ | ||
fs: flag.NewFlagSet("config", flag.ContinueOnError), | ||
} | ||
|
||
gc.fs.StringVar(&gc.socket, "socket", control.DefaultWagSocket, "Wag control socket to act on") | ||
|
||
gc.fs.StringVar(&gc.key, "key", "", "Key to set or get") | ||
gc.fs.StringVar(&gc.value, "value", "", "Value to set (put only)") | ||
|
||
gc.fs.Bool("get", false, "Get key value") | ||
gc.fs.Bool("put", false, "Set key value") | ||
|
||
return gc | ||
} | ||
|
||
func (g *modifyConfig) FlagSet() *flag.FlagSet { | ||
return g.fs | ||
} | ||
|
||
func (g *modifyConfig) Name() string { | ||
|
||
return g.fs.Name() | ||
} | ||
|
||
func (g *modifyConfig) PrintUsage() { | ||
g.fs.Usage() | ||
} | ||
|
||
func (g *modifyConfig) Check() error { | ||
g.fs.Visit(func(f *flag.Flag) { | ||
switch f.Name { | ||
case "get", "put": | ||
g.action = strings.ToLower(f.Name) | ||
} | ||
}) | ||
|
||
switch g.action { | ||
case "get": | ||
if g.value != "" { | ||
return errors.New("value supplied when getting") | ||
} | ||
default: | ||
return errors.New("Unknown flag: " + g.action) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (g *modifyConfig) Run() error { | ||
|
||
ctl := wagctl.NewControlClient(g.socket) | ||
|
||
switch g.action { | ||
case "put": | ||
err := ctl.PutDBKey(g.key, g.value) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Println("OK!") | ||
case "get": | ||
contents, err := ctl.GetDBKey(g.key) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Println(contents) | ||
} | ||
|
||
return nil | ||
} |