diff --git a/cmd/last.go b/cmd/last.go new file mode 100644 index 0000000..506cb86 --- /dev/null +++ b/cmd/last.go @@ -0,0 +1,58 @@ +package cmd + +import ( + "fmt" + "os" + + "github.com/spf13/cobra" + "github.com/warrenronsiek/ctask/internal/config" + "github.com/warrenronsiek/ctask/internal/workspace" +) + +var lastCmd = &cobra.Command{ + Use: "last", + Short: "Resume the most recently updated workspace", + Args: cobra.NoArgs, + SilenceUsage: true, + RunE: runLast, +} + +var ( + lastShell bool + lastAgent string +) + +func init() { + lastCmd.Flags().BoolVar(&lastShell, "shell", false, "Open shell instead of agent") + lastCmd.Flags().StringVarP(&lastAgent, "agent", "a", "", "Override agent command") + rootCmd.AddCommand(lastCmd) +} + +func runLast(cmd *cobra.Command, args []string) error { + root := config.ResolveRoot() + + // Scan all non-archived workspaces and find the most recently updated + results, err := workspace.ListWorkspaces(root, workspace.ListOpts{ + IncludeArchived: false, + Limit: 0, + }) + if err != nil { + return err + } + + if len(results) == 0 { + fmt.Fprintln(os.Stderr, "No active workspaces found.") + os.Exit(1) + } + + // Find the one with the most recent updated_at + best := results[0] + for _, r := range results[1:] { + if r.Meta.UpdatedAt.After(best.Meta.UpdatedAt) { + best = r + } + } + + // Delegate to resume logic using the slug + return doResume(best.Meta.Slug, false, lastShell, lastAgent) +}