-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstop.go
More file actions
111 lines (96 loc) · 3.3 KB
/
Copy pathstop.go
File metadata and controls
111 lines (96 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package cmd
import (
"fmt"
"os"
"time"
"github.com/spf13/cobra"
)
var (
stopProject string
stopDescription string
stopTime string
stopRound string
)
var stopCmd = &cobra.Command{
Use: "stop",
Short: "Stop the currently running task",
Long: `
If project name is set and --project option has been passed
will ask you whether to ignore ir override with the passed name.
Issues a warning if the project name is empty and no name has
been set with --project.
The project name and description can have a total length of 150 utf-8 chars.
`,
Run: func(cmd *cobra.Command, args []string) {
EnsureLogFile(false)
checkArgsProjectDescription(stopProject, stopDescription)
curTask, offset, err := GetLogEntryFromEnd(0)
CheckErr(err)
CheckTaskIsNotZero(&curTask)
CheckTaskIsRunning(&curTask)
// stop time
ts := time.Now().Local()
if stopTime != "" {
parsedTime, err := time.Parse(timeArgLayout, stopTime)
CheckErr(err)
ts = time.Date(ts.Year(), ts.Month(), ts.Day(), parsedTime.Hour(), parsedTime.Minute(), 0, 0, ts.Location())
}
if stopRound != "" {
rd, err := time.ParseDuration(stopRound)
CheckErr(err)
ts = ts.Round(rd)
}
start, err := time.Parse(time.RFC3339, curTask.Start)
CheckErr(err)
if ts.Before(start) {
formattedStringsStyled.PrintfError("stop time before start time")
os.Exit(1)
}
curTask.Stop = ts.Format(time.RFC3339)
// project name
if curTask.Project == "" && stopProject == "" {
formattedStringsStyled.PrintfWarning("empty project name")
} else if curTask.Project == "" && stopProject != "" {
curTask.Project = stopProject
} else if curTask.Project != "" && stopProject != "" {
formattedStringsStyled.PrintfWarning("project name exists: %s", curTask.Project)
answer, err := AskForInputInOptions("[i] ignore or [o] override?", []string{"i", "o"})
CheckErr(err)
if answer == "o" {
curTask.Project = stopProject
}
} // else if curTask.Project != "" && stopProject == "" {}
// description
if curTask.Description == "" && stopDescription == "" {
// TODO ask for description
} else if stopDescription != "" {
totalLen := len(curTask.Project) + len(curTask.Description) + len(stopDescription)
if totalLen > maxLenProDesc {
fmt.Printf("description too long (max %d chars, is %d chars)\n", maxLenProDesc, totalLen)
os.Exit(1)
}
if curTask.Description != "" {
curTask.Description += ", " + stopDescription
} else {
curTask.Description = stopDescription
}
}
// adjust for first line in log file and newline char in subsequent entries
if offset != 0 {
offset++
}
err = RemoveLastLogEntry(offset)
CheckErr(err)
err = WriteLogEntry(&curTask)
CheckErr(err)
fmt.Printf("stopped %s at %s\ntook %s\n", curTask.Project, ts.Format(time.TimeOnly), ts.Sub(start).Truncate(time.Second).String())
},
}
func init() {
rootCmd.AddCommand(stopCmd)
// stopCmd.Flags().BoolVarP(&noAsk, "no-ask", "n", false, "do not task for missing info")
stopCmd.Flags().StringVarP(&stopProject, "project", "p", "", "project name")
stopCmd.Flags().StringVarP(&stopDescription, "description", "d", "", "activity description")
stopCmd.Flags().StringVarP(&stopTime, "time", "t", "", "from a specific time (HH:MM)")
stopCmd.Flags().StringVarP(&stopRound, "round", "r", "", "round time by e.g. 15m, 1m, etc.")
}