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