Files
ctask/cmd/resume.go
T

91 lines
2.8 KiB
Go

package cmd
import (
"fmt"
"os"
"path/filepath"
"time"
"github.com/spf13/cobra"
"github.com/warrenronsiek/ctask/internal/config"
"github.com/warrenronsiek/ctask/internal/shell"
"github.com/warrenronsiek/ctask/internal/workspace"
)
var resumeCmd = &cobra.Command{
Use: "resume <query>",
Short: "Reopen an existing workspace and launch the agent",
Args: cobra.ExactArgs(1),
SilenceUsage: true,
RunE: runResume,
}
var (
resumeContainer bool
resumeShell bool
resumeAgent string
resumeForce bool
resumeDirect bool
)
func init() {
resumeCmd.Flags().BoolVar(&resumeContainer, "container", false, "Resume in container mode (deferred)")
resumeCmd.Flags().BoolVar(&resumeShell, "shell", false, "Open shell instead of agent")
resumeCmd.Flags().StringVarP(&resumeAgent, "agent", "a", "", "Override agent command")
resumeCmd.Flags().BoolVar(&resumeForce, "force", false, "Skip active-session and stale-workspace warnings")
resumeCmd.Flags().BoolVar(&resumeDirect, "direct", false, "Bypass persistent session mode for this command")
resumeCmd.ValidArgsFunction = completeWorkspaces(completionActive)
rootCmd.AddCommand(resumeCmd)
}
func runResume(cmd *cobra.Command, args []string) error {
return doResume(args[0], resumeContainer, resumeShell, resumeForce, resumeAgent, resumeDirect)
}
// doResume is the shared resume logic used by both `resume` and `last`.
// It preserves resume's existing archive-inclusive resolution and
// restore-hint behavior, then delegates to runWorkspaceEntry for the
// persistent-vs-direct decision and tmux dispatch.
func doResume(query string, container, useShell, force bool, agentOverride string, directFlag bool) error {
if container {
fmt.Println(shell.ContainerNotice())
return nil
}
roots := config.SearchRoots()
// resume resolves archived-inclusive so we can give a helpful hint when
// the user resumes an archived workspace (v0.5.2 behavior — preserved).
ws := resolveOne(roots, query, true)
if ws.Meta.Status == "archived" {
fmt.Fprintf(os.Stderr,
"[ctask] error: workspace %q is archived\n\nTo restore it:\n ctask restore %s\n",
query, query)
return fmt.Errorf("workspace archived")
}
// updated_at bump (existing v0.4 behavior).
now := time.Now().UTC().Truncate(time.Second)
ws.Meta.UpdatedAt = now
metaPath := filepath.Join(ws.Path, "task.yaml")
if err := workspace.WriteMetaLocked(metaPath, ws.Meta); err != nil {
return fmt.Errorf("updating metadata: %w", err)
}
agent := agentOverride
if agent == "" {
agent = ws.Meta.Agent
}
return runWorkspaceEntry(WorkspaceEntryOptions{
WsPath: ws.Path,
WsRoot: ws.Root,
WsMeta: ws.Meta,
Agent: agent,
Shell: useShell,
Force: force,
Direct: directFlag,
CommandName: "resume",
})
}