my-log/cmd/drop.go

119 lines
2.6 KiB
Go
Raw Normal View History

2024-01-26 19:32:21 -06:00
/*
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 (
2024-02-11 13:50:27 -06:00
"encoding/json"
2024-01-26 19:32:21 -06:00
"fmt"
2024-02-11 13:50:27 -06:00
"time"
2024-01-26 19:32:21 -06:00
2024-02-11 13:50:27 -06:00
"codeberg.org/danjones000/my-log/files"
"codeberg.org/danjones000/my-log/formatters"
2024-02-11 13:50:27 -06:00
"codeberg.org/danjones000/my-log/models"
"codeberg.org/danjones000/my-log/tools"
2024-01-26 19:32:21 -06:00
"github.com/spf13/cobra"
)
2024-02-25 15:25:34 -06:00
var d Date
2024-02-11 13:50:27 -06:00
var fields map[string]string
var j Json
2024-01-26 19:32:21 -06:00
// dropCmd represents the drop command
var dropCmd = &cobra.Command{
2024-02-11 13:50:27 -06:00
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]
2024-03-10 15:26:00 -05:00
ms := models.Metas{}
2024-02-11 13:50:27 -06:00
if len(j.RawMessage) > 8 {
2024-03-10 15:26:00 -05:00
err := json.Unmarshal([]byte(j.RawMessage), &ms)
2024-02-11 13:50:27 -06:00
if err != nil {
return err
}
}
for k, v := range fields {
2024-03-10 15:26:00 -05:00
ms = append(ms, models.Meta{k, tools.ParseString(v)})
2024-02-11 13:50:27 -06:00
}
2024-03-10 15:26:00 -05:00
e := models.Entry{title, d.t, ms}
2024-02-11 13:50:27 -06:00
l := models.Log{log, []models.Entry{e}}
err := files.Append(l)
if err != nil {
return err
}
2024-03-09 16:05:59 -06:00
form, err := formatters.Preferred()
2024-02-11 13:50:27 -06:00
if err != nil {
return err
}
out, err := form.Log(l)
if err != nil {
return err
}
fmt.Fprintf(cmd.OutOrStdout(), "%s\n", out)
2024-02-11 13:50:27 -06:00
return nil
2024-01-26 19:32:21 -06:00
},
}
func init() {
rootCmd.AddCommand(dropCmd)
2024-02-25 15:25:34 -06:00
(&d).Set("now")
dropCmd.Flags().VarP(&d, "date", "d", "Date for log entry")
2024-02-11 13:50:27 -06:00
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
}
2024-01-26 19:32:21 -06:00
2024-02-11 13:50:27 -06:00
func (j *Json) String() string {
return string(j.RawMessage)
}
func (j *Json) Set(in string) error {
return json.Unmarshal([]byte(in), &j.RawMessage)
}
2024-01-26 19:32:21 -06:00
2024-02-11 13:50:27 -06:00
func (j *Json) Type() string {
return "json"
2024-01-26 19:32:21 -06:00
}
2024-02-25 15:25:34 -06:00
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"
}