results.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package cmd
  2. import (
  3. "fmt"
  4. "os"
  5. "github.com/letta/letta-schedules-cli/internal/client"
  6. "github.com/letta/letta-schedules-cli/internal/config"
  7. "github.com/olekukonko/tablewriter"
  8. "github.com/spf13/cobra"
  9. )
  10. var resultsCmd = &cobra.Command{
  11. Use: "results",
  12. Short: "View schedule execution results",
  13. Long: "List and view execution results for scheduled messages",
  14. }
  15. var resultsListCmd = &cobra.Command{
  16. Use: "list",
  17. Short: "List all execution results",
  18. RunE: func(cmd *cobra.Command, args []string) error {
  19. cfg, err := config.Load()
  20. if err != nil {
  21. return err
  22. }
  23. if err := cfg.Validate(); err != nil {
  24. return err
  25. }
  26. apiClient := client.NewClient(cfg.BaseURL, cfg.APIKey)
  27. results, err := apiClient.ListResults()
  28. if err != nil {
  29. return fmt.Errorf("failed to list results: %w", err)
  30. }
  31. if len(results) == 0 {
  32. fmt.Println("No execution results found")
  33. return nil
  34. }
  35. table := tablewriter.NewWriter(os.Stdout)
  36. table.SetHeader([]string{"Schedule ID", "Type", "Agent ID", "Run ID", "Executed At"})
  37. table.SetAutoWrapText(false)
  38. table.SetAutoFormatHeaders(true)
  39. table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
  40. table.SetAlignment(tablewriter.ALIGN_LEFT)
  41. table.SetCenterSeparator("")
  42. table.SetColumnSeparator("")
  43. table.SetRowSeparator("")
  44. table.SetHeaderLine(false)
  45. table.SetBorder(false)
  46. table.SetTablePadding("\t")
  47. table.SetNoWhiteSpace(true)
  48. for _, r := range results {
  49. table.Append([]string{
  50. r.ScheduleID,
  51. r.ScheduleType,
  52. r.AgentID,
  53. r.RunID,
  54. r.ExecutedAt,
  55. })
  56. }
  57. table.Render()
  58. return nil
  59. },
  60. }
  61. var resultsGetCmd = &cobra.Command{
  62. Use: "get [schedule-id]",
  63. Short: "Get execution result for a specific schedule",
  64. Args: cobra.ExactArgs(1),
  65. RunE: func(cmd *cobra.Command, args []string) error {
  66. scheduleID := args[0]
  67. cfg, err := config.Load()
  68. if err != nil {
  69. return err
  70. }
  71. if err := cfg.Validate(); err != nil {
  72. return err
  73. }
  74. apiClient := client.NewClient(cfg.BaseURL, cfg.APIKey)
  75. result, err := apiClient.GetResult(scheduleID)
  76. if err != nil {
  77. return fmt.Errorf("failed to get result: %w", err)
  78. }
  79. fmt.Printf("Schedule ID: %s\n", result.ScheduleID)
  80. fmt.Printf("Schedule Type: %s\n", result.ScheduleType)
  81. fmt.Printf("Agent ID: %s\n", result.AgentID)
  82. fmt.Printf("Run ID: %s\n", result.RunID)
  83. fmt.Printf("Message: %s\n", result.Message)
  84. fmt.Printf("Executed At: %s\n", result.ExecutedAt)
  85. return nil
  86. },
  87. }
  88. func init() {
  89. rootCmd.AddCommand(resultsCmd)
  90. resultsCmd.AddCommand(resultsListCmd)
  91. resultsCmd.AddCommand(resultsGetCmd)
  92. }