|
1 | 1 | package z
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "os" |
5 |
| - "fmt" |
6 |
| - "time" |
7 |
| - "strings" |
8 |
| - "github.com/spf13/cobra" |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/araddon/dateparse" |
| 9 | + "github.com/spf13/cobra" |
9 | 10 | )
|
10 | 11 |
|
11 | 12 | var entryCmd = &cobra.Command{
|
12 |
| - Use: "entry ([flags]) [id]", |
13 |
| - Short: "Display or update activity", |
14 |
| - Long: "Display or update tracked activity.", |
15 |
| - Args: cobra.ExactArgs(1), |
16 |
| - Run: func(cmd *cobra.Command, args []string) { |
17 |
| - user := GetCurrentUser() |
18 |
| - id := args[0] |
| 13 | + Use: "entry ([flags]) [id]", |
| 14 | + Short: "Display or update activity", |
| 15 | + Long: "Display or update tracked activity.", |
| 16 | + Args: cobra.ExactArgs(1), |
| 17 | + Run: func(cmd *cobra.Command, args []string) { |
| 18 | + user := GetCurrentUser() |
| 19 | + id := args[0] |
19 | 20 |
|
20 |
| - entry, err := database.GetEntry(user, id) |
21 |
| - if err != nil { |
22 |
| - fmt.Printf("%s %+v\n", CharError, err) |
23 |
| - os.Exit(1) |
24 |
| - } |
| 21 | + entry, err := database.GetEntry(user, id) |
| 22 | + if err != nil { |
| 23 | + fmt.Printf("%s %+v\n", CharError, err) |
| 24 | + os.Exit(1) |
| 25 | + } |
25 | 26 |
|
26 |
| - if begin != "" || finish != "" || project != "" || notes != "" || task != "" { |
27 |
| - if begin != "" { |
28 |
| - entry.Begin, err = time.Parse(time.RFC3339, begin) |
29 |
| - if err != nil { |
30 |
| - fmt.Printf("%s %+v\n", CharError, err) |
31 |
| - os.Exit(1) |
32 |
| - } |
33 |
| - } |
| 27 | + if begin != "" || finish != "" || project != "" || notes != "" || task != "" { |
| 28 | + if begin != "" { |
| 29 | + entry.Begin, err = dateparse.ParseAny(begin) |
| 30 | + if err != nil { |
| 31 | + fmt.Printf("%s %+v\n", CharError, err) |
| 32 | + os.Exit(1) |
| 33 | + } |
| 34 | + } |
34 | 35 |
|
35 |
| - if finish != "" { |
36 |
| - entry.Finish, err = time.Parse(time.RFC3339, finish) |
37 |
| - if err != nil { |
38 |
| - fmt.Printf("%s %+v\n", CharError, err) |
39 |
| - os.Exit(1) |
40 |
| - } |
41 |
| - } |
| 36 | + if finish != "" { |
| 37 | + entry.Finish, err = dateparse.ParseAny(finish) |
| 38 | + if err != nil { |
| 39 | + fmt.Printf("%s %+v\n", CharError, err) |
| 40 | + os.Exit(1) |
| 41 | + } |
| 42 | + } |
42 | 43 |
|
43 |
| - if project != "" { |
44 |
| - entry.Project = project |
45 |
| - } |
| 44 | + if project != "" { |
| 45 | + entry.Project = project |
| 46 | + } |
46 | 47 |
|
47 |
| - if task != "" { |
48 |
| - entry.Task = task |
49 |
| - } |
| 48 | + if task != "" { |
| 49 | + entry.Task = task |
| 50 | + } |
50 | 51 |
|
51 |
| - if notes != "" { |
52 |
| - entry.Notes = strings.Replace(notes, "\\n", "\n", -1) |
53 |
| - } |
| 52 | + if notes != "" { |
| 53 | + entry.Notes = strings.Replace(notes, "\\n", "\n", -1) |
| 54 | + } |
54 | 55 |
|
55 |
| - _, err = database.UpdateEntry(user, entry) |
56 |
| - if err != nil { |
57 |
| - fmt.Printf("%s %+v\n", CharError, err) |
58 |
| - os.Exit(1) |
59 |
| - } |
60 |
| - } |
| 56 | + _, err = database.UpdateEntry(user, entry) |
| 57 | + if err != nil { |
| 58 | + fmt.Printf("%s %+v\n", CharError, err) |
| 59 | + os.Exit(1) |
| 60 | + } |
| 61 | + } |
61 | 62 |
|
62 |
| - fmt.Printf("%s %s\n", CharInfo, entry.GetOutput(true)) |
63 |
| - return |
64 |
| - }, |
| 63 | + fmt.Printf("%s %s\n", CharInfo, entry.GetOutput(true)) |
| 64 | + return |
| 65 | + }, |
65 | 66 | }
|
66 | 67 |
|
67 | 68 | func init() {
|
68 |
| - rootCmd.AddCommand(entryCmd) |
69 |
| - entryCmd.Flags().StringVarP(&begin, "begin", "b", "", "Update date/time the activity began at\n\nUse RFC3339 format.") |
70 |
| - entryCmd.Flags().StringVarP(&finish, "finish", "s", "", "Update date/time the activity finished at\n\nUse RFC3339 format.") |
71 |
| - entryCmd.Flags().StringVarP(&project, "project", "p", "", "Update activity project") |
72 |
| - entryCmd.Flags().StringVarP(¬es, "notes", "n", "", "Update activity notes") |
73 |
| - entryCmd.Flags().StringVarP(&task, "task", "t", "", "Update activity task") |
74 |
| - entryCmd.Flags().BoolVar(&fractional, "decimal", false, "Show fractional hours in decimal format instead of minutes") |
| 69 | + rootCmd.AddCommand(entryCmd) |
| 70 | + entryCmd.Flags().StringVarP(&begin, "begin", "b", "", "Update date/time the activity began at") |
| 71 | + entryCmd.Flags().StringVarP(&finish, "finish", "s", "", "Update date/time the activity finished at") |
| 72 | + entryCmd.Flags().StringVarP(&project, "project", "p", "", "Update activity project") |
| 73 | + entryCmd.Flags().StringVarP(¬es, "notes", "n", "", "Update activity notes") |
| 74 | + entryCmd.Flags().StringVarP(&task, "task", "t", "", "Update activity task") |
| 75 | + entryCmd.Flags().BoolVar(&fractional, "decimal", false, "Show fractional hours in decimal format instead of minutes") |
75 | 76 |
|
76 |
| - var err error |
77 |
| - database, err = InitDatabase() |
78 |
| - if err != nil { |
79 |
| - fmt.Printf("%s %+v\n", CharError, err) |
80 |
| - os.Exit(1) |
81 |
| - } |
| 77 | + var err error |
| 78 | + database, err = InitDatabase() |
| 79 | + if err != nil { |
| 80 | + fmt.Printf("%s %+v\n", CharError, err) |
| 81 | + os.Exit(1) |
| 82 | + } |
82 | 83 | }
|
0 commit comments