🔀 Merge branch 'rel/0.0.1' into stable
This commit is contained in:
commit
bcfbbd730e
21 changed files with 942 additions and 160 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -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
|
||||
|
|
|
|||
16
CHANGELOG.md
Normal file
16
CHANGELOG.md
Normal file
|
|
@ -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
|
||||
63
cmd/config.go
Normal file
63
cmd/config.go
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
/*
|
||||
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"
|
||||
fp "path/filepath"
|
||||
|
||||
"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) (err 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)
|
||||
}
|
||||
}
|
||||
dir := fp.Dir(config.ConfigPath)
|
||||
err = os.MkdirAll(dir, 0755)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
f, err := os.Create(config.ConfigPath)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
c := config.DefaultStr()
|
||||
fmt.Fprint(f, c)
|
||||
return
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(configCmd)
|
||||
|
||||
configCmd.Flags().BoolP("force", "f", false, "Force overwrite")
|
||||
}
|
||||
98
cmd/drop.go
98
cmd/drop.go
|
|
@ -17,36 +17,96 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||
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 d Date
|
||||
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 = d.t
|
||||
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")
|
||||
(&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")
|
||||
}
|
||||
|
||||
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"
|
||||
}
|
||||
|
||||
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"
|
||||
}
|
||||
|
|
|
|||
43
cmd/root.go
43
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) { },
|
||||
|
|
@ -51,39 +43,16 @@ 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,
|
||||
// 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")
|
||||
}
|
||||
|
||||
// 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())
|
||||
}
|
||||
// rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||
}
|
||||
|
|
|
|||
46
config/default.go
Normal file
46
config/default.go
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
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
|
||||
# Whether to use a dot as a folder separator in log names
|
||||
dotFolder = 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
|
||||
}
|
||||
76
config/load_test.go
Normal file
76
config/load_test.go
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"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)
|
||||
}
|
||||
28
config/types.go
Normal file
28
config/types.go
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
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"`
|
||||
DotFolder bool `env:"LOG_DOT_FOLDER"`
|
||||
}
|
||||
|
||||
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"`
|
||||
}
|
||||
49
files/append.go
Normal file
49
files/append.go
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
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 := 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)
|
||||
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
|
||||
}
|
||||
156
files/append_test.go
Normal file
156
files/append_test.go
Normal file
|
|
@ -0,0 +1,156 @@
|
|||
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")
|
||||
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) 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")
|
||||
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")
|
||||
}
|
||||
35
go.mod
35
go.mod
|
|
@ -3,32 +3,31 @@ 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/markusmobius/go-dateparser v1.2.1
|
||||
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/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/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/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/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
|
||||
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
|
||||
|
|
|
|||
76
go.sum
76
go.sum
|
|
@ -1,75 +1,55 @@
|
|||
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/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/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
|
||||
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
|
||||
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/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=
|
||||
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=
|
||||
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=
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -20,6 +22,10 @@ type Entry struct {
|
|||
skipMissing bool
|
||||
}
|
||||
|
||||
func PartialEntry() Entry {
|
||||
return Entry{skipMissing: true}
|
||||
}
|
||||
|
||||
type metaRes struct {
|
||||
out []byte
|
||||
err error
|
||||
|
|
@ -68,7 +74,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)
|
||||
|
|
@ -103,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
|
||||
|
||||
|
|
@ -264,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)
|
||||
}
|
||||
|
|
@ -274,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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
@ -266,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)}},
|
||||
|
|
@ -310,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
|
||||
|
|
@ -323,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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
43
tools/parse.go
Normal file
43
tools/parse.go
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
package tools
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
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 := ParseDate(s); err == nil {
|
||||
return t
|
||||
} else if err := json.Unmarshal([]byte(s), &j); err == nil {
|
||||
return j
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
57
tools/parse_date.go
Normal file
57
tools/parse_date.go
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
package tools
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
dp "github.com/markusmobius/go-dateparser"
|
||||
"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
|
||||
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
|
||||
}
|
||||
|
||||
conf := dp.Configuration{
|
||||
CurrentTime: time.Now().Local(),
|
||||
ReturnTimeAsPeriod: true,
|
||||
Languages: []string{"en"},
|
||||
}
|
||||
|
||||
d, err := dp.Parse(&conf, in)
|
||||
t = d.Time
|
||||
if err != nil {
|
||||
d, err = dp.Parse(&conf, in, DateFormat)
|
||||
t = d.Time
|
||||
return
|
||||
}
|
||||
|
||||
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:
|
||||
t = t.Truncate(time.Minute)
|
||||
case date.Hour:
|
||||
t = time.Date(y, mon, day, h, 0, 0, 0, loc)
|
||||
case date.Day:
|
||||
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)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
68
tools/parse_date_test.go
Normal file
68
tools/parse_date_test.go
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
package tools
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"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 := 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 := 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", -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
|
||||
exp time.Time
|
||||
err string
|
||||
}{
|
||||
{"now", sec, ""},
|
||||
{"today", today, ""},
|
||||
{"tomorrow", tomorrow, ""},
|
||||
{"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, ""},
|
||||
{ent, entd, ""},
|
||||
{"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)
|
||||
}
|
||||
}
|
||||
}
|
||||
69
tools/parse_test.go
Normal file
69
tools/parse_test.go
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
package tools
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestParse(t *testing.T) {
|
||||
when := time.Now()
|
||||
now := when.Local().Truncate(time.Second)
|
||||
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},
|
||||
{"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", "", ""},
|
||||
{"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(-2*time.Second), ti.Add(2*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(-2*time.Second), ti.Add(2*time.Second))
|
||||
}
|
||||
} else {
|
||||
assert.Equal(t, exp, out)
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue