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..34723f6 100644 --- a/data/activities.go +++ b/data/activities.go @@ -134,6 +134,30 @@ 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") + } + + 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")