From beb5b6cf0465e2a4beed72adfb72f50ffa876f5f Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Sun, 18 Feb 2024 11:44:19 +0100 Subject: [PATCH] cmd/thalos/main.go: move cli flags to internal/config/cli.go as it is easier to write tests if we can get a hold of the flags. --- cmd/thalos/main.go | 14 ++------------ internal/config/builder_test.go | 6 ++---- internal/config/cli.go | 14 ++++++++++++++ 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/cmd/thalos/main.go b/cmd/thalos/main.go index c31c956..b2d8f33 100644 --- a/cmd/thalos/main.go +++ b/cmd/thalos/main.go @@ -1,9 +1,9 @@ package main import ( + "github.com/eosswedenorg/thalos/internal/config" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" - "github.com/spf13/pflag" ) var VersionString string = "dev" @@ -27,17 +27,7 @@ func init() { `) rootCmd.SetVersionTemplate(`{{with .Name}}{{printf "%s " .}}{{end}}{{printf "v%s" .Version}}` + "\n") - flags := pflag.FlagSet{} - flags.StringP("config", "c", "./config.yml", "Config file to read") - flags.StringP("level", "L", "info", "Log level to use") - flags.StringP("log", "l", "", "Path to log file (default: print to stdout/stderr)") - flags.StringP("pid", "p", "", "Where to write process id") - flags.BoolP("no-state-cache", "n", false, "Force the application to take start block from config/api") - - flags.Int("start-block", 0, "Start to stream from this block (default: config value, cache, head from api)") - flags.Int("end-block", 0, "Stop streaming when this block is reached") - - rootCmd.PersistentFlags().AddFlagSet(&flags) + rootCmd.PersistentFlags().AddFlagSet(config.GetFlags()) } func main() { diff --git a/internal/config/builder_test.go b/internal/config/builder_test.go index e7ceaf5..8ff339f 100644 --- a/internal/config/builder_test.go +++ b/internal/config/builder_test.go @@ -6,7 +6,6 @@ import ( "time" "github.com/eosswedenorg/thalos/internal/log" - "github.com/spf13/pflag" "github.com/stretchr/testify/require" shipclient "github.com/eosswedenorg-go/antelope-ship-client" @@ -132,14 +131,13 @@ redis: } func TestBuilder_Flags(t *testing.T) { - flags := pflag.FlagSet{} - flags.StringP("log", "l", "", "") + flags := GetFlags() require.NoError(t, flags.Set("log", "/path/to/logs")) cfg, err := NewBuilder(). SetSource(bytes.NewReader([]byte(``))). - SetFlags(&flags). + SetFlags(flags). Build() expected := New() diff --git a/internal/config/cli.go b/internal/config/cli.go index b56f6a0..0993601 100644 --- a/internal/config/cli.go +++ b/internal/config/cli.go @@ -6,6 +6,20 @@ import ( "github.com/spf13/pflag" ) +func GetFlags() *pflag.FlagSet { + flags := pflag.FlagSet{} + flags.StringP("config", "c", "./config.yml", "Config file to read") + flags.StringP("level", "L", "info", "Log level to use") + flags.StringP("log", "l", "", "Path to log file (default: print to stdout/stderr)") + flags.StringP("pid", "p", "", "Where to write process id") + flags.BoolP("no-state-cache", "n", false, "Force the application to take start block from config/api") + + flags.Int("start-block", 0, "Start to stream from this block (default: config value, cache, head from api)") + flags.Int("end-block", 0, "Stop streaming when this block is reached") + + return &flags +} + func overrideCliFlags(cfg *Config, flags *pflag.FlagSet) error { logFile, _ := flags.GetString("log") if len(logFile) > 0 {