Compare commits
No commits in common. "9ca2b9156d77cba492b67e6b6688312fba26fe50" and "e67f159ce712822575412a2fb523b1e0354612bc" have entirely different histories.
9ca2b9156d
...
e67f159ce7
15 changed files with 28 additions and 146 deletions
|
|
@ -1,14 +1,5 @@
|
||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
## [0.0.4] - 2024-05-08
|
|
||||||
|
|
||||||
- ✨ Add -p flag to config to print config path
|
|
||||||
|
|
||||||
## [0.0.3] - 2024-03-11
|
|
||||||
|
|
||||||
- ✨ Add JSON formatter
|
|
||||||
- 💥 Breaking change: renamed output.stdout.config value formatter to format
|
|
||||||
|
|
||||||
## [0.0.2] - 2024-03-09
|
## [0.0.2] - 2024-03-09
|
||||||
|
|
||||||
- ✨ Use plain formatter to output entry from drop
|
- ✨ Use plain formatter to output entry from drop
|
||||||
|
|
|
||||||
|
|
@ -123,7 +123,7 @@ Each separate output has its own set of configuration. So, replace `which-one` w
|
||||||
|
|
||||||
*This section may change in the near future. We're considering supporting multiple formats.*
|
*This section may change in the near future. We're considering supporting multiple formats.*
|
||||||
|
|
||||||
- `format`: Which formatter to use when outputting data. This value is also used by `my-log drop` to output the new entry.
|
- `formatter`: Which formatter to use when outputting data. This value is used by `my-log drop` to output the new entry.
|
||||||
|
|
||||||
### `[formatters]`
|
### `[formatters]`
|
||||||
|
|
||||||
|
|
@ -144,7 +144,7 @@ Some formatters may have custom configuration.
|
||||||
+ [ ] filter to specific logs
|
+ [ ] filter to specific logs
|
||||||
+ [ ] stdout
|
+ [ ] stdout
|
||||||
- [x] plain text
|
- [x] plain text
|
||||||
- [x] JSON
|
- [ ] JSON
|
||||||
- [ ] YAML
|
- [ ] YAML
|
||||||
- [ ] Other formats? Submit an issue!
|
- [ ] Other formats? Submit an issue!
|
||||||
+ [ ] file output
|
+ [ ] file output
|
||||||
|
|
|
||||||
|
|
@ -32,11 +32,6 @@ var configCmd = &cobra.Command{
|
||||||
//Long: ``,
|
//Long: ``,
|
||||||
SilenceUsage: true,
|
SilenceUsage: true,
|
||||||
RunE: func(cmd *cobra.Command, args []string) (err error) {
|
RunE: func(cmd *cobra.Command, args []string) (err error) {
|
||||||
print, _ := cmd.Flags().GetBool("print")
|
|
||||||
if print {
|
|
||||||
fmt.Fprintln(cmd.OutOrStdout(), config.ConfigPath)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
force, _ := cmd.Flags().GetBool("force")
|
force, _ := cmd.Flags().GetBool("force")
|
||||||
if !force {
|
if !force {
|
||||||
_, err = os.Stat(config.ConfigPath)
|
_, err = os.Stat(config.ConfigPath)
|
||||||
|
|
@ -65,5 +60,4 @@ func init() {
|
||||||
rootCmd.AddCommand(configCmd)
|
rootCmd.AddCommand(configCmd)
|
||||||
|
|
||||||
configCmd.Flags().BoolP("force", "f", false, "Force overwrite")
|
configCmd.Flags().BoolP("force", "f", false, "Force overwrite")
|
||||||
configCmd.Flags().BoolP("print", "p", false, "Print path only")
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
13
cmd/drop.go
13
cmd/drop.go
|
|
@ -42,17 +42,17 @@ var dropCmd = &cobra.Command{
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
log := args[0]
|
log := args[0]
|
||||||
title := args[1]
|
title := args[1]
|
||||||
ms := &models.Metas{}
|
ms := models.Metas{}
|
||||||
if len(j.RawMessage) > 8 {
|
if len(j.RawMessage) > 8 {
|
||||||
err := json.Unmarshal([]byte(j.RawMessage), ms)
|
err := json.Unmarshal([]byte(j.RawMessage), &ms)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for k, v := range fields {
|
for k, v := range fields {
|
||||||
ms.AppendTo(k, tools.ParseString(v))
|
ms = append(ms, models.Meta{k, tools.ParseString(v)})
|
||||||
}
|
}
|
||||||
e := models.Entry{title, d.t, *ms}
|
e := models.Entry{title, d.t, ms}
|
||||||
l := models.Log{log, []models.Entry{e}}
|
l := models.Log{log, []models.Entry{e}}
|
||||||
err := files.Append(l)
|
err := files.Append(l)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -67,10 +67,7 @@ var dropCmd = &cobra.Command{
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if len(out) > 0 && out[len(out)-1] != 10 {
|
fmt.Fprintf(cmd.OutOrStdout(), "%s\n", out)
|
||||||
out = append(out, 10)
|
|
||||||
}
|
|
||||||
fmt.Fprintf(cmd.OutOrStdout(), "%s", out)
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -50,7 +50,7 @@ func init() {
|
||||||
// will be global for your application.
|
// will be global for your application.
|
||||||
|
|
||||||
rootCmd.PersistentFlags().StringVarP(&config.ConfigPath, "config", "c", config.ConfigPath, "config file")
|
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.format=json")
|
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.formatter=json")
|
||||||
|
|
||||||
// Cobra also supports local flags, which will only run
|
// Cobra also supports local flags, which will only run
|
||||||
// when this action is called directly.
|
// when this action is called directly.
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,7 @@ dotFolder = true
|
||||||
enabled = true
|
enabled = true
|
||||||
[output.stdout.config]
|
[output.stdout.config]
|
||||||
# Formatter to use when outputting to stdout
|
# Formatter to use when outputting to stdout
|
||||||
format = "plain"
|
formatter = "plain"
|
||||||
|
|
||||||
[formatters]
|
[formatters]
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -70,14 +70,14 @@ func TestStdoutMissing(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestStdoutLoad(t *testing.T) {
|
func TestStdoutLoad(t *testing.T) {
|
||||||
os.Setenv("LOG_STDOUT_FORMAT", "json")
|
os.Setenv("LOG_STDOUT_FORMATTER", "json")
|
||||||
defer os.Unsetenv("LOG_STDOUT_FORMAT")
|
defer os.Unsetenv("LOG_STDOUT_FORMATTER")
|
||||||
os.Setenv("LOG_STDOUT_ENABLED", "true")
|
os.Setenv("LOG_STDOUT_ENABLED", "true")
|
||||||
defer os.Unsetenv("LOG_STDOUT_ENABLED")
|
defer os.Unsetenv("LOG_STDOUT_ENABLED")
|
||||||
c, _ := Load()
|
c, _ := Load()
|
||||||
std, en := c.Outputs.Stdout()
|
std, en := c.Outputs.Stdout()
|
||||||
assert.True(t, en)
|
assert.True(t, en)
|
||||||
assert.Equal(t, "json", std.Format)
|
assert.Equal(t, "json", std.Formatter)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFormatJson(t *testing.T) {
|
func TestFormatJson(t *testing.T) {
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@ type Output struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Stdout struct {
|
type Stdout struct {
|
||||||
Format string `env:"LOG_STDOUT_FORMAT" mapstructure:"format"`
|
Formatter string `env:"LOG_STDOUT_FORMATTER" mapstructure:"formatter"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type stdoutEnabled struct {
|
type stdoutEnabled struct {
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,6 @@ type formatMaker func(config.Formatters) (Formatter, error)
|
||||||
var formatterMap = map[string]formatMaker{
|
var formatterMap = map[string]formatMaker{
|
||||||
"plain": newPlain,
|
"plain": newPlain,
|
||||||
"json": newJson,
|
"json": newJson,
|
||||||
"zero": newNull,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Preferred() (f Formatter, err error) {
|
func Preferred() (f Formatter, err error) {
|
||||||
|
|
@ -20,7 +19,8 @@ func Preferred() (f Formatter, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
std, _ := conf.Outputs.Stdout()
|
std, _ := conf.Outputs.Stdout()
|
||||||
return New(std.Format)
|
kind := std.Formatter
|
||||||
|
return New(kind)
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(kind string) (f Formatter, err error) {
|
func New(kind string) (f Formatter, err error) {
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestKinds(t *testing.T) {
|
func TestKinds(t *testing.T) {
|
||||||
assert.ElementsMatch(t, []string{"plain", "json", "zero"}, Kinds())
|
assert.ElementsMatch(t, []string{"plain", "json"}, Kinds())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNewUnsupported(t *testing.T) {
|
func TestNewUnsupported(t *testing.T) {
|
||||||
|
|
|
||||||
|
|
@ -1,32 +0,0 @@
|
||||||
package formatters
|
|
||||||
|
|
||||||
import (
|
|
||||||
"codeberg.org/danjones000/my-log/config"
|
|
||||||
"codeberg.org/danjones000/my-log/models"
|
|
||||||
)
|
|
||||||
|
|
||||||
func newNull(ff config.Formatters) (Formatter, error) {
|
|
||||||
return &Null{}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type Null struct{}
|
|
||||||
|
|
||||||
func (n *Null) Name() string {
|
|
||||||
return "zero"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (n *Null) Meta(m models.Meta) (o []byte, err error) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func (n *Null) Entry(e models.Entry) (o []byte, err error) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func (n *Null) Log(l models.Log) (o []byte, err error) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func (n *Null) Logs(logs []models.Log) (out []byte, err error) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
@ -1,45 +0,0 @@
|
||||||
package formatters
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"codeberg.org/danjones000/my-log/models"
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
)
|
|
||||||
|
|
||||||
var empty []byte
|
|
||||||
|
|
||||||
func TestNullName(t *testing.T) {
|
|
||||||
f, err := New("zero")
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, "zero", f.Name())
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestNullMeta(t *testing.T) {
|
|
||||||
f, _ := New("zero")
|
|
||||||
o, err := f.Meta(models.Meta{"foo", 42})
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, empty, o)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestNullEntry(t *testing.T) {
|
|
||||||
f, _ := New("zero")
|
|
||||||
o, err := f.Entry(models.Entry{"title", time.Now(), models.Metas{}})
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, empty, o)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestNullLog(t *testing.T) {
|
|
||||||
f, _ := New("zero")
|
|
||||||
o, err := f.Log(models.Log{"jim", []models.Entry{{"title", time.Now(), models.Metas{}}}})
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, empty, o)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestNullLogs(t *testing.T) {
|
|
||||||
f, _ := New("zero")
|
|
||||||
o, err := f.Logs([]models.Log{{"jim", []models.Entry{{"title", time.Now(), models.Metas{}}}}})
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, empty, o)
|
|
||||||
}
|
|
||||||
|
|
@ -186,7 +186,7 @@ func (e Entry) MarshalJSON() ([]byte, error) {
|
||||||
out := map[string]any{}
|
out := map[string]any{}
|
||||||
out["title"] = e.Title
|
out["title"] = e.Title
|
||||||
out["date"] = e.Date.Format(time.RFC3339)
|
out["date"] = e.Date.Format(time.RFC3339)
|
||||||
for k, v := range e.Fields.Map() {
|
for k, v := range e.Fields.toMap() {
|
||||||
out[k] = v
|
out[k] = v
|
||||||
}
|
}
|
||||||
return json.Marshal(out)
|
return json.Marshal(out)
|
||||||
|
|
|
||||||
|
|
@ -13,8 +13,6 @@ import (
|
||||||
// Type assertions
|
// Type assertions
|
||||||
var _ encoding.TextMarshaler = Meta{}
|
var _ encoding.TextMarshaler = Meta{}
|
||||||
var _ encoding.TextUnmarshaler = new(Meta)
|
var _ encoding.TextUnmarshaler = new(Meta)
|
||||||
var _ json.Marshaler = Metas{}
|
|
||||||
var _ json.Unmarshaler = new(Metas)
|
|
||||||
|
|
||||||
var skipMarshalTest = errors.New("skip marshal")
|
var skipMarshalTest = errors.New("skip marshal")
|
||||||
|
|
||||||
|
|
@ -127,17 +125,3 @@ func TestMetasJsonError(t *testing.T) {
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
assert.Len(t, ms, 0)
|
assert.Len(t, ms, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMetasAppend(t *testing.T) {
|
|
||||||
ms := Metas{}
|
|
||||||
ms = ms.Append("foo", 42)
|
|
||||||
assert.Len(t, ms, 1)
|
|
||||||
assert.Equal(t, Meta{"foo", 42}, ms[0])
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestMetasAppendTo(t *testing.T) {
|
|
||||||
ms := &Metas{}
|
|
||||||
ms.AppendTo("foo", 42)
|
|
||||||
assert.Len(t, *ms, 1)
|
|
||||||
assert.Equal(t, Meta{"foo", 42}, (*ms)[0])
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,21 @@
|
||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
//"bufio"
|
||||||
|
//"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
//"fmt"
|
||||||
|
//"errors"
|
||||||
|
//"regexp"
|
||||||
|
//"strings"
|
||||||
|
//"sync"
|
||||||
"time"
|
"time"
|
||||||
|
//"codeberg.org/danjones000/my-log/tools"
|
||||||
)
|
)
|
||||||
|
|
||||||
// A slice of Meta
|
|
||||||
type Metas []Meta
|
type Metas []Meta
|
||||||
|
|
||||||
// Returns a single map containing all the Meta. Is useful when encoding to JSON
|
func (ms Metas) toMap() map[string]any {
|
||||||
func (ms Metas) Map() map[string]any {
|
|
||||||
out := map[string]any{}
|
out := map[string]any{}
|
||||||
for _, f := range ms {
|
for _, f := range ms {
|
||||||
if _, found := out[f.Key]; found || f.Key == "title" || f.Key == "date" {
|
if _, found := out[f.Key]; found || f.Key == "title" || f.Key == "date" {
|
||||||
|
|
@ -36,14 +42,12 @@ func (ms Metas) Map() map[string]any {
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
// Implements json.Marshaler
|
|
||||||
func (ms Metas) MarshalJSON() ([]byte, error) {
|
func (ms Metas) MarshalJSON() ([]byte, error) {
|
||||||
return json.Marshal(ms.Map())
|
return json.Marshal(ms.toMap())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Implements json.Unmarshaler
|
|
||||||
func (ms *Metas) UnmarshalJSON(in []byte) error {
|
func (ms *Metas) UnmarshalJSON(in []byte) error {
|
||||||
old := (*ms).Map()
|
old := (*ms).toMap()
|
||||||
out := map[string]any{}
|
out := map[string]any{}
|
||||||
err := json.Unmarshal(in, &out)
|
err := json.Unmarshal(in, &out)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -58,14 +62,3 @@ func (ms *Metas) UnmarshalJSON(in []byte) error {
|
||||||
*ms = ret
|
*ms = ret
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns a new Metas with a new Meta appended
|
|
||||||
func (ms Metas) Append(k string, v any) Metas {
|
|
||||||
return append(ms, Meta{k, v})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Appends a new Meta to this Metas
|
|
||||||
func (ms *Metas) AppendTo(k string, v any) {
|
|
||||||
n := (*ms).Append(k, v)
|
|
||||||
*ms = n
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue