@@ -3,27 +3,43 @@ package cmd
33import (
44 "fmt"
55 "moco/data"
6+ "os"
67 "strconv"
78
89 "github.com/charmbracelet/huh"
10+ "github.com/joho/godotenv"
911 "github.com/spf13/cobra"
1012)
1113
12- var newCmd = & cobra.Command {
13- Use : "new " ,
14+ var createCmd = & cobra.Command {
15+ Use : "create " ,
1416 Short : "Create a new activity" ,
1517 Run : func (cmd * cobra.Command , args []string ) {
16- projectId , _ := cmd .Flags ().GetInt ("project" )
17- taskId , _ := cmd .Flags ().GetInt ("task" )
18+ projects , err := data .GetProjects ()
19+ if err != nil {
20+ fmt .Println ("Could not retrieve projects" , err )
21+ return
22+ }
23+
24+ // flags has highest priority
25+ projectId , err := cmd .Flags ().GetInt ("project" )
26+ taskId , err := cmd .Flags ().GetInt ("task" )
27+ minutes , err := cmd .Flags ().GetInt ("minutes" )
1828 description , err := cmd .Flags ().GetString ("description" )
1929
20- projects , err := data .GetProjects ()
21- if err != nil {
22- fmt .Println ("Could not retrieve projects" , err )
23- }
24-
30+ // env has second priority
31+ err = godotenv .Load (".moco" )
32+ if projectId == 0 && err == nil {
33+ projectId , err = strconv .Atoi (os .Getenv ("MOCO_PROJECT_ID" ))
34+ fmt .Println ("read projectId" , projectId )
35+ }
36+ if taskId == 0 && err == nil {
37+ taskId , err = strconv .Atoi (os .Getenv ("MOCO_TASK_ID" ))
38+ fmt .Println ("read taskId" , taskId )
39+ }
40+
41+ // if no flags or config, prompt
2542 if projectId == 0 {
26-
2743 options := make ([]huh.Option [int ], len (projects ))
2844 for i , p := range projects {
2945 options [i ] = huh .NewOption [int ](p .Name , p .Id )
@@ -55,11 +71,26 @@ var newCmd = &cobra.Command{
5571 }
5672 }
5773
58- if description == "" {
59- huh .NewInput ().Title ("Description:" ).Prompt (">" ).Value (& description ).Run ()
60- }
61-
62- err = data .CreateActivity (projectId , taskId , description )
74+ if description == "" {
75+ huh .NewInput ().Title ("Description:" ).Prompt ("> " ).Value (& description ).Run ()
76+ }
77+
78+ if minutes == 0 {
79+ var minutesStr string
80+ huh .NewInput ().Title ("Time (minutes, entering '0' or empty will start a timer):" ).Prompt ("> " ).Value (& minutesStr ).Run ()
81+ if minutesStr == "" {
82+ minutes = 0
83+ } else {
84+ minutes , err = strconv .Atoi (minutesStr )
85+ if err != nil {
86+ fmt .Println ("Invalid minutes" )
87+ return
88+ }
89+ }
90+ }
91+
92+
93+ err = data .CreateActivity (projectId , taskId , description , minutes )
6394 if err != nil {
6495 fmt .Println ("Could not create activity:" , err )
6596 }
@@ -79,13 +110,13 @@ var editCmd = &cobra.Command{
79110 fmt .Println ("Invalid activity id" )
80111 return
81112 }
82- seconds , err := cmd .Flags ().GetInt ("time" )
113+ minutes , err := cmd .Flags ().GetInt ("time" )
83114 description , err := cmd .Flags ().GetString ("description" )
84- if err != nil || (seconds == 0 && description == "" ) {
115+ if err != nil || (minutes == 0 && description == "" ) {
85116 cmd .Help ()
86117 return
87118 }
88- err = data .EditActivity (activityId , seconds , description )
119+ err = data .EditActivity (activityId , minutes , description )
89120 if err != nil {
90121 fmt .Println ("Could not edit activity:" , err )
91122 }
@@ -118,14 +149,16 @@ var activitiesCmd = &cobra.Command{
118149}
119150
120151func init () {
121- editCmd .Flags ().IntP ("time" , "t" , 0 , "Set the time for the activity (in seconds )" )
152+ editCmd .Flags ().IntP ("time" , "t" , 0 , "Set the time for the activity (in minutes )" )
122153 editCmd .Flags ().StringP ("description" , "d" , "" , "Set the description for the activity" )
123154
124- activitiesCmd . AddCommand ( editCmd )
125-
126- newCmd .Flags ().Bool ( "no-start " , false , "Don't start the activity when created " )
127- activitiesCmd . AddCommand ( newCmd )
155+ createCmd . Flags (). IntP ( "project" , "p" , 0 , "Set the project for the activity" )
156+ createCmd . Flags (). IntP ( "task" , "t" , 0 , "Set the task for the activity" )
157+ createCmd .Flags ().StringP ( "description " , "d" , "" , "Set the description for the activity " )
158+ createCmd . Flags (). IntP ( "minutes" , "m" , 0 , "Set the number of minutes for the activity" )
128159
160+ activitiesCmd .AddCommand (editCmd )
161+ activitiesCmd .AddCommand (createCmd )
129162 activitiesCmd .AddCommand (deleteCmd )
130163
131164 rootCmd .AddCommand (activitiesCmd )
0 commit comments