feat: allow multiple configs

This commit is contained in:
Kevin Franklin Kim 2025-03-20 16:17:14 +01:00
parent 8569b47ed4
commit 4f632d515a
No known key found for this signature in database
3 changed files with 49 additions and 38 deletions

View File

@ -1,10 +1,14 @@
package cmd
import (
"bytes"
"encoding/json"
"fmt"
"os"
"github.com/alecthomas/chroma/quick"
pkgcmd "github.com/foomo/sesamy-cli/pkg/cmd"
"github.com/itchyny/json2yaml"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
@ -22,11 +26,15 @@ func NewConfig(root *cobra.Command) {
out, err := json.MarshalIndent(cfg, "", " ")
if err != nil {
return err
return errors.Wrap(err, "failed to marshal config")
}
fmt.Println(string(out))
return nil
var buf bytes.Buffer
if err := json2yaml.Convert(&buf, bytes.NewBuffer(out)); err != nil {
return errors.Wrap(err, "failed to convert config")
}
return quick.Highlight(os.Stdout, buf.String(), "yaml", "terminal", "monokai")
},
}

11
go.mod
View File

@ -3,15 +3,19 @@ module github.com/foomo/sesamy-cli
go 1.24.1
require (
github.com/Code-Hex/Neo-cowsay/v2 v2.0.4
github.com/alecthomas/chroma v0.10.0
github.com/fatih/structtag v1.2.0
github.com/foomo/go v0.0.3
github.com/foomo/gocontemplate v0.2.0
github.com/foomo/sesamy-go v0.9.0
github.com/go-viper/mapstructure/v2 v2.2.1
github.com/invopop/jsonschema v0.13.0
github.com/itchyny/json2yaml v0.1.4
github.com/joho/godotenv v1.5.1
github.com/knadh/koanf/parsers/yaml v0.1.0
github.com/knadh/koanf/providers/file v1.1.2
github.com/knadh/koanf/providers/rawbytes v0.1.0
github.com/knadh/koanf/v2 v2.1.2
github.com/pkg/errors v0.9.1
github.com/pterm/pterm v0.12.80
github.com/spf13/cobra v1.9.1
@ -29,6 +33,7 @@ require (
cloud.google.com/go/auth v0.15.0 // indirect
cloud.google.com/go/auth/oauth2adapt v0.2.7 // indirect
cloud.google.com/go/compute/metadata v0.6.0 // indirect
github.com/Code-Hex/go-wordwrap v1.0.0 // indirect
github.com/bahlo/generic-list-go v0.2.0 // indirect
github.com/buger/jsonparser v1.1.1 // indirect
github.com/containerd/console v1.0.4 // indirect
@ -39,16 +44,20 @@ require (
github.com/fsnotify/fsnotify v1.8.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-viper/mapstructure/v2 v2.2.1 // indirect
github.com/google/s2a-go v0.1.9 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.5 // indirect
github.com/googleapis/gax-go/v2 v2.14.1 // indirect
github.com/gookit/color v1.5.4 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/knadh/koanf/maps v0.1.1 // indirect
github.com/lithammer/fuzzysearch v1.1.8 // indirect
github.com/mailru/easyjson v0.9.0 // indirect
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/mapstructure v1.5.1-0.20220423185008-bf980b35cac4 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rivo/uniseg v0.4.7 // indirect

View File

@ -1,57 +1,51 @@
package cmd
import (
"bytes"
"io"
"log/slog"
"github.com/foomo/sesamy-cli/pkg/config"
"github.com/go-viper/mapstructure/v2"
"github.com/knadh/koanf/parsers/yaml"
"github.com/knadh/koanf/providers/file"
"github.com/knadh/koanf/providers/rawbytes"
"github.com/knadh/koanf/v2"
"github.com/pkg/errors"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// InitConfig reads in config file and ENV variables if set.
func InitConfig() {
filename := viper.GetString("config")
viper.SetConfigType("yaml")
if filename == "-" {
// do nothing
} else if filename != "" {
// Use config file from the flag.
viper.SetConfigFile(filename)
} else {
// Search config in home directory with name ".sesamy" (without extension).
viper.AddConfigPath(".")
viper.SetConfigName("sesamy")
}
var conf = koanf.Conf{
Delim: "/",
}
var k = koanf.NewWithConf(conf)
func ReadConfig(l *slog.Logger, cmd *cobra.Command) (*config.Config, error) {
filename := viper.GetString("config")
filenames := viper.GetStringSlice("config")
if filename == "-" {
l.Debug("using config from stdin")
b, err := io.ReadAll(cmd.InOrStdin())
if err != nil {
return nil, err
for _, filename := range filenames {
var p koanf.Provider
switch {
case filename == "-":
pterm.Debug.Println("reading config from stdin")
if b, err := io.ReadAll(cmd.InOrStdin()); err != nil {
return nil, err
} else {
p = rawbytes.Provider(b)
}
default:
pterm.Debug.Println("reading config from filename: " + filename)
p = file.Provider(filename)
}
if err := viper.ReadConfig(bytes.NewBuffer(b)); err != nil {
return nil, err
}
} else {
l.Debug("using config file", "filename", viper.ConfigFileUsed())
if err := viper.ReadInConfig(); err != nil {
return nil, err
if err := k.Load(p, yaml.Parser()); err != nil {
return nil, errors.Wrap(err, "error loading config file: "+filename)
}
}
// l.Debug("config", l.ArgsFromMap(viper.AllSettings()))
var cfg *config.Config
if err := viper.Unmarshal(&cfg, func(decoderConfig *mapstructure.DecoderConfig) {
decoderConfig.TagName = "yaml"
pterm.Debug.Println("unmarshalling config")
if err := k.UnmarshalWithConf("", &cfg, koanf.UnmarshalConf{
Tag: "yaml",
}); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal config")
}