Files
ctask/cmd/restore.go
T
typebasedio 176e788f67 feat(v0.5.2): add restore, notes, path commands with completion plumbing
Three new direct-lookup commands per v0.5.2-spec.md:

- ctask restore <ws>   un-archive a workspace (metadata-only flip,
                       mirrors archive's lease guard, refuses to
                       restore an already-active workspace)
- ctask notes <ws>     stream a workspace's notes.md to stdout (raw,
                       no framing, [ctask]-prefixed stderr on error)
                       so AI agents can read prior workspace context
                       through standard shell pipelines
- ctask path <ws>      print the absolute filesystem path of a
                       workspace, OS-native separators, one line

All three resolve archived-inclusive: the user typed a name, so we
find the workspace whether or not it's archived. Listing stays
filtered (active-only by default) per the v0.5.2 design rule
"listing is filtered, direct lookup is comprehensive".

Adds shared completion infrastructure (cmd/completion.go) used by
these commands and wired into the existing workspace-accepting
commands in a follow-up commit. Candidates are workspace directory
basenames (e.g. 2026-04-22_promptvolley) rather than bare slugs
because basenames are unique under the resolver's exact-match step
while slugs can collide across categories or dates.
2026-05-07 19:47:14 -04:00

86 lines
2.4 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/session"
"github.com/warrenronsiek/ctask/internal/workspace"
)
var restoreCmd = &cobra.Command{
Use: "restore <query>",
Short: "Un-archive a workspace (set status back to active)",
Args: cobra.ExactArgs(1),
SilenceUsage: true,
RunE: runRestore,
}
func init() {
restoreCmd.ValidArgsFunction = completeWorkspaces(completionArchived)
rootCmd.AddCommand(restoreCmd)
}
func runRestore(cmd *cobra.Command, args []string) error {
roots := config.SearchRoots()
ws := resolveOne(roots, args[0], true)
if ws.Meta.Status != "archived" {
return fmt.Errorf("workspace %q is already active", args[0])
}
// Mirror the active-session lease guard from archive: if a session is
// holding a fresh lease, prompt on TTY and refuse on non-TTY. Restoring
// under an active session is unusual but not catastrophic — the same
// "refuse before silent surprise" rule applies.
if lease, err := session.ReadLease(session.LeasePath(ws.Path)); err == nil && lease != nil {
if session.IsFresh(lease, time.Now(), session.StaleLeaseAfter) {
fmt.Print(formatRestoreActiveWarning(lease, time.Now()))
if !isStdinTerminal() {
fmt.Println()
return fmt.Errorf("refusing to restore workspace with active session (stdin is not a terminal)")
}
if !session.ConfirmYN(os.Stdin, os.Stdout, " Restore anyway? [y/N] ", false) {
fmt.Println(" Cancelled.")
return nil
}
}
}
now := time.Now().UTC().Truncate(time.Second)
ws.Meta.Status = "active"
ws.Meta.ArchivedAt = nil
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)
}
relPath := workspace.RelativePath(ws.Root, ws.Path)
fmt.Printf("[ctask] restored: %s\n", relPath)
fmt.Printf("[ctask] status: active\n")
return nil
}
func formatRestoreActiveWarning(l *session.Lease, now time.Time) string {
startedAgo := now.Sub(l.StartedAt)
return fmt.Sprintf(
"[ctask] Warning: this workspace has an active session:\n"+
" Host: %s\n"+
" Agent: %s\n"+
" Started: %s (%s ago)\n\n"+
" Restoring will mark it active again.\n\n",
l.Hostname,
l.Agent,
l.StartedAt.Local().Format("2006-01-02 15:04"),
session.FormatAgo(startedAgo),
)
}