From 14e437b11e79d7a08f22db20dbd9ba76a18a1ad6 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Tue, 14 Nov 2023 14:45:55 +0100 Subject: [PATCH] Add settings help for healthchecks Signed-off-by: Knut Ahlers --- README.md | 12 +++++++++++- cli_checkhelp.go | 42 ++++++++++++++++++++++++++++++++++++++++++ go.mod | 9 ++++++++- go.sum | 20 ++++++++++++++++++-- main.go | 14 +++++++++++++- 5 files changed, 92 insertions(+), 5 deletions(-) create mode 100644 cli_checkhelp.go diff --git a/README.md b/README.md index 5d15d34..810a1b3 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,15 @@ Usage of ipt-loadbalancer: -e, --enable-managed-chain Modify PREROUTING / POSTROUTING chain to contain a jump to managed chain --log-level string Log level (debug, info, warn, error, fatal) (default "info") --version Prints current version and exits + +# ipt-loadbalancer help +Supported sub-commands are: + checkhelp Display available settings for a check + +# ipt-loadbalancer checkhelp http +Setting Default Description +code 200 HTTP Status-Code to expect from the request +... ``` ### Main Configuration File @@ -47,7 +56,8 @@ healthCheck: # in parallel interval: 2s # Settings defines parameters for the given health-check and are - # individual to the type. + # individual to the type. See the `checkhelp ` subcommand for + # all supported settings and their default values. settings: insecureTLS: true path: /healthz diff --git a/cli_checkhelp.go b/cli_checkhelp.go new file mode 100644 index 0000000..cdc5455 --- /dev/null +++ b/cli_checkhelp.go @@ -0,0 +1,42 @@ +package main + +import ( + "fmt" + + "git.luzifer.io/luzifer/ipt-loadbalancer/pkg/healthcheck" + "github.com/Luzifer/go_helpers/v2/cli" + "github.com/fatih/color" + "github.com/rodaine/table" +) + +func init() { + registry.Add(cli.RegistryEntry{ + Description: "Display available settings for a check", + Name: "checkhelp", + Params: []string{""}, + Run: func(args []string) error { + if len(args[1:]) < 1 { + return fmt.Errorf("usage: checkhelp ") + } + + check := healthcheck.ByName(args[1]) + if check == nil { + return fmt.Errorf("check %q not found", args[1]) + } + + headerFmt := color.New(color.FgGreen, color.Underline).SprintfFunc() + columnFmt := color.New(color.FgYellow).SprintfFunc() + + tbl := table.New("Setting", "Default", "Description") + tbl.WithHeaderFormatter(headerFmt).WithFirstColumnFormatter(columnFmt) + + for _, help := range check.Help() { + tbl.AddRow(help.Name, help.Default, help.Description) + } + + tbl.Print() + + return nil + }, + }) +} diff --git a/go.mod b/go.mod index 6ac9584..83585a3 100644 --- a/go.mod +++ b/go.mod @@ -6,16 +6,23 @@ require ( github.com/Luzifer/go_helpers/v2 v2.21.0 github.com/Luzifer/rconfig/v2 v2.4.0 github.com/coreos/go-iptables v0.7.0 + github.com/fatih/color v1.16.0 github.com/pkg/errors v0.9.1 + github.com/rodaine/table v1.1.0 github.com/sirupsen/logrus v1.9.3 gopkg.in/yaml.v3 v3.0.1 ) +require ( + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect +) + require ( github.com/kr/pretty v0.3.1 // indirect github.com/mitchellh/hashstructure/v2 v2.0.2 github.com/spf13/pflag v1.0.5 // indirect - golang.org/x/sys v0.13.0 // indirect + golang.org/x/sys v0.14.0 // indirect gopkg.in/validator.v2 v2.0.0-20210331031555-b37d688a7fb0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect ) diff --git a/go.sum b/go.sum index 013c2e6..e31bd4e 100644 --- a/go.sum +++ b/go.sum @@ -8,10 +8,21 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM= +github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0= +github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= github.com/mitchellh/hashstructure/v2 v2.0.2 h1:vGKWl0YJqUNxE8d+h8f6NJLcCJrgbhC4NcD46KavDd4= github.com/mitchellh/hashstructure/v2 v2.0.2/go.mod h1:MG3aRVU/N29oo/V/IhBX8GR/zz4kQkprJgF2EVszyDE= github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= @@ -19,6 +30,8 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rodaine/table v1.1.0 h1:/fUlCSdjamMY8VifdQRIu3VWZXYLY7QHFkVorS8NTr4= +github.com/rodaine/table v1.1.0/go.mod h1:Qu3q5wi1jTQD6B6HsP6szie/S4w1QUQ8pq22pz9iL8g= github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= @@ -26,12 +39,15 @@ github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVs github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= -golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q= +golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= diff --git a/main.go b/main.go index 9dac1a8..d7943c6 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,7 @@ import ( "github.com/pkg/errors" "github.com/sirupsen/logrus" + "github.com/Luzifer/go_helpers/v2/cli" "github.com/Luzifer/rconfig/v2" ) @@ -20,7 +21,8 @@ var ( VersionAndExit bool `flag:"version" default:"false" description:"Prints current version and exits"` }{} - version = "dev" + registry = cli.New() + version = "dev" ) func initApp() error { @@ -49,6 +51,16 @@ func main() { os.Exit(0) } + if len(rconfig.Args()) > 1 { + if err = registry.Call(rconfig.Args()[1:]); err != nil { + if errors.Is(err, cli.ErrHelpCalled) { + os.Exit(1) + } + logrus.WithError(err).Fatal("executing sub-command") + } + os.Exit(0) + } + confFile, err := config.Load(cfg.Config) if err != nil { logrus.WithError(err).Fatal("loading config file")