From 2da9fcac37da86a393bf1f49abdacac3f1e91676 Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Sun, 4 Feb 2024 10:11:04 -0600 Subject: [PATCH 01/17] =?UTF-8?q?=F0=9F=94=A5=20Remove=20viper?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/root.go | 30 ++---------------------------- go.mod | 20 ++------------------ go.sum | 52 ++-------------------------------------------------- 3 files changed, 6 insertions(+), 96 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 2ae709d..33935e1 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -17,11 +17,9 @@ along with this program. If not, see . package cmd import ( - "fmt" "os" "github.com/spf13/cobra" - "github.com/spf13/viper" ) var cfgFile string @@ -30,12 +28,7 @@ var cfgFile string 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) { }, @@ -66,24 +59,5 @@ func init() { // 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) - - // 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()) - } + // @todo } diff --git a/go.mod b/go.mod index 2aafd80..6d1c748 100644 --- a/go.mod +++ b/go.mod @@ -4,31 +4,15 @@ go 1.21.5 require ( 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 ) diff --git a/go.sum b/go.sum index b6a7dcc..08d0c2a 100644 --- a/go.sum +++ b/go.sum @@ -1,75 +1,27 @@ 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= From 40f9518611b0c27f3b8a3ae9dc6b1ebe94ad5a79 Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Sun, 4 Feb 2024 18:19:19 -0600 Subject: [PATCH 02/17] =?UTF-8?q?=E2=9C=A8=20Load=20config=20from=20file?= =?UTF-8?q?=20with=20defaults?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/root.go | 5 +++-- config/default.go | 31 +++++++++++++++++++++++++++++++ config/default_test.go | 18 ++++++++++++++++++ config/load.go | 26 ++++++++++++++++++++++++++ config/load_test.go | 29 +++++++++++++++++++++++++++++ config/types.go | 17 +++++++++++++++++ go.mod | 1 + go.sum | 2 ++ 8 files changed, 127 insertions(+), 2 deletions(-) create mode 100644 config/default.go create mode 100644 config/default_test.go create mode 100644 config/load.go create mode 100644 config/load_test.go create mode 100644 config/types.go 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= From 25f5c37243ebd108f446267f47d3dae94150f25c Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Fri, 9 Feb 2024 09:44:35 -0600 Subject: [PATCH 03/17] =?UTF-8?q?=E2=9C=A8=20Parse=20config=20overrides=20?= =?UTF-8?q?on=20cli?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/root.go | 4 +-- config/load.go | 33 ++++++++++++++++++++--- config/types.go | 20 +++++++++++++- go.mod | 1 + go.sum | 2 ++ models/meta.go | 31 ++++----------------- tools/parse.go | 44 ++++++++++++++++++++++++++++++ tools/parse_test.go | 66 +++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 169 insertions(+), 32 deletions(-) create mode 100644 tools/parse.go create mode 100644 tools/parse_test.go diff --git a/cmd/root.go b/cmd/root.go index dc1273f..a07455d 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -23,8 +23,6 @@ import ( "github.com/spf13/cobra" ) -var cfgFile string - // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ Use: "my-log", @@ -52,6 +50,7 @@ func init() { // will be global for your application. 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. @@ -61,4 +60,5 @@ func init() { // initConfig reads in config file and ENV variables if set. func initConfig() { // @todo + } diff --git a/config/load.go b/config/load.go index fad626c..44a1b75 100644 --- a/config/load.go +++ b/config/load.go @@ -1,13 +1,18 @@ package config import ( + "encoding/json" + "fmt" "os" fp "path/filepath" + "time" + "codeberg.org/danjones000/my-log/tools" "github.com/BurntSushi/toml" ) var ConfigPath string +var Overrides map[string]string func init() { conf, _ := os.UserConfigDir() @@ -17,10 +22,32 @@ func init() { func Load() (Config, error) { c, _ := DefaultConfig() _, err := os.Stat(ConfigPath) - if os.IsNotExist(err) { - return c, nil + if !os.IsNotExist(err) { + _, err = toml.DecodeFile(ConfigPath, &c) + if err != nil { + return c, err + } } - _, err = toml.DecodeFile(ConfigPath, &c) // @todo get environ + + 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 } diff --git a/config/types.go b/config/types.go index b8d56bb..bc07f82 100644 --- a/config/types.go +++ b/config/types.go @@ -1,8 +1,10 @@ package config +import mapst "github.com/mitchellh/mapstructure" + type Config struct { Input Input - Outputs map[string]Output `toml:"output"` + Outputs Outputs `toml:"output"` } type Input struct { @@ -11,7 +13,23 @@ type Input struct { Ext string } +type Outputs map[string]Output + type Output struct { Enabled bool Config map[string]any } + +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 +} + +type Stdout struct { + Json bool +} diff --git a/go.mod b/go.mod index b162b9b..a1de78f 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.21.5 require ( github.com/BurntSushi/toml v1.3.2 + github.com/mitchellh/mapstructure v1.5.0 github.com/spf13/cobra v1.8.0 github.com/stretchr/testify v1.8.4 ) diff --git a/go.sum b/go.sum index 3c50e65..ea20855 100644 --- a/go.sum +++ b/go.sum @@ -10,6 +10,8 @@ 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/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= +github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= 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= diff --git a/models/meta.go b/models/meta.go index 2d6522c..c737f95 100644 --- a/models/meta.go +++ b/models/meta.go @@ -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 } diff --git a/tools/parse.go b/tools/parse.go new file mode 100644 index 0000000..38d7b5e --- /dev/null +++ b/tools/parse.go @@ -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 +} diff --git a/tools/parse_test.go b/tools/parse_test.go new file mode 100644 index 0000000..2e8e49f --- /dev/null +++ b/tools/parse_test.go @@ -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) + } + } +} From 85f666cbc2129064ec56c04cba0b889c5734c99e Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Sat, 10 Feb 2024 11:06:00 -0600 Subject: [PATCH 04/17] =?UTF-8?q?=E2=9C=A8=20Parse=20env=20vars?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/load.go | 21 ++++++++++++++++++++- config/load_test.go | 30 ++++++++++++++++++++++++++++++ config/types.go | 20 ++++---------------- go.mod | 1 + go.sum | 2 ++ 5 files changed, 57 insertions(+), 17 deletions(-) diff --git a/config/load.go b/config/load.go index 44a1b75..a7b9c57 100644 --- a/config/load.go +++ b/config/load.go @@ -9,6 +9,8 @@ import ( "codeberg.org/danjones000/my-log/tools" "github.com/BurntSushi/toml" + "github.com/caarlos0/env/v10" + mapst "github.com/mitchellh/mapstructure" ) var ConfigPath string @@ -28,7 +30,8 @@ func Load() (Config, error) { return c, err } } - // @todo get environ + env.Parse(&c) + // @todo how to handle env on options so cli args aren't overwrittem? l := "" for k, v := range Overrides { @@ -51,3 +54,19 @@ func Load() (Config, error) { _, err = toml.Decode(l, &c) return c, err } + +func (oo Outputs) Stdout() (s Stdout, enabled bool) { + o, ok := oo["stdout"] + if !ok { + return s, false + } + st := struct { + Enabled bool `env:"LOG_STDOUT_ENABLED"` + }{o.Enabled} + env.Parse(&st) + + enabled = st.Enabled + mapst.Decode(o.Config, &s) + env.Parse(&s) + return +} diff --git a/config/load_test.go b/config/load_test.go index 1f0ed2e..6cc9077 100644 --- a/config/load_test.go +++ b/config/load_test.go @@ -13,6 +13,7 @@ import ( func TestLoad(t *testing.T) { f, _ := os.CreateTemp("", "test") ConfigPath = f.Name() + defer f.Close() fmt.Fprint(f, `[input] ext = "log"`) c, err := Load() @@ -20,6 +21,15 @@ ext = "log"`) 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" @@ -27,3 +37,23 @@ func TestLoadIgnoreMissingFile(t *testing.T) { 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 diff --git a/config/types.go b/config/types.go index bc07f82..4226fca 100644 --- a/config/types.go +++ b/config/types.go @@ -1,16 +1,14 @@ package config -import mapst "github.com/mitchellh/mapstructure" - type Config struct { Input Input Outputs Outputs `toml:"output"` } type Input struct { - Path string - Recurse bool - Ext string + Path string `env:"LOG_PATH"` + Recurse bool `env:"LOG_RECURSE"` + Ext string `env:"LOG_EXT"` } type Outputs map[string]Output @@ -20,16 +18,6 @@ type Output struct { Config map[string]any } -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 -} - type Stdout struct { - Json bool + Json bool `env:"LOG_STDOUT_JSON"` } diff --git a/go.mod b/go.mod index a1de78f..4bbf1f1 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ 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/stretchr/testify v1.8.4 diff --git a/go.sum b/go.sum index ea20855..c97eec7 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ 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/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= From a6f3cf39a0da9d1902f2d4d443201c17e36307ed Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Sat, 10 Feb 2024 11:48:23 -0600 Subject: [PATCH 05/17] =?UTF-8?q?=F0=9F=90=9B=20Load=20config=20env=20befo?= =?UTF-8?q?re=20paraing=20cli?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/load.go | 21 ++++++++++++++------- config/load_test.go | 18 ++++++++++++++++++ config/types.go | 6 +++++- 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/config/load.go b/config/load.go index a7b9c57..c561575 100644 --- a/config/load.go +++ b/config/load.go @@ -31,7 +31,7 @@ func Load() (Config, error) { } } env.Parse(&c) - // @todo how to handle env on options so cli args aren't overwrittem? + c.Outputs["stdout"] = loadStdout(c.Outputs["stdout"]) l := "" for k, v := range Overrides { @@ -55,18 +55,25 @@ func Load() (Config, error) { 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 } - st := struct { - Enabled bool `env:"LOG_STDOUT_ENABLED"` - }{o.Enabled} - env.Parse(&st) - enabled = st.Enabled + enabled = o.Enabled mapst.Decode(o.Config, &s) - env.Parse(&s) + return } diff --git a/config/load_test.go b/config/load_test.go index 6cc9077..e215ff2 100644 --- a/config/load_test.go +++ b/config/load_test.go @@ -57,3 +57,21 @@ func TestOverrideJson(t *testing.T) { } // @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) +} diff --git a/config/types.go b/config/types.go index 4226fca..b218509 100644 --- a/config/types.go +++ b/config/types.go @@ -19,5 +19,9 @@ type Output struct { } type Stdout struct { - Json bool `env:"LOG_STDOUT_JSON"` + Json bool `env:"LOG_STDOUT_JSON" mapstructure:"json"` +} + +type stdoutEnabled struct { + Enabled bool `env:"LOG_STDOUT_ENABLED"` } From 3546c1d7f6bdc25158213f51b690d9c49f53b7a7 Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Sat, 10 Feb 2024 13:35:47 -0600 Subject: [PATCH 06/17] =?UTF-8?q?=E2=9C=A8=20Write=20config=20file?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/config.go | 56 +++++++++++++++++++++++++++++++++++++++++++++++ config/default.go | 27 +++++++++++++++++------ 2 files changed, 76 insertions(+), 7 deletions(-) create mode 100644 cmd/config.go diff --git a/cmd/config.go b/cmd/config.go new file mode 100644 index 0000000..530dc6e --- /dev/null +++ b/cmd/config.go @@ -0,0 +1,56 @@ +/* +Copyright © 2024 Dan Jones + +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 . +*/ +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") +} diff --git a/config/default.go b/config/default.go index d60f003..04aacf2 100644 --- a/config/default.go +++ b/config/default.go @@ -8,23 +8,36 @@ import ( "github.com/BurntSushi/toml" ) -const ConfigStr = ` -[input] -ext = "txt" -recurse = true -path = "%s" +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 DefaultConfig() (Config, error) { +func DefaultStr() string { home, _ := os.UserHomeDir() inDir := fp.Join(home, "my-log") - s := fmt.Sprintf(ConfigStr, inDir) + return fmt.Sprintf(ConfigStr, inDir) +} + +func DefaultConfig() (Config, error) { + s := DefaultStr() c := Config{} _, err := toml.Decode(s, &c) return c, err From cc9e8f6167d0635bbaa1cb2abd7d74b827d44245 Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Sun, 11 Feb 2024 13:50:27 -0600 Subject: [PATCH 07/17] =?UTF-8?q?=E2=9C=A8=20Drop=20command?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/drop.go | 78 ++++++++++++++++++++++++++++--------- config/load.go | 2 +- files/append.go | 42 ++++++++++++++++++++ files/append_test.go | 91 ++++++++++++++++++++++++++++++++++++++++++++ models/entry.go | 6 ++- models/entry_test.go | 18 ++++++++- 6 files changed, 214 insertions(+), 23 deletions(-) create mode 100644 files/append.go create mode 100644 files/append_test.go diff --git a/cmd/drop.go b/cmd/drop.go index d68b6fa..fef062f 100644 --- a/cmd/drop.go +++ b/cmd/drop.go @@ -17,36 +17,76 @@ along with this program. If not, see . package cmd import ( + "encoding/json" "fmt" + "time" + "codeberg.org/danjones000/my-log/files" + "codeberg.org/danjones000/my-log/models" + "codeberg.org/danjones000/my-log/tools" "github.com/spf13/cobra" ) +var dateStr string +var fields map[string]string +var j Json + // dropCmd represents the drop command var dropCmd = &cobra.Command{ - Use: "drop", - Short: "A brief description of your command", - Long: `A longer description that spans multiple lines and likely contains examples -and usage of using your command. 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.`, - Run: func(cmd *cobra.Command, args []string) { - fmt.Println("drop called") + Use: "drop log title", + Short: "Add a new log entry", + // Long: ``, + Args: cobra.ExactArgs(2), + SilenceUsage: true, + RunE: func(cmd *cobra.Command, args []string) error { + log := args[0] + title := args[1] + e := models.PartialEntry() + if len(j.RawMessage) > 8 { + err := json.Unmarshal([]byte(j.RawMessage), &e) + if err != nil { + return err + } + } + for k, v := range fields { + e.Fields = append(e.Fields, models.Meta{k, tools.ParseString(v)}) + } + e.Title = title + e.Date = time.Now().Local() // @todo parse date + l := models.Log{log, []models.Entry{e}} + err := files.Append(l) + if err != nil { + return err + } + by, err := e.MarshalText() + if err != nil { + return err + } + fmt.Fprintf(cmd.OutOrStdout(), "%s\n", by) + return nil }, } func init() { rootCmd.AddCommand(dropCmd) - // Here you will define your flags and configuration settings. - - // Cobra supports Persistent Flags which will work for this command - // and all subcommands, e.g.: - // dropCmd.PersistentFlags().String("foo", "", "A help for foo") - - // Cobra supports local flags which will only run when this command - // is called directly, e.g.: - // dropCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") + dropCmd.Flags().StringVarP(&dateStr, "date", "d", time.Now().Local().Format(time.RFC3339), "Date for log entry") + dropCmd.Flags().StringToStringVarP(&fields, "fields", "f", nil, "Fields you add to entry") + dropCmd.Flags().VarP(&j, "json", "j", "Entire entry as json") +} + +type Json struct { + json.RawMessage +} + +func (j *Json) String() string { + return string(j.RawMessage) +} + +func (j *Json) Set(in string) error { + return json.Unmarshal([]byte(in), &j.RawMessage) +} + +func (j *Json) Type() string { + return "json" } diff --git a/config/load.go b/config/load.go index c561575..04fd95d 100644 --- a/config/load.go +++ b/config/load.go @@ -14,7 +14,7 @@ import ( ) var ConfigPath string -var Overrides map[string]string +var Overrides = map[string]string{} func init() { conf, _ := os.UserConfigDir() diff --git a/files/append.go b/files/append.go new file mode 100644 index 0000000..4600f4d --- /dev/null +++ b/files/append.go @@ -0,0 +1,42 @@ +package files + +import ( + "fmt" + "os" + fp "path/filepath" + "strings" + + "codeberg.org/danjones000/my-log/config" + "codeberg.org/danjones000/my-log/models" +) + +func Append(l models.Log) error { + conf, err := config.Load() + if err != nil { + return err + } + + filename := fmt.Sprintf("%s.%s", strings.ReplaceAll(l.Name, ".", string(os.PathSeparator)), conf.Input.Ext) + path := fp.Join(conf.Input.Path, filename) + dir := fp.Dir(path) + err = os.MkdirAll(dir, 0750) + if err != nil { + return err + } + + f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0640) + if err != nil { + return err + } + defer f.Close() + + for _, e := range l.Entries { + by, err := e.MarshalText() + if err != nil { + continue + } + f.Write(by) + } + + return nil +} diff --git a/files/append_test.go b/files/append_test.go new file mode 100644 index 0000000..48987e4 --- /dev/null +++ b/files/append_test.go @@ -0,0 +1,91 @@ +package files + +import ( + "fmt" + "os" + "testing" + "time" + + "codeberg.org/danjones000/my-log/config" + "codeberg.org/danjones000/my-log/models" + "github.com/stretchr/testify/suite" +) + +func TestAppend(t *testing.T) { + suite.Run(t, new(AppendTestSuite)) +} + +type AppendTestSuite struct { + suite.Suite + dir string +} + +func (s *AppendTestSuite) SetupSuite() { + s.dir, _ = os.MkdirTemp("", "append-test") + config.Overrides["input.path"] = s.dir + config.Overrides["input.ext"] = "log" +} + +func (s *AppendTestSuite) TearDownSuite() { + os.RemoveAll(s.dir) + delete(config.Overrides, "input.path") + delete(config.Overrides, "input.ext") +} + +func (s *AppendTestSuite) TestSuccess() { + when := time.Now().Local() + e := models.Entry{ + Title: "Jimmy", + Date: when, + Fields: []models.Meta{ + {"foo", 42}, + {"bar", true}, + }, + } + l := models.Log{ + Name: "test", + Entries: []models.Entry{e}, + } + err := Append(l) + s.Require().NoError(err) + s.Require().FileExists(s.dir + "/test.log") + // @todo test file contents +} + +func (s *AppendTestSuite) TestConfLoadErr() { + currConf := config.ConfigPath + tmp, _ := os.CreateTemp("", "app-conf-*.toml") + fname := tmp.Name() + defer tmp.Close() + defer os.Remove(fname) + fmt.Fprintln(tmp, `{"not":"toml"}`) + config.ConfigPath = fname + defer func(path string) { + config.ConfigPath = path + }(currConf) + err := Append(models.Log{}) + s.Assert().ErrorContains(err, "toml") +} + +func (s *AppendTestSuite) TestMkdirErr() { + // Don't run this test as root + config.Overrides["input.path"] = "/root/my-logs-test" + defer func(path string) { + config.Overrides["input.path"] = path + }(s.dir) + err := Append(models.Log{}) + s.Assert().ErrorContains(err, "permission denied") +} + +func (s *AppendTestSuite) TestOpenErr() { + l := models.Log{ + Name: "test-open-err", + } + fname := s.dir + "/test-open-err.log" + os.MkdirAll(s.dir, 0750) + f, _ := os.Create(fname) + f.Close() + os.Chmod(fname, 0400) // read only + err := Append(l) + s.Assert().ErrorContains(err, "permission denied") +} diff --git a/models/entry.go b/models/entry.go index 93be3ef..24054b2 100644 --- a/models/entry.go +++ b/models/entry.go @@ -20,6 +20,10 @@ type Entry struct { skipMissing bool } +func PartialEntry() Entry { + return Entry{skipMissing: true} +} + type metaRes struct { out []byte err error @@ -68,7 +72,7 @@ func (e Entry) MarshalText() ([]byte, error) { } ch := e.getFieldMarshalChan() buff := &bytes.Buffer{} - buff.WriteString("@begin ") + buff.WriteString("\n@begin ") buff.WriteString(e.Date.Format(DateFormat)) buff.WriteString(" - ") buff.WriteString(e.Title) diff --git a/models/entry_test.go b/models/entry_test.go index 695524d..cf83298 100644 --- a/models/entry_test.go +++ b/models/entry_test.go @@ -9,6 +9,7 @@ import ( "time" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) // Type assertions @@ -17,6 +18,19 @@ var _ encoding.TextUnmarshaler = new(Entry) var _ json.Marshaler = Entry{} var _ json.Unmarshaler = new(Entry) +func TestPartialEntry(t *testing.T) { + e := PartialEntry() + assert.True(t, e.skipMissing) + err := json.Unmarshal([]byte(`{"a":42}`), &e) + assert.NoError(t, err) + assert.Equal(t, "", e.Title) + assert.Equal(t, time.Time{}, e.Date) + require.Len(t, e.Fields, 1) + f := e.Fields[0] + assert.Equal(t, "a", f.Key) + assert.Equal(t, int64(42), f.Value) +} + func TestEntryMarshal(t *testing.T) { when := time.Now() whens := when.Format(DateFormat) @@ -77,12 +91,12 @@ func getEntryMarshalTestRunner(title string, date time.Time, fields []Meta, firs return } if len(lines) == 0 { - assert.Equal(t, first, string(o)) + assert.Equal(t, "\n"+first, string(o)) return } os := string(o) - assert.Regexp(t, "^"+first, os) + assert.Regexp(t, "^\n"+first, os) for _, line := range lines { assert.Regexp(t, "(?m)^"+line, os) } From c01fb53a0e5122389550abfab8cde9470c1efc8b Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Sun, 11 Feb 2024 17:24:50 -0600 Subject: [PATCH 08/17] =?UTF-8?q?=E2=9C=85=20Fill=20out=20append=20test=20?= =?UTF-8?q?better?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- files/append_test.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/files/append_test.go b/files/append_test.go index 48987e4..da6a5a6 100644 --- a/files/append_test.go +++ b/files/append_test.go @@ -49,7 +49,12 @@ func (s *AppendTestSuite) TestSuccess() { err := Append(l) s.Require().NoError(err) s.Require().FileExists(s.dir + "/test.log") - // @todo test file contents + by, err := os.ReadFile(s.dir + "/test.log") + st := string(by) + s.Require().NoError(err) + s.Assert().Contains(st, "Jimmy\n") + s.Assert().Contains(st, "\n@foo 42") + s.Assert().Contains(st, "\n@bar true") } func (s *AppendTestSuite) TestConfLoadErr() { From 70b82761c2eca1ceff8907f4e5a816f06bfbeaae Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Sat, 24 Feb 2024 20:38:27 -0600 Subject: [PATCH 09/17] =?UTF-8?q?=E2=9C=A8=20Add=20tools.ParseDate?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/root.go | 8 +- cmd/test.go | 73 ++++ config/load_test.go | 1 - cover.html | 863 +++++++++++++++++++++++++++++++++++++++ go.mod | 12 + go.sum | 22 + tools/parse_date.go | 45 ++ tools/parse_date_test.go | 51 +++ 8 files changed, 1067 insertions(+), 8 deletions(-) create mode 100644 cmd/test.go create mode 100644 cover.html create mode 100644 tools/parse_date.go create mode 100644 tools/parse_date_test.go diff --git a/cmd/root.go b/cmd/root.go index a07455d..954a3a7 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -43,7 +43,7 @@ func Execute() { } func init() { - cobra.OnInitialize(initConfig) + //cobra.OnInitialize(initConfig) // Here you will define your flags and configuration settings. // Cobra supports persistent flags, which, if defined here, @@ -56,9 +56,3 @@ func init() { // when this action is called directly. // rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") } - -// initConfig reads in config file and ENV variables if set. -func initConfig() { - // @todo - -} diff --git a/cmd/test.go b/cmd/test.go new file mode 100644 index 0000000..05c426c --- /dev/null +++ b/cmd/test.go @@ -0,0 +1,73 @@ +/* +Copyright © 2024 Dan Jones + +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 . +*/ +package cmd + +import ( + "fmt" + + "codeberg.org/danjones000/my-log/config" + "github.com/spf13/cobra" + // "github.com/BurntSushi/toml" + dp "github.com/markusmobius/go-dateparser" +) + +// testCmd represents the test command +var testCmd = &cobra.Command{ + Use: "test", + Short: "A brief description of your command", + //Long: ``, + Run: func(cmd *cobra.Command, args []string) { + c, err := config.Load() + fmt.Println("error", err) + fmt.Printf("%+v\n", c) + //ne := "output.stdout.config.json = true" + //toml.Decode(ne, &c) + st, _ := c.Outputs.Stdout() + fmt.Printf("%+v\n", st) + //nc := "\n[input]\nrecurse = false\n[output.stdout.config]\njson = true" + //toml.Decode(nc, &c) + //fmt.Printf("%+v\n", c) + d, err := dp.Parse(nil, "now") + fmt.Println(d.Time, d.Period) + d, err = dp.Parse(nil, "today") + fmt.Println(d.Time, d.Period) + d, err = dp.Parse(nil, "2 days, 3 minutes ago") + fmt.Println(d.Time, d.Period) + d, err = dp.Parse(nil, "in 2 decades, 5 days, 3 minutes") + fmt.Println(d.Time, d.Period) + d, err = dp.Parse(nil, "3 years ago") + fmt.Println(d.Time, d.Period) + d, err = dp.Parse(nil, "1707711800") + fmt.Println(d.Time, d.Period) + d, err = dp.Parse(nil, "@1707711800") + fmt.Println(d.Time, d.Period) + }, +} + +func init() { + rootCmd.AddCommand(testCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // testCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // testCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/config/load_test.go b/config/load_test.go index e215ff2..7399316 100644 --- a/config/load_test.go +++ b/config/load_test.go @@ -3,7 +3,6 @@ package config import ( "fmt" "os" - //fp "path/filepath" "testing" "github.com/stretchr/testify/assert" diff --git a/cover.html b/cover.html new file mode 100644 index 0000000..239f0e5 --- /dev/null +++ b/cover.html @@ -0,0 +1,863 @@ + + + + + + config: Go Coverage Report + + + +
+ +
+ not tracked + + no coverage + low coverage + * + * + * + * + * + * + * + * + high coverage + +
+
+
+ + + + + + + + + + + + + + + + + + + +
+ + + diff --git a/go.mod b/go.mod index 4bbf1f1..4928514 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.21.5 require ( github.com/BurntSushi/toml v1.3.2 github.com/caarlos0/env/v10 v10.0.0 + github.com/markusmobius/go-dateparser v1.2.1 github.com/mitchellh/mapstructure v1.5.0 github.com/spf13/cobra v1.8.0 github.com/stretchr/testify v1.8.4 @@ -12,10 +13,21 @@ require ( require ( github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect + github.com/elliotchance/pie/v2 v2.7.0 // indirect + github.com/hablullah/go-hijri v1.0.2 // indirect + github.com/hablullah/go-juliandays v1.0.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/jalaali/go-jalaali v0.0.0-20210801064154-80525e88d958 // indirect github.com/kr/pretty v0.3.1 // indirect + github.com/magefile/mage v1.14.0 // indirect github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect github.com/spf13/pflag v1.0.5 // indirect + github.com/tetratelabs/wazero v1.2.1 // indirect + github.com/wasilibs/go-re2 v1.3.0 // indirect + golang.org/x/exp v0.0.0-20220321173239-a90fa8a75705 // indirect + golang.org/x/text v0.10.0 // indirect gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) + +replace github.com/markusmobius/go-dateparser => github.com/goodevilgenius/go-dateparser v1.2.2 diff --git a/go.sum b/go.sum index c97eec7..350aa50 100644 --- a/go.sum +++ b/go.sum @@ -6,12 +6,24 @@ github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t 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/elliotchance/pie/v2 v2.7.0 h1:FqoIKg4uj0G/CrLGuMS9ejnFKa92lxE1dEgBD3pShXg= +github.com/elliotchance/pie/v2 v2.7.0/go.mod h1:18t0dgGFH006g4eVdDtWfgFZPQEgl10IoEO8YWEq3Og= +github.com/goodevilgenius/go-dateparser v1.2.2 h1:Up9KokPx/h07mesQGAZQg3Xi/8yrDVn1638h3k/lRyk= +github.com/goodevilgenius/go-dateparser v1.2.2/go.mod h1:5xYsZ1h7iB3sE1BSu8bkjYpbFST7EU1/AFxcyO3mgYg= +github.com/hablullah/go-hijri v1.0.2 h1:drT/MZpSZJQXo7jftf5fthArShcaMtsal0Zf/dnmp6k= +github.com/hablullah/go-hijri v1.0.2/go.mod h1:OS5qyYLDjORXzK4O1adFw9Q5WfhOcMdAKglDkcTxgWQ= +github.com/hablullah/go-juliandays v1.0.0 h1:A8YM7wIj16SzlKT0SRJc9CD29iiaUzpBLzh5hr0/5p0= +github.com/hablullah/go-juliandays v1.0.0/go.mod h1:0JOYq4oFOuDja+oospuc61YoX+uNEn7Z6uHYTbBzdGc= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/jalaali/go-jalaali v0.0.0-20210801064154-80525e88d958 h1:qxLoi6CAcXVzjfvu+KXIXJOAsQB62LXjsfbOaErsVzE= +github.com/jalaali/go-jalaali v0.0.0-20210801064154-80525e88d958/go.mod h1:Wqfu7mjUHj9WDzSSPI5KfBclTTEnLveRUFr/ujWnTgE= 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/magefile/mage v1.14.0 h1:6QDX3g6z1YvJ4olPhT1wksUcSa/V0a1B+pJb73fBjyo= +github.com/magefile/mage v1.14.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A= 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/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= @@ -26,6 +38,16 @@ 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/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/tetratelabs/wazero v1.2.1 h1:J4X2hrGzJvt+wqltuvcSjHQ7ujQxA9gb6PeMs4qlUWs= +github.com/tetratelabs/wazero v1.2.1/go.mod h1:wYx2gNRg8/WihJfSDxA1TIL8H+GkfLYm+bIfbblu9VQ= +github.com/wasilibs/go-re2 v1.3.0 h1:LFhBNzoStM3wMie6rN2slD1cuYH2CGiHpvNL3UtcsMw= +github.com/wasilibs/go-re2 v1.3.0/go.mod h1:AafrCXVvGRJJOImMajgJ2M7rVmWyisVK7sFshbxnVrg= +github.com/wasilibs/nottinygc v0.4.0 h1:h1TJMihMC4neN6Zq+WKpLxgd9xCFMw7O9ETLwY2exJQ= +github.com/wasilibs/nottinygc v0.4.0/go.mod h1:oDcIotskuYNMpqMF23l7Z8uzD4TC0WXHK8jetlB3HIo= +golang.org/x/exp v0.0.0-20220321173239-a90fa8a75705 h1:ba9YlqfDGTTQ5aZ2fwOoQ1hf32QySyQkR6ODGDzHlnE= +golang.org/x/exp v0.0.0-20220321173239-a90fa8a75705/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE= +golang.org/x/text v0.10.0 h1:UpjohKhiEgNc0CSauXmwYftY1+LlaC75SJwh0SgCX58= +golang.org/x/text v0.10.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= 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= diff --git a/tools/parse_date.go b/tools/parse_date.go new file mode 100644 index 0000000..5b93673 --- /dev/null +++ b/tools/parse_date.go @@ -0,0 +1,45 @@ +package tools + +import ( + "time" + + dp "github.com/markusmobius/go-dateparser" + "github.com/markusmobius/go-dateparser/date" +) + +const ( + day = time.Hour * 24 +) + +// These are somewhat arbitrary, but reasonably useful min and max times +var ( + MinTime = time.Unix(-2208988800, 0) // Jan 1, 1900 + MaxTime = MinTime.Add(1<<63 - 1) +) + +func ParseDate(in string) (t time.Time, err error) { + if in == "min" { + return MinTime, nil + } + if in == "max" { + return MaxTime, nil + } + + d, err := dp.Parse(nil, in) + if err != nil { + return + } + t = d.Time.Local() + trunc := time.Second + switch d.Period { + case date.Minute: + trunc = time.Minute + case date.Hour: + trunc = time.Hour + case date.Day: + trunc = day + // @todo Handle other cases separately + } + t = t.Truncate(trunc) + return +} diff --git a/tools/parse_date_test.go b/tools/parse_date_test.go new file mode 100644 index 0000000..655c3c1 --- /dev/null +++ b/tools/parse_date_test.go @@ -0,0 +1,51 @@ +package tools + +import ( + "fmt" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestParseDate(t *testing.T) { + now := time.Now().Local() + sec := now.Truncate(time.Second) + today := now.Truncate(day) + tomorrow := today.Add(day) + yesterday := today.Add(-day) + twoMin := now.Add(2 * time.Minute).Truncate(time.Minute) + twoHour := now.Add(2 * time.Hour).Truncate(time.Hour) + tests := []struct { + name string + exp time.Time + err string + }{ + {"now", sec, ""}, + {"today", today, ""}, + {"tomorrow", tomorrow, ""}, + {"yesterday", yesterday, ""}, + {"in two minutes", twoMin, ""}, + {"in two hours", twoHour, ""}, + {"min", MinTime, ""}, + {"max", MaxTime, ""}, + {"not a date", now, fmt.Sprintf(`failed to parse "%s": unknown format`, "not a date")}, + } + for _, tt := range tests { + t.Run(tt.name, getDateTest(tt.name, tt.exp, tt.err)) + } +} + +func getDateTest(in string, exp time.Time, err string) func(t *testing.T) { + return func(t *testing.T) { + out, er := ParseDate(in) + if err != "" { + assert.ErrorContains(t, er, err) + } else { + require.NoError(t, er) + + assert.Equal(t, exp, out) + } + } +} From 44e79916d3ef6776a20f74edb154db7c9a09eec8 Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Sun, 25 Feb 2024 09:53:52 -0600 Subject: [PATCH 10/17] =?UTF-8?q?=F0=9F=90=9B=20Only=20truncate=20minutes?= =?UTF-8?q?=20and=20seconds?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Also ensure we're getting date.Time for specific times --- tools/parse_date.go | 29 +++++++++++++++++------------ tools/parse_date_test.go | 18 ++++++++++++++++-- 2 files changed, 33 insertions(+), 14 deletions(-) diff --git a/tools/parse_date.go b/tools/parse_date.go index 5b93673..0fd0cb2 100644 --- a/tools/parse_date.go +++ b/tools/parse_date.go @@ -7,10 +7,6 @@ import ( "github.com/markusmobius/go-dateparser/date" ) -const ( - day = time.Hour * 24 -) - // These are somewhat arbitrary, but reasonably useful min and max times var ( MinTime = time.Unix(-2208988800, 0) // Jan 1, 1900 @@ -25,21 +21,30 @@ func ParseDate(in string) (t time.Time, err error) { return MaxTime, nil } - d, err := dp.Parse(nil, in) + d, err := dp.Parse(&dp.Configuration{ + CurrentTime: time.Now().Local(), + ReturnTimeAsPeriod: true, + }, in) + t = d.Time if err != nil { return } - t = d.Time.Local() - trunc := time.Second + + y, mon, day, h, loc := t.Year(), t.Month(), t.Day(), t.Hour(), t.Location() switch d.Period { + case date.Second: + t = t.Truncate(time.Second) case date.Minute: - trunc = time.Minute + t = t.Truncate(time.Minute) case date.Hour: - trunc = time.Hour + t = time.Date(y, mon, day, h, 0, 0, 0, loc) case date.Day: - trunc = day - // @todo Handle other cases separately + t = time.Date(y, mon, day, 0, 0, 0, 0, loc) + case date.Month: + t = time.Date(y, mon, 1, 0, 0, 0, 0, loc) + case date.Year: + t = time.Date(y, 1, 1, 0, 0, 0, 0, loc) } - t = t.Truncate(trunc) + return } diff --git a/tools/parse_date_test.go b/tools/parse_date_test.go index 655c3c1..5ba355d 100644 --- a/tools/parse_date_test.go +++ b/tools/parse_date_test.go @@ -9,14 +9,24 @@ import ( "github.com/stretchr/testify/require" ) +const day = time.Hour * 24 + func TestParseDate(t *testing.T) { now := time.Now().Local() + y, mon, d, h, loc := now.Year(), now.Month(), now.Day(), now.Hour(), now.Location() sec := now.Truncate(time.Second) - today := now.Truncate(day) + today := time.Date(y, mon, d, 0, 0, 0, 0, loc) tomorrow := today.Add(day) yesterday := today.Add(-day) twoMin := now.Add(2 * time.Minute).Truncate(time.Minute) - twoHour := now.Add(2 * time.Hour).Truncate(time.Hour) + twoHour := time.Date(y, mon, d, h+2, 0, 0, 0, loc) + firstMonth := time.Date(y, mon, 1, 0, 0, 0, 0, loc) + firstYear := time.Date(y, 1, 1, 0, 0, 0, 0, loc) + exact := "2075-02-12T12:13:54.536-02:00" + exactd, _ := time.ParseInLocation(time.RFC3339, exact, time.FixedZone("UTC-02:00", -2*60*60)) + var ts int64 = 1708876012 + tsd := time.Unix(ts, 0) + tests := []struct { name string exp time.Time @@ -28,8 +38,12 @@ func TestParseDate(t *testing.T) { {"yesterday", yesterday, ""}, {"in two minutes", twoMin, ""}, {"in two hours", twoHour, ""}, + {"this month", firstMonth, ""}, + {"this year", firstYear, ""}, {"min", MinTime, ""}, {"max", MaxTime, ""}, + {exact, exactd, ""}, + {fmt.Sprint(ts), tsd, ""}, {"not a date", now, fmt.Sprintf(`failed to parse "%s": unknown format`, "not a date")}, } for _, tt := range tests { From 391452e3d95ada3dc38d1de3be033790cbc6b234 Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Sun, 25 Feb 2024 12:36:43 -0600 Subject: [PATCH 11/17] =?UTF-8?q?=E2=9C=A8=20ParseDate=20should=20handle?= =?UTF-8?q?=20DateFormat?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 + cmd/test.go | 73 ---- cover.html | 863 --------------------------------------- models/entry.go | 4 +- tools/parse_date.go | 10 +- tools/parse_date_test.go | 5 +- 6 files changed, 17 insertions(+), 940 deletions(-) delete mode 100644 cmd/test.go delete mode 100644 cover.html diff --git a/.gitignore b/.gitignore index 461da1e..3a550dc 100644 --- a/.gitignore +++ b/.gitignore @@ -123,3 +123,5 @@ Temporary Items # End of https://www.toptal.com/developers/gitignore/api/go,linux,emacs,macos my-log +cover.html +cmd/test.go diff --git a/cmd/test.go b/cmd/test.go deleted file mode 100644 index 05c426c..0000000 --- a/cmd/test.go +++ /dev/null @@ -1,73 +0,0 @@ -/* -Copyright © 2024 Dan Jones - -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 . -*/ -package cmd - -import ( - "fmt" - - "codeberg.org/danjones000/my-log/config" - "github.com/spf13/cobra" - // "github.com/BurntSushi/toml" - dp "github.com/markusmobius/go-dateparser" -) - -// testCmd represents the test command -var testCmd = &cobra.Command{ - Use: "test", - Short: "A brief description of your command", - //Long: ``, - Run: func(cmd *cobra.Command, args []string) { - c, err := config.Load() - fmt.Println("error", err) - fmt.Printf("%+v\n", c) - //ne := "output.stdout.config.json = true" - //toml.Decode(ne, &c) - st, _ := c.Outputs.Stdout() - fmt.Printf("%+v\n", st) - //nc := "\n[input]\nrecurse = false\n[output.stdout.config]\njson = true" - //toml.Decode(nc, &c) - //fmt.Printf("%+v\n", c) - d, err := dp.Parse(nil, "now") - fmt.Println(d.Time, d.Period) - d, err = dp.Parse(nil, "today") - fmt.Println(d.Time, d.Period) - d, err = dp.Parse(nil, "2 days, 3 minutes ago") - fmt.Println(d.Time, d.Period) - d, err = dp.Parse(nil, "in 2 decades, 5 days, 3 minutes") - fmt.Println(d.Time, d.Period) - d, err = dp.Parse(nil, "3 years ago") - fmt.Println(d.Time, d.Period) - d, err = dp.Parse(nil, "1707711800") - fmt.Println(d.Time, d.Period) - d, err = dp.Parse(nil, "@1707711800") - fmt.Println(d.Time, d.Period) - }, -} - -func init() { - rootCmd.AddCommand(testCmd) - - // Here you will define your flags and configuration settings. - - // Cobra supports Persistent Flags which will work for this command - // and all subcommands, e.g.: - // testCmd.PersistentFlags().String("foo", "", "A help for foo") - - // Cobra supports local flags which will only run when this command - // is called directly, e.g.: - // testCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") -} diff --git a/cover.html b/cover.html deleted file mode 100644 index 239f0e5..0000000 --- a/cover.html +++ /dev/null @@ -1,863 +0,0 @@ - - - - - - config: Go Coverage Report - - - -
- -
- not tracked - - no coverage - low coverage - * - * - * - * - * - * - * - * - high coverage - -
-
-
- - - - - - - - - - - - - - - - - - - -
- - - diff --git a/models/entry.go b/models/entry.go index 24054b2..8b8f3bc 100644 --- a/models/entry.go +++ b/models/entry.go @@ -9,9 +9,11 @@ import ( "strings" "sync" "time" + + "codeberg.org/danjones000/my-log/tools" ) -const DateFormat = "January 02, 2006 at 03:04:05PM -0700" +const DateFormat = tools.DateFormat type Entry struct { Title string diff --git a/tools/parse_date.go b/tools/parse_date.go index 0fd0cb2..e110f0f 100644 --- a/tools/parse_date.go +++ b/tools/parse_date.go @@ -7,6 +7,8 @@ import ( "github.com/markusmobius/go-dateparser/date" ) +const DateFormat = "January 02, 2006 at 03:04:05PM -0700" + // These are somewhat arbitrary, but reasonably useful min and max times var ( MinTime = time.Unix(-2208988800, 0) // Jan 1, 1900 @@ -21,12 +23,16 @@ func ParseDate(in string) (t time.Time, err error) { return MaxTime, nil } - d, err := dp.Parse(&dp.Configuration{ + conf := dp.Configuration{ CurrentTime: time.Now().Local(), ReturnTimeAsPeriod: true, - }, in) + } + + d, err := dp.Parse(&conf, in) t = d.Time if err != nil { + d, err = dp.Parse(&conf, in, DateFormat) + t = d.Time return } diff --git a/tools/parse_date_test.go b/tools/parse_date_test.go index 5ba355d..cfaadd1 100644 --- a/tools/parse_date_test.go +++ b/tools/parse_date_test.go @@ -23,9 +23,11 @@ func TestParseDate(t *testing.T) { firstMonth := time.Date(y, mon, 1, 0, 0, 0, 0, loc) firstYear := time.Date(y, 1, 1, 0, 0, 0, 0, loc) exact := "2075-02-12T12:13:54.536-02:00" - exactd, _ := time.ParseInLocation(time.RFC3339, exact, time.FixedZone("UTC-02:00", -2*60*60)) + exactd, _ := time.ParseInLocation(time.RFC3339, exact, time.FixedZone("UTC-02:00", -7200)) var ts int64 = 1708876012 tsd := time.Unix(ts, 0) + ent := "February 25, 2024 at 04:00:13AM +0230" + entd, _ := time.Parse(DateFormat, ent) tests := []struct { name string @@ -44,6 +46,7 @@ func TestParseDate(t *testing.T) { {"max", MaxTime, ""}, {exact, exactd, ""}, {fmt.Sprint(ts), tsd, ""}, + {ent, entd, ""}, {"not a date", now, fmt.Sprintf(`failed to parse "%s": unknown format`, "not a date")}, } for _, tt := range tests { From 96c3b2ff300003fa5ea70d50ad471822bf6d3ec4 Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Sun, 25 Feb 2024 13:12:13 -0600 Subject: [PATCH 12/17] =?UTF-8?q?=E2=9C=A8=20Use=20ParseDate=20in=20Parse?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Also limit to English, or we get lots of false positives --- tools/parse.go | 3 +-- tools/parse_date.go | 1 + tools/parse_test.go | 7 +++++-- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/tools/parse.go b/tools/parse.go index 38d7b5e..021c6a2 100644 --- a/tools/parse.go +++ b/tools/parse.go @@ -5,7 +5,6 @@ import ( "regexp" "strconv" "strings" - "time" ) func ParseBytes(in []byte) any { @@ -34,7 +33,7 @@ func ParseString(in string) any { 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 { + } else if t, err := ParseDate(s); err == nil { return t } else if err := json.Unmarshal([]byte(s), &j); err == nil { return j diff --git a/tools/parse_date.go b/tools/parse_date.go index e110f0f..d060299 100644 --- a/tools/parse_date.go +++ b/tools/parse_date.go @@ -26,6 +26,7 @@ func ParseDate(in string) (t time.Time, err error) { conf := dp.Configuration{ CurrentTime: time.Now().Local(), ReturnTimeAsPeriod: true, + Languages: []string{"en"}, } d, err := dp.Parse(&conf, in) diff --git a/tools/parse_test.go b/tools/parse_test.go index 2e8e49f..826af9b 100644 --- a/tools/parse_test.go +++ b/tools/parse_test.go @@ -10,6 +10,7 @@ import ( func TestParse(t *testing.T) { when := time.Now() + now := when.Local().Truncate(time.Second) tests := []struct { name string in string @@ -22,6 +23,8 @@ func TestParse(t *testing.T) { {"false", "false", false}, {"nil", "nil", nil}, {"time", when.Format(time.RFC3339), when}, + {"now", "now", now}, + {"DateFormat", now.Format(DateFormat), now}, {"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", "", ""}, @@ -48,7 +51,7 @@ func getParseTestRunner(in string, exp any) func(*testing.T) { 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)) + assert.WithinRange(t, expT, ti.Add(-2*time.Second), ti.Add(2*time.Second)) } } else { assert.Equal(t, exp, out) @@ -57,7 +60,7 @@ func getParseTestRunner(in string, exp any) func(*testing.T) { 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)) + assert.WithinRange(t, expT, ti.Add(-2*time.Second), ti.Add(2*time.Second)) } } else { assert.Equal(t, exp, out) From c1b1ceb283dd71385af679394801b5e9286a87e6 Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Sun, 25 Feb 2024 13:39:12 -0600 Subject: [PATCH 13/17] =?UTF-8?q?=E2=9C=A8=20Use=20ParseDate=20in=20Entry?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- models/entry.go | 11 ++++------- models/entry_test.go | 10 +++++++++- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/models/entry.go b/models/entry.go index 8b8f3bc..d504536 100644 --- a/models/entry.go +++ b/models/entry.go @@ -109,12 +109,9 @@ func (m *Entry) UnmarshalText(in []byte) error { if date == "" { return ErrorMissingDate } - d, e := time.Parse(time.RFC3339, date) + d, e := tools.ParseDate(date) if e != nil { - d, e = time.Parse(DateFormat, date) - if e != nil { - return newParsingError(e) - } + return newParsingError(e) } m.Date = d @@ -270,7 +267,7 @@ func (e *Entry) UnmarshalJSON(in []byte) error { if (!ok || dates == "") && !e.skipMissing { return ErrorMissingDate } - date, err := time.Parse(time.RFC3339, dates) + date, err := tools.ParseDate(dates) if err != nil && !e.skipMissing { return newParsingError(err) } @@ -280,7 +277,7 @@ func (e *Entry) UnmarshalJSON(in []byte) error { if m.Key == "title" || m.Key == "date" { continue } else if vs, ok := m.Value.(string); ok { - if vd, err := time.Parse(time.RFC3339, vs); err == nil { + if vd, err := tools.ParseDate(vs); err == nil { m.Value = vd } else { m.Value = vs diff --git a/models/entry_test.go b/models/entry_test.go index cf83298..a8bec44 100644 --- a/models/entry_test.go +++ b/models/entry_test.go @@ -280,7 +280,7 @@ func TestEntryJsonUnmarshal(t *testing.T) { }, { "date-field", - `{"title":"A Title","date":"` + whens + `","posted":"` + when.Add(-time.Hour).Format(time.RFC3339) + `"}`, + `{"title":"A Title","date":"` + whens + `","posted":"` + when.Add(-time.Hour).In(time.UTC).Format(time.RFC3339) + `"}`, "A Title", when, []Meta{{"posted", when.Add(-time.Hour)}}, @@ -324,6 +324,7 @@ func getEntryJsonUnmarshalTestRunner(in, title string, date time.Time, fields [] assert.Len(t, e.Fields, len(fields)) for _, f := range fields { got := false + fTime, isTime := f.Value.(time.Time) for _, m := range e.Fields { var mVal any = m.Value var fVal any = f.Value @@ -337,6 +338,13 @@ func getEntryJsonUnmarshalTestRunner(in, title string, date time.Time, fields [] got = true break } + if isTime && m.Key == f.Key { + mTime, _ := mVal.(time.Time) + if assert.WithinRange(t, mTime, fTime.Add(-2*time.Second), fTime.Add(2*time.Second)) { + got = true + break + } + } } assert.Truef(t, got, "Couldn't find field %+v. We have %+v", f, e.Fields) } From bc6c3d0f369eb16b82968cad8643c1986490906f Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Sun, 25 Feb 2024 15:25:34 -0600 Subject: [PATCH 14/17] =?UTF-8?q?=E2=9C=A8=20Parse=20date=20in=20drop=20co?= =?UTF-8?q?mmand?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/drop.go | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/cmd/drop.go b/cmd/drop.go index fef062f..f7fc43d 100644 --- a/cmd/drop.go +++ b/cmd/drop.go @@ -27,7 +27,7 @@ import ( "github.com/spf13/cobra" ) -var dateStr string +var d Date var fields map[string]string var j Json @@ -52,7 +52,7 @@ var dropCmd = &cobra.Command{ e.Fields = append(e.Fields, models.Meta{k, tools.ParseString(v)}) } e.Title = title - e.Date = time.Now().Local() // @todo parse date + e.Date = d.t l := models.Log{log, []models.Entry{e}} err := files.Append(l) if err != nil { @@ -70,7 +70,8 @@ var dropCmd = &cobra.Command{ func init() { rootCmd.AddCommand(dropCmd) - dropCmd.Flags().StringVarP(&dateStr, "date", "d", time.Now().Local().Format(time.RFC3339), "Date for log entry") + (&d).Set("now") + dropCmd.Flags().VarP(&d, "date", "d", "Date for log entry") dropCmd.Flags().StringToStringVarP(&fields, "fields", "f", nil, "Fields you add to entry") dropCmd.Flags().VarP(&j, "json", "j", "Entire entry as json") } @@ -90,3 +91,22 @@ func (j *Json) Set(in string) error { func (j *Json) Type() string { return "json" } + +type Date struct { + s string + t time.Time +} + +func (d *Date) String() string { + return d.s +} + +func (d *Date) Set(in string) (err error) { + d.s = in + d.t, err = tools.ParseDate(in) + return +} + +func (d *Date) Type() string { + return "datetime" +} From 0687671c7d0090089b3e3133e0d9678cc122673c Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Sun, 25 Feb 2024 15:53:37 -0600 Subject: [PATCH 15/17] =?UTF-8?q?=F0=9F=90=9B=20Ensure=20config=20director?= =?UTF-8?q?y=20exists?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/config.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/cmd/config.go b/cmd/config.go index 530dc6e..dc28dd7 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -19,6 +19,7 @@ package cmd import ( "fmt" "os" + fp "path/filepath" "codeberg.org/danjones000/my-log/config" "github.com/spf13/cobra" @@ -30,22 +31,28 @@ var configCmd = &cobra.Command{ Short: "Save default config to file", //Long: ``, SilenceUsage: true, - RunE: func(cmd *cobra.Command, args []string) error { + RunE: func(cmd *cobra.Command, args []string) (err error) { force, _ := cmd.Flags().GetBool("force") if !force { - _, err := os.Stat(config.ConfigPath) + _, err = os.Stat(config.ConfigPath) if !os.IsNotExist(err) { return fmt.Errorf("%s already exists. Use -f to overwrite", config.ConfigPath) } } + dir := fp.Dir(config.ConfigPath) + err = os.MkdirAll(dir, 0755) + if err != nil { + return + } + f, err := os.Create(config.ConfigPath) if err != nil { - return err + return } defer f.Close() c := config.DefaultStr() fmt.Fprint(f, c) - return nil + return }, } From a61af1b4b317e9deafa9af69204da6f54fb200cd Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Mon, 26 Feb 2024 19:30:32 -0600 Subject: [PATCH 16/17] =?UTF-8?q?=E2=9C=A8=20Use=20dot=20as=20folder=20sep?= =?UTF-8?q?arator?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Also don't add dot if no extension --- config/default.go | 2 ++ config/types.go | 7 +++--- files/append.go | 9 ++++++- files/append_test.go | 60 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+), 4 deletions(-) diff --git a/config/default.go b/config/default.go index 04aacf2..461f5c3 100644 --- a/config/default.go +++ b/config/default.go @@ -17,6 +17,8 @@ path = "%s" ext = "txt" # Whether to look in sub-folders recurse = true +# Whether to use a dot as a folder separator in log names +dotFolder = true # config for output types [output] diff --git a/config/types.go b/config/types.go index b218509..a3a3681 100644 --- a/config/types.go +++ b/config/types.go @@ -6,9 +6,10 @@ type Config struct { } type Input struct { - Path string `env:"LOG_PATH"` - Recurse bool `env:"LOG_RECURSE"` - Ext string `env:"LOG_EXT"` + Path string `env:"LOG_PATH"` + Recurse bool `env:"LOG_RECURSE"` + Ext string `env:"LOG_EXT"` + DotFolder bool `env:"LOG_DOT_FOLDER"` } type Outputs map[string]Output diff --git a/files/append.go b/files/append.go index 4600f4d..bd9f4ee 100644 --- a/files/append.go +++ b/files/append.go @@ -16,7 +16,14 @@ func Append(l models.Log) error { return err } - filename := fmt.Sprintf("%s.%s", strings.ReplaceAll(l.Name, ".", string(os.PathSeparator)), conf.Input.Ext) + filename := l.Name + if conf.Input.DotFolder { + filename = strings.ReplaceAll(filename, ".", string(os.PathSeparator)) + } + + if conf.Input.Ext != "" { + filename = fmt.Sprintf("%s.%s", filename, conf.Input.Ext) + } path := fp.Join(conf.Input.Path, filename) dir := fp.Dir(path) err = os.MkdirAll(dir, 0750) diff --git a/files/append_test.go b/files/append_test.go index da6a5a6..487acfd 100644 --- a/files/append_test.go +++ b/files/append_test.go @@ -57,6 +57,66 @@ func (s *AppendTestSuite) TestSuccess() { s.Assert().Contains(st, "\n@bar true") } +func (s *AppendTestSuite) TestDotFolder() { + config.Overrides["input.dotFolder"] = "true" + e := models.Entry{ + Title: "something", + Date: time.Now(), + } + l := models.Log{ + Name: "sub.test", + Entries: []models.Entry{e}, + } + err := Append(l) + s.Require().NoError(err) + s.Require().FileExists(s.dir + "/sub/test.log") + by, err := os.ReadFile(s.dir + "/sub/test.log") + st := string(by) + s.Require().NoError(err) + s.Assert().Contains(st, "something @end") +} + +func (s *AppendTestSuite) TestDotFolderNo() { + config.Overrides["input.dotFolder"] = "false" + e := models.Entry{ + Title: "another", + Date: time.Now(), + } + l := models.Log{ + Name: "sub.test", + Entries: []models.Entry{e}, + } + err := Append(l) + s.Require().NoError(err) + s.Require().FileExists(s.dir + "/sub.test.log") + by, err := os.ReadFile(s.dir + "/sub.test.log") + st := string(by) + s.Require().NoError(err) + s.Assert().Contains(st, "another @end") +} + +func (s *AppendTestSuite) TestNoExt() { + config.Overrides["input.ext"] = "" + defer func() { + config.Overrides["input.ext"] = "log" + }() + e := models.Entry{ + Title: "baz", + Date: time.Now(), + } + l := models.Log{ + Name: "foobar", + Entries: []models.Entry{e}, + } + err := Append(l) + s.Require().NoError(err) + s.Require().FileExists(s.dir + "/foobar") + by, err := os.ReadFile(s.dir + "/foobar") + st := string(by) + s.Require().NoError(err) + s.Assert().Contains(st, "baz @end") +} + func (s *AppendTestSuite) TestConfLoadErr() { currConf := config.ConfigPath tmp, _ := os.CreateTemp("", "app-conf-*.toml") From a39665747d23db9337ba91935fab13dc1c8c3939 Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Sat, 2 Mar 2024 15:06:43 -0600 Subject: [PATCH 17/17] =?UTF-8?q?=F0=9F=93=9D=20Added=20CHANGELOG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..122a538 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,16 @@ +# Changelog + +## [0.0.1] - 2024-03-02 + +🎉 Initial release. + +For this first release, we only have the initial `my-log drop` command implemented, which adds the ability to add new entries to a log. + +We also have `my-log config` to create the initial config file. + +Parsing the log files will be added to a future release, but the generated logs are compatible with [droplogger](https://github.com/goodevilgenius/droplogger), which can still be used. + +### Added + +- `my-log drop`: adds a new log entry +- `my-log config`: copies the default config to the default file location