Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix restart command #13

Merged
merged 2 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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