my-log/cli/drop.go
Dan Jones 9f05f933dd ♲ Refactor configuration to use viper with context propagation
- Replace global ConfigPath and Overrides with viper-based configuration
- Add viper.New() to create configurable viper instances
- Store viper and unmarshaled Config struct in context for testability
- Add RetrieveFromContext and AddToContext helper functions
- Update files.Append to accept context and retrieve config from it
- Update formatters.Preferred and formatters.New to accept context
- Add PersistentPreRunE in CLI to create and configure viper instance
- Support -c flag for custom config file path
- Support -v flag for config value overrides
- Update all test files to create viper and add to context
- Remove unused config types and load functions
- Add viper as dependency with automatic env var support (MYLOG_*)
2026-03-09 13:46:01 -05:00

133 lines
3 KiB
Go

/*
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 cli
import (
"encoding/json"
"fmt"
"time"
"codeberg.org/danjones000/my-log/config"
"codeberg.org/danjones000/my-log/files"
"codeberg.org/danjones000/my-log/formatters"
"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
var outJson bool
// DropCmd represents the drop command
var DropCmd = &cobra.Command{
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 {
v, _ := config.RetrieveFromContext(cmd.Context())
if outJson {
v.Set("output.stdout.config.format", "json")
}
log := args[0]
title := args[1]
ms := &models.Metas{}
if len(j.RawMessage) > 8 {
err := json.Unmarshal([]byte(j.RawMessage), ms)
if err != nil {
return err
}
}
for k, v := range fields {
ms.AppendTo(k, tools.ParseString(v))
}
e := models.Entry{Title: title, Date: d.Time(), Fields: *ms}
l := models.Log{Name: log, Entries: []models.Entry{e}}
err := files.Append(cmd.Context(), l)
if err != nil {
return err
}
form, err := formatters.Preferred(cmd.Context())
if err != nil {
return err
}
out, err := form.Log(l)
if err != nil {
return err
}
if len(out) > 0 && out[len(out)-1] != 10 {
out = append(out, 10)
}
fmt.Fprintf(cmd.OutOrStdout(), "%s", out)
return nil
},
}
func init() {
RootCmd.AddCommand(DropCmd)
(&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")
DropCmd.Flags().BoolVarP(&outJson, "output_json", "o", false, "Output result 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"
}
func (d Date) Time() time.Time {
return d.t
}