✨ Load config from file with defaults
This commit is contained in:
parent
2da9fcac37
commit
40f9518611
8 changed files with 127 additions and 2 deletions
31
config/default.go
Normal file
31
config/default.go
Normal 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
18
config/default_test.go
Normal 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
26
config/load.go
Normal 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
29
config/load_test.go
Normal 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
17
config/types.go
Normal 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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue