✨ Drop command
This commit is contained in:
parent
a062e71a29
commit
cc9e8f6167
6 changed files with 214 additions and 23 deletions
78
cmd/drop.go
78
cmd/drop.go
|
|
@ -17,36 +17,76 @@ 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 dateStr string
|
||||
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 = time.Now().Local() // @todo parse date
|
||||
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")
|
||||
dropCmd.Flags().StringVarP(&dateStr, "date", "d", time.Now().Local().Format(time.RFC3339), "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"
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue