diff --git a/cmd/root.go b/cmd/root.go index 33935e1..dc1273f 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -19,6 +19,7 @@ package cmd import ( "os" + "codeberg.org/danjones000/my-log/config" "github.com/spf13/cobra" ) @@ -50,11 +51,11 @@ func init() { // Cobra supports persistent flags, which, if defined here, // 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 // 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. diff --git a/config/default.go b/config/default.go new file mode 100644 index 0000000..d60f003 --- /dev/null +++ b/config/default.go @@ -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 +} diff --git a/config/default_test.go b/config/default_test.go new file mode 100644 index 0000000..6b6ab33 --- /dev/null +++ b/config/default_test.go @@ -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) +} diff --git a/config/load.go b/config/load.go new file mode 100644 index 0000000..fad626c --- /dev/null +++ b/config/load.go @@ -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 +} diff --git a/config/load_test.go b/config/load_test.go new file mode 100644 index 0000000..1f0ed2e --- /dev/null +++ b/config/load_test.go @@ -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) +} diff --git a/config/types.go b/config/types.go new file mode 100644 index 0000000..b8d56bb --- /dev/null +++ b/config/types.go @@ -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 +} diff --git a/go.mod b/go.mod index 6d1c748..b162b9b 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module codeberg.org/danjones000/my-log go 1.21.5 require ( + github.com/BurntSushi/toml v1.3.2 github.com/spf13/cobra v1.8.0 github.com/stretchr/testify v1.8.4 ) diff --git a/go.sum b/go.sum index 08d0c2a..3c50e65 100644 --- a/go.sum +++ b/go.sum @@ -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/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=