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) + } + } +}