1package commands
2
3import (
4	"fmt"
5	"github.com/gochrono/chrono/chronolib"
6	"github.com/jinzhu/now"
7	"github.com/spf13/cobra"
8	"time"
9)
10
11var stopNote string
12var stopAt string
13
14func newStopCmd() *cobra.Command {
15	stopCmd := &cobra.Command{
16		Use:   "stop",
17		Short: "Stop the current frame",
18		Long:  "Stop the current frame",
19		Run: func(cmd *cobra.Command, args []string) {
20			configDir := chronolib.GetCorrectConfigDirectory("")
21			config := chronolib.GetConfig(configDir)
22			state, _ := chronolib.GetState(config)
23
24			if state.IsEmpty() {
25				fmt.Println("No project started")
26				return
27			}
28
29			var endedAt time.Time
30			var err error
31			if stopAt != "" {
32				endedAt, err = now.Parse(stopAt)
33				if err != nil {
34					fmt.Println("Unable to parse time string")
35					return
36				}
37			} else {
38				endedAt = time.Now()
39			}
40
41			newFrame := state.ToFrame(endedAt)
42
43			if stopNote != "" {
44				newFrame.Notes = append(newFrame.Notes, stopNote)
45			}
46
47			frames, _ := chronolib.GetFrames(config)
48			frames.Add(newFrame)
49			chronolib.SaveFrames(config, frames)
50			state.Clear()
51			chronolib.SaveState(config, state)
52
53			fmt.Println(chronolib.FormatStopFrameMessage(newFrame))
54		},
55	}
56	stopCmd.Flags().StringVarP(&stopNote, "note", "n", "", "add a final note to current frame")
57	stopCmd.Flags().StringVarP(&stopAt, "at", "a", "", "sets the time the current frame ended to something other than now - format: 'mm/dd/yyyy HH:MM'. Make sure it is wrapped in quotes.")
58	stopCmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "enable verbose output")
59	return stopCmd
60}
61