Skip to content

Commit

Permalink
Merge pull request #13 from foobaragency/restart
Browse files Browse the repository at this point in the history
fix restart command
  • Loading branch information
prmaloney authored Jan 25, 2024
2 parents bf10500 + 30f0a6a commit c9dac85
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
27 changes: 27 additions & 0 deletions cmd/activities.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,32 @@ var createCmd = &cobra.Command{
},
}

var restartCmd = &cobra.Command{
Use: "restart",
Short: "Restart an activity (empty <activity> 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 <activity>",
Short: "Edit an activity",
Expand Down Expand Up @@ -158,6 +184,7 @@ func init() {
activitiesCmd.AddCommand(editCmd)
activitiesCmd.AddCommand(createCmd)
activitiesCmd.AddCommand(deleteCmd)
activitiesCmd.AddCommand(restartCmd)

rootCmd.AddCommand(activitiesCmd)
}
24 changes: 24 additions & 0 deletions data/activities.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit c9dac85

Please sign in to comment.