🚚 Refactor project structure to follow standard Go layout conventions
This commit is contained in:
parent
c0007c291d
commit
ad7eba9b03
7 changed files with 5 additions and 4 deletions
128
internal/cmd/drop.go
Normal file
128
internal/cmd/drop.go
Normal file
|
|
@ -0,0 +1,128 @@
|
|||
/*
|
||||
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 (
|
||||
"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 {
|
||||
if outJson {
|
||||
config.Overrides["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, d.t, *ms}
|
||||
l := models.Log{log, []models.Entry{e}}
|
||||
err := files.Append(l)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
form, err := formatters.Preferred()
|
||||
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"
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue