mirror of
https://github.com/foomo/go-clockify.git
synced 2025-10-16 12:35:40 +00:00
148 lines
3.6 KiB
Go
148 lines
3.6 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/foomo/go-clockify/pkg/clockify"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var (
|
|
apiURL = "https://api.clockify.me/api"
|
|
accessToken string
|
|
workspace *clockify.WorkspaceOverviewDto
|
|
workspaces []clockify.WorkspaceOverviewDto
|
|
)
|
|
|
|
func main() {
|
|
var client *clockify.ClientWithResponses
|
|
|
|
rootCmd := &cobra.Command{
|
|
Use: "cli-tool",
|
|
Short: "A CLI tool for managing projects and users",
|
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
|
if accessToken == "" {
|
|
accessToken = promptForInput("Enter your access token: ")
|
|
c, err := clockify.NewClockifyClient(accessToken)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
client = c
|
|
|
|
resp, err := client.GetWorkspacesOfUserWithResponse(context.Background())
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
if resp.JSON200 != nil {
|
|
workspaces = *resp.JSON200
|
|
}
|
|
}
|
|
|
|
if workspace == nil {
|
|
fmt.Println("Select a workspace:")
|
|
for i, ws := range workspaces {
|
|
fmt.Printf("%d. %s (%s)\n", i+1, *ws.Name, *ws.Id)
|
|
}
|
|
choice := promptForInput("Enter the number of the workspace: ")
|
|
idx := strings.TrimSpace(choice)
|
|
if i, err := strconv.Atoi(idx); err == nil && i > 0 && i <= len(workspaces) {
|
|
workspace = &workspaces[i-1]
|
|
} else {
|
|
return fmt.Errorf("invalid workspace selection")
|
|
}
|
|
}
|
|
return nil
|
|
},
|
|
}
|
|
|
|
projectsCmd := &cobra.Command{
|
|
Use: "projects",
|
|
Short: "Manage projects",
|
|
}
|
|
|
|
projectsLsCmd := &cobra.Command{
|
|
Use: "ls",
|
|
Short: "List all projects",
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
fmt.Println("Listing projects for workspace:", *workspace.Name, *workspace.Id)
|
|
ps, err := client.GetProjectsListWithResponse(context.Background(), *workspace.Id, &clockify.GetProjectsListParams{})
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
return
|
|
}
|
|
users, err := client.GetMembersInfoWithResponse(context.Background(), *workspace.Id, &clockify.GetMembersInfoParams{})
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
return
|
|
}
|
|
for _, m := range *users.JSON200 {
|
|
fmt.Printf("- %s (%s) (%s)\n", *m.Name, *m.Email, *m.Id)
|
|
}
|
|
|
|
if users.JSON200 == nil {
|
|
log.Fatal("No users found")
|
|
return
|
|
}
|
|
|
|
someUser := (*users.JSON200)[0]
|
|
|
|
for _, p := range *ps.JSON200 {
|
|
fmt.Println("- " + *p.Name)
|
|
resp, err := client.GetTimeEntriesWithResponse(context.Background(), *workspace.Id, *someUser.Id, &clockify.GetTimeEntriesParams{
|
|
Project: p.Id,
|
|
})
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
for _, entry := range *resp.JSON200 {
|
|
fmt.Println(" - ", *entry.TimeInterval.Start, *entry.Type, ": ", *entry.Description)
|
|
}
|
|
}
|
|
},
|
|
}
|
|
|
|
usersCmd := &cobra.Command{
|
|
Use: "users",
|
|
Short: "Manage users",
|
|
}
|
|
|
|
usersLsCmd := &cobra.Command{
|
|
Use: "ls",
|
|
Short: "List all users",
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
fmt.Println("Listing users for workspace:", *workspace.Name)
|
|
ps, err := client.GetMembersInfoWithResponse(context.Background(), *workspace.Id, &clockify.GetMembersInfoParams{})
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
for _, m := range *ps.JSON200 {
|
|
fmt.Printf("- %s (%s) (%s)\n", *m.Name, *m.Email, *m.Id)
|
|
}
|
|
},
|
|
}
|
|
|
|
projectsCmd.AddCommand(projectsLsCmd)
|
|
usersCmd.AddCommand(usersLsCmd)
|
|
|
|
rootCmd.AddCommand(projectsCmd, usersCmd)
|
|
|
|
if err := rootCmd.Execute(); err != nil {
|
|
fmt.Println(err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func promptForInput(prompt string) string {
|
|
reader := bufio.NewReader(os.Stdin)
|
|
fmt.Print(prompt)
|
|
input, _ := reader.ReadString('\n')
|
|
return strings.TrimSpace(input)
|
|
}
|