From 36b08f1e00a3b7fd88e4c074b93fd9f9f077386e Mon Sep 17 00:00:00 2001 From: Michael Quigley Date: Tue, 9 Aug 2022 11:34:00 -0400 Subject: [PATCH] cf-based config (#23) --- cmd/zrok/controller.go | 25 +++++-------------------- controller/config.go | 18 +++++++++++++++--- etc/ctrl.yml | 9 +++++++++ go.mod | 2 ++ go.sum | 2 ++ 5 files changed, 33 insertions(+), 23 deletions(-) create mode 100644 etc/ctrl.yml diff --git a/cmd/zrok/controller.go b/cmd/zrok/controller.go index 2415df92..c6381ee8 100644 --- a/cmd/zrok/controller.go +++ b/cmd/zrok/controller.go @@ -2,11 +2,7 @@ package main import ( "github.com/openziti-test-kitchen/zrok/controller" - "github.com/openziti-test-kitchen/zrok/controller/store" - "github.com/pkg/errors" "github.com/spf13/cobra" - "strconv" - "strings" ) func init() { @@ -20,35 +16,24 @@ type controllerCommand struct { func newControllerCommand() *controllerCommand { cmd := &cobra.Command{ - Use: "controller", + Use: "controller ", Short: "Start a zrok controller", Aliases: []string{"ctrl"}, + Args: cobra.ExactArgs(1), } 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]) +func (cmd *controllerCommand) run(_ *cobra.Command, args []string) { + cfg, err := controller.LoadConfig(args[0]) if err != nil { panic(err) } - if err := controller.Run(&controller.Config{ - Endpoint: controller.EndpointConfig{ - Host: host, - Port: port, - }, - Store: &store.Config{Path: cmd.dbPath}, - }); err != nil { + if err := controller.Run(cfg); err != nil { panic(err) } } diff --git a/controller/config.go b/controller/config.go index 745f0737..2c566b85 100644 --- a/controller/config.go +++ b/controller/config.go @@ -1,10 +1,14 @@ package controller -import "github.com/openziti-test-kitchen/zrok/controller/store" +import ( + "github.com/michaelquigley/cf" + "github.com/openziti-test-kitchen/zrok/controller/store" + "github.com/pkg/errors" +) type Config struct { - Endpoint EndpointConfig - Proxy ProxyConfig + Endpoint *EndpointConfig + Proxy *ProxyConfig Store *store.Config } @@ -16,3 +20,11 @@ type EndpointConfig struct { type ProxyConfig struct { UrlTemplate string } + +func LoadConfig(path string) (*Config, error) { + cfg := &Config{} + if err := cf.BindYaml(cfg, path, cf.DefaultOptions()); err != nil { + return nil, errors.Wrapf(err, "error loading controller config '%v'", path) + } + return cfg, nil +} diff --git a/etc/ctrl.yml b/etc/ctrl.yml new file mode 100644 index 00000000..85ba7b26 --- /dev/null +++ b/etc/ctrl.yml @@ -0,0 +1,9 @@ +endpoint: + host: 0.0.0.0 + port: 10888 + +proxy: + url_template: "http://{id}.zrok.quigley.com:10111/" + +store: + path: zrok.db \ No newline at end of file diff --git a/go.mod b/go.mod index dea07cf2..b833ba19 100644 --- a/go.mod +++ b/go.mod @@ -46,6 +46,7 @@ require ( github.com/mattn/go-colorable v0.1.12 // indirect github.com/mattn/go-isatty v0.0.14 // indirect github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect + github.com/michaelquigley/cf v0.0.12 // indirect github.com/miekg/pkcs11 v1.1.1 // indirect github.com/mitchellh/go-ps v1.0.0 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect @@ -73,4 +74,5 @@ require ( golang.org/x/term v0.0.0-20220526004731-065cf7ba2467 // indirect google.golang.org/protobuf v1.28.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index e9eb683d..0bad4af4 100644 --- a/go.sum +++ b/go.sum @@ -337,6 +337,8 @@ github.com/mattn/go-sqlite3 v1.14.14/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4 github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE= github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d h1:5PJl274Y63IEHC+7izoQE9x6ikvDFZS2mDVS3drnohI= github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE= +github.com/michaelquigley/cf v0.0.12 h1:DEGncGj4gikWMSkpnAD5h7l472hjGgnaGYnT2zAS1V8= +github.com/michaelquigley/cf v0.0.12/go.mod h1:yzQJzoNRfeyB4pr5lxia1dtoS/7X96IoM5uZEIW6nzo= github.com/michaelquigley/pfxlog v0.6.9 h1:K/weH6ARu58aEDQi0ccinItvV958CeO6Ri4jqeFwd/w= github.com/michaelquigley/pfxlog v0.6.9/go.mod h1:D2vg1tPyPdSXWWkSnGk6Fomwh5b3clwVJDUh71tq8Sk= github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=