🔀 Merge branch 'feature/config' into develop
This commit is contained in:
commit
a062e71a29
12 changed files with 433 additions and 123 deletions
56
cmd/config.go
Normal file
56
cmd/config.go
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
/*
|
||||
Copyright © 2024 Dan Jones <danjones@goodevilgenius.org>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"codeberg.org/danjones000/my-log/config"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// configCmd represents the config command
|
||||
var configCmd = &cobra.Command{
|
||||
Use: "config",
|
||||
Short: "Save default config to file",
|
||||
//Long: ``,
|
||||
SilenceUsage: true,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
force, _ := cmd.Flags().GetBool("force")
|
||||
if !force {
|
||||
_, err := os.Stat(config.ConfigPath)
|
||||
if !os.IsNotExist(err) {
|
||||
return fmt.Errorf("%s already exists. Use -f to overwrite", config.ConfigPath)
|
||||
}
|
||||
}
|
||||
f, err := os.Create(config.ConfigPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
c := config.DefaultStr()
|
||||
fmt.Fprint(f, c)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(configCmd)
|
||||
|
||||
configCmd.Flags().BoolP("force", "f", false, "Force overwrite")
|
||||
}
|
||||
37
cmd/root.go
37
cmd/root.go
|
|
@ -17,25 +17,17 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"codeberg.org/danjones000/my-log/config"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var cfgFile string
|
||||
|
||||
// rootCmd represents the base command when called without any subcommands
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "my-log",
|
||||
Short: "A brief description of your application",
|
||||
Long: `A longer description that spans multiple lines and likely contains
|
||||
examples and usage of using your application. For example:
|
||||
|
||||
Cobra is a CLI library for Go that empowers applications.
|
||||
This application is a tool to generate the needed files
|
||||
to quickly create a Cobra application.`,
|
||||
//Long: ``,
|
||||
// Uncomment the following line if your bare application
|
||||
// has an action associated with it:
|
||||
// Run: func(cmd *cobra.Command, args []string) { },
|
||||
|
|
@ -57,33 +49,16 @@ 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")
|
||||
rootCmd.PersistentFlags().StringToStringVarP(&config.Overrides, "config-value", "v", config.Overrides, "Override config values. Use dot syntax to specify key. E.g. -v output.stdout.config.json=true")
|
||||
|
||||
// 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.
|
||||
func initConfig() {
|
||||
if cfgFile != "" {
|
||||
// Use config file from the flag.
|
||||
viper.SetConfigFile(cfgFile)
|
||||
} else {
|
||||
// Find home directory.
|
||||
home, err := os.UserHomeDir()
|
||||
cobra.CheckErr(err)
|
||||
// @todo
|
||||
|
||||
// Search config in home directory with name ".my-log" (without extension).
|
||||
viper.AddConfigPath(home)
|
||||
viper.SetConfigType("yaml")
|
||||
viper.SetConfigName(".my-log")
|
||||
}
|
||||
|
||||
viper.AutomaticEnv() // read in environment variables that match
|
||||
|
||||
// If a config file is found, read it in.
|
||||
if err := viper.ReadInConfig(); err == nil {
|
||||
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
44
config/default.go
Normal file
44
config/default.go
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
fp "path/filepath"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
)
|
||||
|
||||
const ConfigStr = `# Configuration for my-log
|
||||
|
||||
[input]
|
||||
# Path to where the log files are stored
|
||||
path = "%s"
|
||||
# File extension for log files
|
||||
ext = "txt"
|
||||
# Whether to look in sub-folders
|
||||
recurse = true
|
||||
|
||||
# config for output types
|
||||
[output]
|
||||
|
||||
# This one just prints the logs to stdout when run
|
||||
[output.stdout]
|
||||
enabled = true
|
||||
[output.stdout.config]
|
||||
# Whether to output as JSON. Maybe useful to pipe elsewhere.
|
||||
json = false
|
||||
|
||||
`
|
||||
|
||||
func DefaultStr() string {
|
||||
home, _ := os.UserHomeDir()
|
||||
inDir := fp.Join(home, "my-log")
|
||||
return fmt.Sprintf(ConfigStr, inDir)
|
||||
}
|
||||
|
||||
func DefaultConfig() (Config, error) {
|
||||
s := DefaultStr()
|
||||
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)
|
||||
}
|
||||
79
config/load.go
Normal file
79
config/load.go
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
fp "path/filepath"
|
||||
"time"
|
||||
|
||||
"codeberg.org/danjones000/my-log/tools"
|
||||
"github.com/BurntSushi/toml"
|
||||
"github.com/caarlos0/env/v10"
|
||||
mapst "github.com/mitchellh/mapstructure"
|
||||
)
|
||||
|
||||
var ConfigPath string
|
||||
var Overrides map[string]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) {
|
||||
_, err = toml.DecodeFile(ConfigPath, &c)
|
||||
if err != nil {
|
||||
return c, err
|
||||
}
|
||||
}
|
||||
env.Parse(&c)
|
||||
c.Outputs["stdout"] = loadStdout(c.Outputs["stdout"])
|
||||
|
||||
l := ""
|
||||
for k, v := range Overrides {
|
||||
val := tools.ParseString(v)
|
||||
if val == nil {
|
||||
continue
|
||||
}
|
||||
if _, isJson := val.(json.RawMessage); isJson {
|
||||
continue
|
||||
}
|
||||
valout := fmt.Sprintf("%v", val)
|
||||
if vals, isString := val.(string); isString {
|
||||
valout = fmt.Sprintf(`"%s"`, vals)
|
||||
}
|
||||
if valt, isTime := val.(time.Time); isTime {
|
||||
valout = valt.Format(time.RFC3339)
|
||||
}
|
||||
l = l + "\n" + fmt.Sprintf("%s = %s", k, valout)
|
||||
}
|
||||
_, err = toml.Decode(l, &c)
|
||||
return c, err
|
||||
}
|
||||
|
||||
func loadStdout(stdout Output) Output {
|
||||
st := stdoutEnabled{stdout.Enabled}
|
||||
env.Parse(&st)
|
||||
stdout.Enabled = st.Enabled
|
||||
var std Stdout
|
||||
mapst.Decode(stdout.Config, &std)
|
||||
env.Parse(&std)
|
||||
mapst.Decode(std, &stdout.Config)
|
||||
return stdout
|
||||
}
|
||||
|
||||
func (oo Outputs) Stdout() (s Stdout, enabled bool) {
|
||||
o, ok := oo["stdout"]
|
||||
if !ok {
|
||||
return s, false
|
||||
}
|
||||
|
||||
enabled = o.Enabled
|
||||
mapst.Decode(o.Config, &s)
|
||||
|
||||
return
|
||||
}
|
||||
77
config/load_test.go
Normal file
77
config/load_test.go
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
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()
|
||||
defer f.Close()
|
||||
fmt.Fprint(f, `[input]
|
||||
ext = "log"`)
|
||||
c, err := Load()
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "log", c.Input.Ext)
|
||||
}
|
||||
|
||||
func TestLoadBadFile(t *testing.T) {
|
||||
f, _ := os.CreateTemp("", "test")
|
||||
ConfigPath = f.Name()
|
||||
defer f.Close()
|
||||
fmt.Fprint(f, `{"not":"toml"}`)
|
||||
_, err := Load()
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestLoadIgnoreMissingFile(t *testing.T) {
|
||||
def, _ := DefaultConfig()
|
||||
ConfigPath = "/not/a/real/file"
|
||||
c, err := Load()
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, def, c)
|
||||
}
|
||||
|
||||
func TestOverride(t *testing.T) {
|
||||
Overrides = map[string]string{
|
||||
"input.path": "/path/to/it",
|
||||
"input.ext": "~",
|
||||
}
|
||||
c, err := Load()
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, Overrides["input.path"], c.Input.Path)
|
||||
assert.Equal(t, "txt", c.Input.Ext)
|
||||
}
|
||||
|
||||
func TestOverrideJson(t *testing.T) {
|
||||
Overrides = map[string]string{"input.ext": `{"a":"b"}`}
|
||||
c, err := Load()
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "txt", c.Input.Ext)
|
||||
}
|
||||
|
||||
// @todo test time
|
||||
|
||||
func TestStdoutMissing(t *testing.T) {
|
||||
var oo Outputs = map[string]Output{}
|
||||
std, en := oo.Stdout()
|
||||
assert.False(t, en)
|
||||
assert.Equal(t, Stdout{}, std)
|
||||
}
|
||||
|
||||
func TestStdoutLoad(t *testing.T) {
|
||||
os.Setenv("LOG_STDOUT_JSON", "true")
|
||||
defer os.Unsetenv("LOG_STDOUT_JSON")
|
||||
os.Setenv("LOG_STDOUT_ENABLED", "true")
|
||||
defer os.Unsetenv("LOG_STDOUT_ENABLED")
|
||||
c, _ := Load()
|
||||
std, en := c.Outputs.Stdout()
|
||||
assert.True(t, en)
|
||||
assert.True(t, std.Json)
|
||||
}
|
||||
27
config/types.go
Normal file
27
config/types.go
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
package config
|
||||
|
||||
type Config struct {
|
||||
Input Input
|
||||
Outputs Outputs `toml:"output"`
|
||||
}
|
||||
|
||||
type Input struct {
|
||||
Path string `env:"LOG_PATH"`
|
||||
Recurse bool `env:"LOG_RECURSE"`
|
||||
Ext string `env:"LOG_EXT"`
|
||||
}
|
||||
|
||||
type Outputs map[string]Output
|
||||
|
||||
type Output struct {
|
||||
Enabled bool
|
||||
Config map[string]any
|
||||
}
|
||||
|
||||
type Stdout struct {
|
||||
Json bool `env:"LOG_STDOUT_JSON" mapstructure:"json"`
|
||||
}
|
||||
|
||||
type stdoutEnabled struct {
|
||||
Enabled bool `env:"LOG_STDOUT_ENABLED"`
|
||||
}
|
||||
23
go.mod
23
go.mod
|
|
@ -3,32 +3,19 @@ module codeberg.org/danjones000/my-log
|
|||
go 1.21.5
|
||||
|
||||
require (
|
||||
github.com/BurntSushi/toml v1.3.2
|
||||
github.com/caarlos0/env/v10 v10.0.0
|
||||
github.com/mitchellh/mapstructure v1.5.0
|
||||
github.com/spf13/cobra v1.8.0
|
||||
github.com/spf13/viper v1.18.2
|
||||
github.com/stretchr/testify v1.8.4
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
|
||||
github.com/fsnotify/fsnotify v1.7.0 // indirect
|
||||
github.com/hashicorp/hcl v1.0.0 // indirect
|
||||
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
||||
github.com/magiconair/properties v1.8.7 // indirect
|
||||
github.com/mitchellh/mapstructure v1.5.0 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
|
||||
github.com/kr/pretty v0.3.1 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
|
||||
github.com/sagikazarmark/locafero v0.4.0 // indirect
|
||||
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
|
||||
github.com/sourcegraph/conc v0.3.0 // indirect
|
||||
github.com/spf13/afero v1.11.0 // indirect
|
||||
github.com/spf13/cast v1.6.0 // indirect
|
||||
github.com/spf13/pflag v1.0.5 // indirect
|
||||
github.com/subosito/gotenv v1.6.0 // indirect
|
||||
go.uber.org/atomic v1.9.0 // indirect
|
||||
go.uber.org/multierr v1.9.0 // indirect
|
||||
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
|
||||
golang.org/x/sys v0.15.0 // indirect
|
||||
golang.org/x/text v0.14.0 // indirect
|
||||
gopkg.in/ini.v1 v1.67.0 // indirect
|
||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
|
|
|||
54
go.sum
54
go.sum
|
|
@ -1,75 +1,33 @@
|
|||
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
|
||||
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
||||
github.com/caarlos0/env/v10 v10.0.0 h1:yIHUBZGsyqCnpTkbjk8asUlx6RFhhEs+h7TOBdgdzXA=
|
||||
github.com/caarlos0/env/v10 v10.0.0/go.mod h1:ZfulV76NvVPw3tm591U4SwL3Xx9ldzBP9aGxzeN7G18=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
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/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
|
||||
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
|
||||
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
|
||||
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
|
||||
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
|
||||
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
|
||||
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
|
||||
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
|
||||
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
|
||||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
|
||||
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
|
||||
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
|
||||
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
|
||||
github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4=
|
||||
github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
|
||||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
|
||||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
|
||||
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
|
||||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||
github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ=
|
||||
github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4=
|
||||
github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE=
|
||||
github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ=
|
||||
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
|
||||
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
|
||||
github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8=
|
||||
github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY=
|
||||
github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0=
|
||||
github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
|
||||
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
|
||||
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
|
||||
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
|
||||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
github.com/spf13/viper v1.18.2 h1:LUXCnvUvSM6FXAsj6nnfc8Q2tp1dIgUfY9Kc8GsSOiQ=
|
||||
github.com/spf13/viper v1.18.2/go.mod h1:EKmWIqdnk5lOcmR72yw6hS+8OPYcwD0jteitLMVB+yk=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
|
||||
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
|
||||
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
|
||||
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
|
||||
go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
|
||||
go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ=
|
||||
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
|
||||
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
|
||||
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
|
||||
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
|
||||
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
|
||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
|
||||
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
|
|
|||
|
|
@ -7,8 +7,9 @@ import (
|
|||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"codeberg.org/danjones000/my-log/tools"
|
||||
)
|
||||
|
||||
type Meta struct {
|
||||
|
|
@ -73,33 +74,11 @@ func (m *Meta) processMeta(in []byte) error {
|
|||
if len(in) == 0 {
|
||||
return newParsingError(errors.New("No value found"))
|
||||
}
|
||||
s := strings.TrimSpace(string(in))
|
||||
if len(s) == 0 {
|
||||
v := tools.ParseBytes(in)
|
||||
if v == "" {
|
||||
return newParsingError(errors.New("No value found"))
|
||||
}
|
||||
yesno := regexp.MustCompile("^(y|Y|yes|Yes|YES|n|N|no|No|NO|true|True|TRUE|false|False|FALSE|on|On|ON|off|Off|OFF)$")
|
||||
yes := regexp.MustCompile("^(y|Y|yes|Yes|YES|true|True|TRUE|on|On|ON)$")
|
||||
null := regexp.MustCompile("^(~|null|Null|NULL|none|None|NONE|nil|Nil|NIL)$")
|
||||
var j json.RawMessage
|
||||
if null.MatchString(s) {
|
||||
m.Value = nil
|
||||
} else if yesno.MatchString(s) {
|
||||
if yes.MatchString(s) {
|
||||
m.Value = true
|
||||
} else {
|
||||
m.Value = false
|
||||
}
|
||||
} else if i, err := strconv.Atoi(s); err == nil {
|
||||
m.Value = i
|
||||
} else if f, err := strconv.ParseFloat(s, 64); err == nil {
|
||||
m.Value = f
|
||||
} else if t, err := time.Parse(time.RFC3339, s); err == nil {
|
||||
m.Value = t
|
||||
} else if err := json.Unmarshal(in, &j); err == nil {
|
||||
m.Value = j
|
||||
} else {
|
||||
m.Value = s
|
||||
}
|
||||
|
||||
m.Value = v
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
44
tools/parse.go
Normal file
44
tools/parse.go
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
package tools
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func ParseBytes(in []byte) any {
|
||||
return ParseString(string(in))
|
||||
}
|
||||
|
||||
func ParseString(in string) any {
|
||||
s := strings.TrimSpace(in)
|
||||
if s == "" {
|
||||
return s
|
||||
}
|
||||
|
||||
yesno := regexp.MustCompile("^(y|Y|yes|Yes|YES|n|N|no|No|NO|true|True|TRUE|false|False|FALSE|on|On|ON|off|Off|OFF)$")
|
||||
yes := regexp.MustCompile("^(y|Y|yes|Yes|YES|true|True|TRUE|on|On|ON)$")
|
||||
null := regexp.MustCompile("^(~|null|Null|NULL|none|None|NONE|nil|Nil|NIL)$")
|
||||
var j json.RawMessage
|
||||
if null.MatchString(s) {
|
||||
return nil
|
||||
} else if yesno.MatchString(s) {
|
||||
if yes.MatchString(s) {
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
} else if i, err := strconv.Atoi(s); err == nil {
|
||||
return i
|
||||
} else if f, err := strconv.ParseFloat(s, 64); err == nil {
|
||||
return f
|
||||
} else if t, err := time.Parse(time.RFC3339, s); err == nil {
|
||||
return t
|
||||
} else if err := json.Unmarshal([]byte(s), &j); err == nil {
|
||||
return j
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
66
tools/parse_test.go
Normal file
66
tools/parse_test.go
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
package tools
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestParse(t *testing.T) {
|
||||
when := time.Now()
|
||||
tests := []struct {
|
||||
name string
|
||||
in string
|
||||
out any
|
||||
}{
|
||||
{"int", "42", 42},
|
||||
{"float", "42.13", 42.13},
|
||||
{"string", "hello", "hello"},
|
||||
{"true", "true", true},
|
||||
{"false", "false", false},
|
||||
{"nil", "nil", nil},
|
||||
{"time", when.Format(time.RFC3339), when},
|
||||
{"json-obj", `{"foo":"bar","baz":"quux"}`, json.RawMessage(`{"foo":"bar","baz":"quux"}`)},
|
||||
{"json-arr", `["foo",42,"bar", null,"quux", true]`, json.RawMessage(`["foo",42,"bar", null,"quux", true]`)},
|
||||
{"empty", "", ""},
|
||||
{"space-value", " ", ""},
|
||||
{"space-nl-value", " \n ", ""},
|
||||
{"null-value", "null", nil},
|
||||
{"tilda-value", "~", nil},
|
||||
{"none-value", "none", nil},
|
||||
{"nil-value", "nil", nil},
|
||||
{"yes-value", "yes", true},
|
||||
{"on-value", "on", true},
|
||||
{"no-value", "no", false},
|
||||
{"off-value", "off", false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, getParseTestRunner(tt.in, tt.out))
|
||||
}
|
||||
}
|
||||
|
||||
func getParseTestRunner(in string, exp any) func(*testing.T) {
|
||||
return func(t *testing.T) {
|
||||
out := ParseString(in)
|
||||
if expT, ok := exp.(time.Time); ok {
|
||||
ti, gotTime := out.(time.Time)
|
||||
if assert.True(t, gotTime, "Should have gotten a time.Time, but didn't") {
|
||||
assert.WithinRange(t, expT, ti.Add(-time.Second), ti.Add(time.Second))
|
||||
}
|
||||
} else {
|
||||
assert.Equal(t, exp, out)
|
||||
}
|
||||
out = ParseBytes([]byte(in))
|
||||
if expT, ok := exp.(time.Time); ok {
|
||||
ti, gotTime := out.(time.Time)
|
||||
if assert.True(t, gotTime, "Should have gotten a time.Time, but didn't") {
|
||||
assert.WithinRange(t, expT, ti.Add(-time.Second), ti.Add(time.Second))
|
||||
}
|
||||
} else {
|
||||
assert.Equal(t, exp, out)
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue