Load config from file with defaults

This commit is contained in:
Dan Jones 2024-02-04 18:19:19 -06:00
commit 40f9518611
8 changed files with 127 additions and 2 deletions

View file

@ -19,6 +19,7 @@ package cmd
import ( import (
"os" "os"
"codeberg.org/danjones000/my-log/config"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -50,11 +51,11 @@ func init() {
// Cobra supports persistent flags, which, if defined here, // Cobra supports persistent flags, which, if defined here,
// will be global for your application. // will be global for your application.
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.my-log.yaml)") rootCmd.PersistentFlags().StringVarP(&config.ConfigPath, "config", "c", config.ConfigPath, "config file")
// Cobra also supports local flags, which will only run // Cobra also supports local flags, which will only run
// when this action is called directly. // when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") // rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
} }
// initConfig reads in config file and ENV variables if set. // initConfig reads in config file and ENV variables if set.

31
config/default.go Normal file
View file

@ -0,0 +1,31 @@
package config
import (
"fmt"
"os"
fp "path/filepath"
"github.com/BurntSushi/toml"
)
const ConfigStr = `
[input]
ext = "txt"
recurse = true
path = "%s"
[output]
[output.stdout]
enabled = true
[output.stdout.config]
json = false
`
func DefaultConfig() (Config, error) {
home, _ := os.UserHomeDir()
inDir := fp.Join(home, "my-log")
s := fmt.Sprintf(ConfigStr, inDir)
c := Config{}
_, err := toml.Decode(s, &c)
return c, err
}

18
config/default_test.go Normal file
View file

@ -0,0 +1,18 @@
package config
import (
"os"
fp "path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDefaultConfig(t *testing.T) {
home, _ := os.UserHomeDir()
inDir := fp.Join(home, "my-log")
c, err := DefaultConfig()
require.NoError(t, err)
assert.Equal(t, inDir, c.Input.Path)
}

26
config/load.go Normal file
View file

@ -0,0 +1,26 @@
package config
import (
"os"
fp "path/filepath"
"github.com/BurntSushi/toml"
)
var ConfigPath string
func init() {
conf, _ := os.UserConfigDir()
ConfigPath = fp.Join(conf, "my-log", "config.toml")
}
func Load() (Config, error) {
c, _ := DefaultConfig()
_, err := os.Stat(ConfigPath)
if os.IsNotExist(err) {
return c, nil
}
_, err = toml.DecodeFile(ConfigPath, &c)
// @todo get environ
return c, err
}

29
config/load_test.go Normal file
View file

@ -0,0 +1,29 @@
package config
import (
"fmt"
"os"
//fp "path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestLoad(t *testing.T) {
f, _ := os.CreateTemp("", "test")
ConfigPath = f.Name()
fmt.Fprint(f, `[input]
ext = "log"`)
c, err := Load()
require.NoError(t, err)
assert.Equal(t, "log", c.Input.Ext)
}
func TestLoadIgnoreMissingFile(t *testing.T) {
def, _ := DefaultConfig()
ConfigPath = "/not/a/real/file"
c, err := Load()
require.NoError(t, err)
assert.Equal(t, def, c)
}

17
config/types.go Normal file
View file

@ -0,0 +1,17 @@
package config
type Config struct {
Input Input
Outputs map[string]Output `toml:"output"`
}
type Input struct {
Path string
Recurse bool
Ext string
}
type Output struct {
Enabled bool
Config map[string]any
}

1
go.mod
View file

@ -3,6 +3,7 @@ module codeberg.org/danjones000/my-log
go 1.21.5 go 1.21.5
require ( require (
github.com/BurntSushi/toml v1.3.2
github.com/spf13/cobra v1.8.0 github.com/spf13/cobra v1.8.0
github.com/stretchr/testify v1.8.4 github.com/stretchr/testify v1.8.4
) )

2
go.sum
View file

@ -1,3 +1,5 @@
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= 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/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=