Skip to content

Commit

Permalink
Merge pull request #42 from wesen/misc/split-init-viper
Browse files Browse the repository at this point in the history
split out helper to initialize viper for easier config loading in tools
  • Loading branch information
wesen authored Jul 5, 2024
2 parents e5f33b1 + 5ea564d commit 653b9ac
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 44 deletions.
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.19
require (
github.com/bmatcuk/doublestar/v4 v4.6.0
github.com/fsnotify/fsnotify v1.6.0
github.com/go-go-golems/glazed v0.5.14
github.com/go-go-golems/glazed v0.5.17
github.com/go-go-golems/sqleton v0.2.4
github.com/go-sql-driver/mysql v1.7.1
github.com/jmoiron/sqlx v1.3.5
Expand Down Expand Up @@ -37,7 +37,6 @@ require (
github.com/charmbracelet/glamour v0.6.1-0.20230531150759-6d5b52861a9d // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dlclark/regexp2 v1.10.0 // indirect
github.com/frankban/quicktest v1.14.5 // indirect
github.com/go-openapi/errors v0.22.0 // indirect
github.com/go-openapi/strfmt v0.23.0 // indirect
github.com/google/uuid v1.6.0 // indirect
Expand Down
11 changes: 2 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnht
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
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=
Expand All @@ -91,14 +90,13 @@ github.com/envoyproxy/go-control-plane v0.9.7/go.mod h1:cwu0lG7PUMfa9snN8LXBig5y
github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/frankban/quicktest v1.14.5 h1:dfYrrRyLtiqT9GyKXgdh+k4inNeTvmGbuSgZ3lx3GhA=
github.com/frankban/quicktest v1.14.5/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-go-golems/glazed v0.5.14 h1:bq6nvrA07iUv0QvOMm5oQVLDgfoPfIJxnLTUFyZIvE0=
github.com/go-go-golems/glazed v0.5.14/go.mod h1:SvPA8GVBwAbyNBZVrK2ZH9cIHLMKxc/bf+rB7ewuonE=
github.com/go-go-golems/glazed v0.5.17 h1:rqkSPGClE2i0wgq/Bo4+dWK0polGITioCs494sWp3+Y=
github.com/go-go-golems/glazed v0.5.17/go.mod h1:C1zWpbRfs3+xmtAZoW6RFgFvTLeC2xq+gkc1S5Luvz4=
github.com/go-go-golems/sqleton v0.2.4 h1:qsgX0RxBXdjOC/+zmRrVvlsbBT8HCM2otLh/bV/f5uU=
github.com/go-go-golems/sqleton v0.2.4/go.mod h1:GAGCz4/wsFwzN5mUA3ARmJfqanYu1k5yP4rCUiTeWgs=
github.com/go-openapi/errors v0.22.0 h1:c4xY/OLxUBSTiepAg3j/MHuAv5mJhnf53LLMWFB+u/w=
Expand Down Expand Up @@ -145,7 +143,6 @@ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
Expand Down Expand Up @@ -199,11 +196,9 @@ github.com/kopoli/go-terminal-size v0.0.0-20170219200355-5c97524c8b54/go.mod h1:
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
Expand Down Expand Up @@ -248,7 +243,6 @@ github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZR9tGQ=
github.com/pelletier/go-toml/v2 v2.0.8/go.mod h1:vuYfssBdrU2XDZ9bYydBu6t+6a6PYNcZljzZR9VXg+4=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qRg=
Expand All @@ -265,7 +259,6 @@ github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJ
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.30.0 h1:SymVODrcRsaRaSInD9yQtKbtWqwsfoPcRff/oRXLj4c=
Expand Down
79 changes: 48 additions & 31 deletions pkg/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,25 @@ func InitLoggerWithConfig(config *LogConfig) error {
}

if config.LogFile != "" {
logWriter = io.MultiWriter(
logWriter,
zerolog.ConsoleWriter{
fileLogger := &lumberjack.Logger{
Filename: config.LogFile,
MaxSize: 10, // megabytes
MaxBackups: 3,
MaxAge: 28, //days
Compress: false, // disabled by default
}
var writer io.Writer
writer = fileLogger
if config.LogFormat == "text" {
writer = zerolog.ConsoleWriter{
NoColor: true,
Out: &lumberjack.Logger{
Filename: config.LogFile,
MaxSize: 10, // megabytes
MaxBackups: 3,
MaxAge: 28, //days
Compress: false, // disabled by default
},
})
Out: fileLogger,
}
}
// TODO(manuel, 2024-07-05) We used to support logging to file *and* stderr, but disabling that for now
// because it makes logging in UI apps tricky.
// logWriter = io.MultiWriter(logWriter, writer)
logWriter = writer
}

log.Logger = log.Output(logWriter)
Expand Down Expand Up @@ -80,26 +87,7 @@ func InitLogger() error {
})
}

func InitViper(appName string, rootCmd *cobra.Command) error {
rootCmd.PersistentFlags().Bool("with-caller", false, "Log caller")
rootCmd.PersistentFlags().String("log-level", "info", "Log level (debug, info, warn, error, fatal)")
rootCmd.PersistentFlags().String("log-format", "text", "Log format (json, text)")
rootCmd.PersistentFlags().String("log-file", "", "Log file (default: stderr)")

rootCmd.PersistentFlags().Bool("verbose", false, "Verbose output")

rootCmd.PersistentFlags().String("config", "",
fmt.Sprintf("Path to config file (default ~/.%s/config.yml)", appName))

// parse the flags one time just to catch --config
configFile := ""
for idx, arg := range os.Args {
if arg == "--config" {
if len(os.Args) > idx+1 {
configFile = os.Args[idx+1]
}
}
}
func InitViperWithAppName(appName string, configFile string) error {
viper.SetEnvPrefix(appName)

if configFile != "" {
Expand Down Expand Up @@ -128,6 +116,35 @@ func InitViper(appName string, rootCmd *cobra.Command) error {
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
viper.AutomaticEnv()

return nil
}

func InitViper(appName string, rootCmd *cobra.Command) error {
rootCmd.PersistentFlags().Bool("with-caller", false, "Log caller")
rootCmd.PersistentFlags().String("log-level", "info", "Log level (debug, info, warn, error, fatal)")
rootCmd.PersistentFlags().String("log-format", "text", "Log format (json, text)")
rootCmd.PersistentFlags().String("log-file", "", "Log file (default: stderr)")

rootCmd.PersistentFlags().Bool("verbose", false, "Verbose output")

rootCmd.PersistentFlags().String("config", "",
fmt.Sprintf("Path to config file (default ~/.%s/config.yml)", appName))

// parse the flags one time just to catch --config
configFile := ""
for idx, arg := range os.Args {
if arg == "--config" {
if len(os.Args) > idx+1 {
configFile = os.Args[idx+1]
}
}
}

err := InitViperWithAppName(appName, configFile)
if err != nil {
return err
}

// Bind the variables to the command-line flags
err = viper.BindPFlags(rootCmd.PersistentFlags())
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/sql/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ func sqlStringLike(value string) string {
// sqlStringIn converts a slice of values into a SQL IN clause string, properly escaping and quoting each value.
// Returns an error if the input cannot be cast to a slice of strings.
func sqlStringIn(values interface{}) (string, error) {
strList, ok := cast.CastList2[string, interface{}](values)
if !ok {
strList, err := cast.CastListToStringList(values)
if err != nil {
return "", errors.Errorf("could not cast %v to []string", values)
}
return fmt.Sprintf("'%s'", strings.Join(strList, "','")), nil
Expand Down

0 comments on commit 653b9ac

Please sign in to comment.