mirror of
https://github.com/openziti/zrok.git
synced 2024-11-07 16:54:23 +01:00
better ctrl command encapsulation (#23)
This commit is contained in:
parent
d6cad21f18
commit
4cd61f85ca
@ -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)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user