better ctrl command encapsulation (#23)

This commit is contained in:
Michael Quigley 2022-08-08 15:50:42 -04:00
parent d6cad21f18
commit 4cd61f85ca
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62

View File

@ -10,31 +10,43 @@ import (
)
func init() {
rootCmd.AddCommand(controllerCmd)
rootCmd.AddCommand(newControllerCommand().cmd)
}
var controllerCmd = &cobra.Command{
Use: "controller <configPath>",
Short: "Start a zrok controller",
Aliases: []string{"ctrl"},
Run: func(_ *cobra.Command, args []string) {
tokens := strings.Split(endpoint, ":")
if len(tokens) != 2 {
panic(errors.Errorf("malformed endpoint '%v'", endpoint))
}
host := tokens[0]
port, err := strconv.Atoi(tokens[1])
if err != nil {
panic(err)
}
if err := controller.Run(&controller.Config{
Host: host,
Port: port,
Store: &store.Config{
Path: "zrok.db",
},
}); err != nil {
panic(err)
}
},
type controllerCommand struct {
dbPath string
cmd *cobra.Command
}
func newControllerCommand() *controllerCommand {
cmd := &cobra.Command{
Use: "controller",
Short: "Start a zrok controller",
Aliases: []string{"ctrl"},
}
ccmd := &controllerCommand{
cmd: cmd,
}
cmd.Run = ccmd.run
cmd.Flags().StringVarP(&ccmd.dbPath, "database", "d", "zrok.db", "Path to zrok controller database")
return ccmd
}
func (cmd *controllerCommand) run(_ *cobra.Command, _ []string) {
tokens := strings.Split(endpoint, ":")
if len(tokens) != 2 {
panic(errors.Errorf("malformed endpoint '%v'", endpoint))
}
host := tokens[0]
port, err := strconv.Atoi(tokens[1])
if err != nil {
panic(err)
}
if err := controller.Run(&controller.Config{
Host: host,
Port: port,
Store: &store.Config{Path: cmd.dbPath},
}); err != nil {
panic(err)
}
}