From 5d9a6c3fbb7de3fe3dfa88a88dd622e3c2634f0e Mon Sep 17 00:00:00 2001 From: Patrick Maloney Date: Thu, 25 Jan 2024 11:03:13 +0100 Subject: [PATCH 1/2] fix restart command --- cmd/activities.go | 27 +++++++++++++++++++++++++++ data/activities.go | 26 ++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/cmd/activities.go b/cmd/activities.go index 852a88f..5d45d12 100644 --- a/cmd/activities.go +++ b/cmd/activities.go @@ -95,6 +95,32 @@ var createCmd = &cobra.Command{ }, } +var restartCmd = &cobra.Command{ + Use: "restart", + Short: "Restart an activity (empty will restart last activity)", + Run: func(cmd *cobra.Command, args []string) { + activityId, err := cmd.Flags().GetInt("activity") + + if activityId == 0 || err != nil { + activities, err := data.GetActivities() + if err != nil { + fmt.Println("Could not retrieve activities", err) + return + } + if len(activities) == 0 { + fmt.Println("No activities found") + return + } + activityId = activities[len(activities)-1].Id + } + + err = data.RestartActivity(activityId) + if err != nil { + fmt.Println("Could not restart activity:", err) + } + }, +} + var editCmd = &cobra.Command{ Use: "edit ", Short: "Edit an activity", @@ -158,6 +184,7 @@ func init() { activitiesCmd.AddCommand(editCmd) activitiesCmd.AddCommand(createCmd) activitiesCmd.AddCommand(deleteCmd) + activitiesCmd.AddCommand(restartCmd) rootCmd.AddCommand(activitiesCmd) } diff --git a/data/activities.go b/data/activities.go index 4a2375d..e9a85ef 100644 --- a/data/activities.go +++ b/data/activities.go @@ -134,6 +134,32 @@ func StartActivity(projectId int, taskId int, description string) error { return nil } +func RestartActivity(id int) error { + config := config.Init() + apiKey := config.GetString("api_key") + domain := config.GetString("domain") + if apiKey == "" { + return fmt.Errorf("api_key not set") + } + if domain == "" { + return fmt.Errorf("domain not set") + } + + fmt.Printf("https://%s.mocoapp.com/api/v1/activities/%d/start_timer", domain, id) + + req, _ := http.NewRequest("PATCH", fmt.Sprintf("https://%s.mocoapp.com/api/v1/activities/%d/start_timer", domain, id), nil) + req.Header.Add("Authorization", fmt.Sprintf("Token token=%s", apiKey)) + req.Header.Add("Content-Type", "application/json") + + client := &http.Client{} + resp, err := client.Do(req) + if err != nil || resp.StatusCode == 422 { + return err + } + defer resp.Body.Close() + return nil +} + func EditActivity(id int, minutes int, description string) error { config := config.Init() apiKey := config.GetString("api_key") From 30f0a6a026f8ae37e03afa32a3de88ffc4bd15ba Mon Sep 17 00:00:00 2001 From: Patrick Maloney Date: Thu, 25 Jan 2024 11:04:26 +0100 Subject: [PATCH 2/2] fix: remove print --- data/activities.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/data/activities.go b/data/activities.go index e9a85ef..34723f6 100644 --- a/data/activities.go +++ b/data/activities.go @@ -145,8 +145,6 @@ func RestartActivity(id int) error { return fmt.Errorf("domain not set") } - fmt.Printf("https://%s.mocoapp.com/api/v1/activities/%d/start_timer", domain, id) - req, _ := http.NewRequest("PATCH", fmt.Sprintf("https://%s.mocoapp.com/api/v1/activities/%d/start_timer", domain, id), nil) req.Header.Add("Authorization", fmt.Sprintf("Token token=%s", apiKey)) req.Header.Add("Content-Type", "application/json")