1
0
Fork 0
mirror of https://github.com/eosswedenorg/thalos synced 2026-07-02 11:43:40 +02:00

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.

This commit is contained in:
Henrik Hautakoski 2024-02-18 11:44:19 +01:00
parent 117f1b50b4
commit beb5b6cf04
3 changed files with 18 additions and 16 deletions

View file

@ -1,9 +1,9 @@
package main package main
import ( import (
"github.com/eosswedenorg/thalos/internal/config"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/pflag"
) )
var VersionString string = "dev" var VersionString string = "dev"
@ -27,17 +27,7 @@ func init() {
`) `)
rootCmd.SetVersionTemplate(`{{with .Name}}{{printf "%s " .}}{{end}}{{printf "v%s" .Version}}` + "\n") rootCmd.SetVersionTemplate(`{{with .Name}}{{printf "%s " .}}{{end}}{{printf "v%s" .Version}}` + "\n")
flags := pflag.FlagSet{} rootCmd.PersistentFlags().AddFlagSet(config.GetFlags())
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)
} }
func main() { func main() {

View file

@ -6,7 +6,6 @@ import (
"time" "time"
"github.com/eosswedenorg/thalos/internal/log" "github.com/eosswedenorg/thalos/internal/log"
"github.com/spf13/pflag"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
shipclient "github.com/eosswedenorg-go/antelope-ship-client" shipclient "github.com/eosswedenorg-go/antelope-ship-client"
@ -132,14 +131,13 @@ redis:
} }
func TestBuilder_Flags(t *testing.T) { func TestBuilder_Flags(t *testing.T) {
flags := pflag.FlagSet{} flags := GetFlags()
flags.StringP("log", "l", "", "")
require.NoError(t, flags.Set("log", "/path/to/logs")) require.NoError(t, flags.Set("log", "/path/to/logs"))
cfg, err := NewBuilder(). cfg, err := NewBuilder().
SetSource(bytes.NewReader([]byte(``))). SetSource(bytes.NewReader([]byte(``))).
SetFlags(&flags). SetFlags(flags).
Build() Build()
expected := New() expected := New()

View file

@ -6,6 +6,20 @@ import (
"github.com/spf13/pflag" "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 { func overrideCliFlags(cfg *Config, flags *pflag.FlagSet) error {
logFile, _ := flags.GetString("log") logFile, _ := flags.GetString("log")
if len(logFile) > 0 { if len(logFile) > 0 {