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 (tasks AND projects) and find the most // recently updated. v0.3: ListWorkspaces filters by type, so we union the // two type buckets here so `last` keeps working for both. tasks, err := workspace.ListWorkspaces(root, workspace.ListOpts{ IncludeArchived: false, Limit: 0, }) if err != nil { return err } projects, err := workspace.ListWorkspaces(root, workspace.ListOpts{ IncludeArchived: false, Projects: true, Limit: 0, }) if err != nil { return err } results := append(tasks, projects...) 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) }