zeit/z/entryCmd.go

84 lines
2.1 KiB
Go
Raw Permalink Normal View History

2020-10-17 20:14:49 +00:00
package z
import (
"os"
"fmt"
"strings"
2022-09-05 00:41:43 +00:00
"github.com/jinzhu/now"
2020-10-17 20:14:49 +00:00
"github.com/spf13/cobra"
)
var entryCmd = &cobra.Command{
Use: "entry ([flags]) [id]",
Short: "Display or update activity",
Long: "Display or update tracked activity.",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
user := GetCurrentUser()
id := args[0]
entry, err := database.GetEntry(user, id)
if err != nil {
fmt.Printf("%s %+v\n", CharError, err)
os.Exit(1)
}
if begin != "" || finish != "" || project != "" || notes != "" || task != "" {
if begin != "" {
2022-09-05 00:41:43 +00:00
entry.Begin, err = now.Parse(begin)
2020-10-17 21:49:29 +00:00
if err != nil {
fmt.Printf("%s %+v\n", CharError, err)
os.Exit(1)
}
2020-10-17 20:14:49 +00:00
}
if finish != "" {
2022-09-05 00:41:43 +00:00
entry.Finish, err = now.Parse(finish)
2020-10-17 21:49:29 +00:00
if err != nil {
fmt.Printf("%s %+v\n", CharError, err)
os.Exit(1)
}
2020-10-17 20:14:49 +00:00
}
if project != "" {
2020-10-17 21:49:29 +00:00
entry.Project = project
2020-10-17 20:14:49 +00:00
}
if task != "" {
2020-10-17 21:49:29 +00:00
entry.Task = task
2020-10-17 20:14:49 +00:00
}
if notes != "" {
entry.Notes = strings.Replace(notes, "\\n", "\n", -1)
}
_, err = database.UpdateEntry(user, entry)
if err != nil {
fmt.Printf("%s %+v\n", CharError, err)
os.Exit(1)
}
}
fmt.Printf("%s %s\n", CharInfo, entry.GetOutput(true))
return
},
}
func init() {
rootCmd.AddCommand(entryCmd)
2022-09-05 00:41:43 +00:00
entryCmd.Flags().StringVarP(&begin, "begin", "b", "", "Update date/time the activity began at")
entryCmd.Flags().StringVarP(&finish, "finish", "s", "", "Update date/time the activity finished at")
2020-10-17 20:14:49 +00:00
entryCmd.Flags().StringVarP(&project, "project", "p", "", "Update activity project")
entryCmd.Flags().StringVarP(&notes, "notes", "n", "", "Update activity notes")
entryCmd.Flags().StringVarP(&task, "task", "t", "", "Update activity task")
entryCmd.Flags().BoolVar(&fractional, "decimal", false, "Show fractional hours in decimal format instead of minutes")
2020-10-17 20:14:49 +00:00
var err error
database, err = InitDatabase()
if err != nil {
fmt.Printf("%s %+v\n", CharError, err)
os.Exit(1)
}
}