cli/pkg/ai/cmd_updates.go
Snider 9931593f9d refactor(cli): move commands from cmd/ to pkg/ with self-registration
Implements defence in depth through build variants - only compiled code
exists in the binary. Commands now self-register via cli.RegisterCommands()
in their init() functions, mirroring the i18n.RegisterLocales() pattern.

Structure changes:
- cmd/{ai,build,ci,dev,docs,doctor,go,php,pkg,sdk,setup,test,vm}/ → pkg/*/cmd_*.go
- cmd/core_dev.go, cmd/core_ci.go → cmd/variants/{full,ci,php,minimal}.go
- Added pkg/cli/commands.go with RegisterCommands API
- Updated pkg/cli/runtime.go to attach registered commands

Build variants:
- go build           → full (21MB, all 13 command groups)
- go build -tags ci  → ci (18MB, build/ci/sdk/doctor)
- go build -tags php → php (14MB, php/doctor)
- go build -tags minimal → minimal (11MB, doctor only)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-30 21:55:55 +00:00

124 lines
3.8 KiB
Go

// cmd_updates.go implements task update and completion commands.
package ai
import (
"context"
"fmt"
"time"
"github.com/host-uk/core/pkg/agentic"
"github.com/host-uk/core/pkg/i18n"
"github.com/spf13/cobra"
)
// task:update command flags
var (
taskUpdateStatus string
taskUpdateProgress int
taskUpdateNotes string
)
// task:complete command flags
var (
taskCompleteOutput string
taskCompleteFailed bool
taskCompleteErrorMsg string
)
var taskUpdateCmd = &cobra.Command{
Use: "task:update [task-id]",
Short: i18n.T("cmd.ai.task_update.short"),
Long: i18n.T("cmd.ai.task_update.long"),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
taskID := args[0]
if taskUpdateStatus == "" && taskUpdateProgress == 0 && taskUpdateNotes == "" {
return fmt.Errorf("%s", i18n.T("cmd.ai.task_update.flag_required"))
}
cfg, err := agentic.LoadConfig("")
if err != nil {
return fmt.Errorf("%s: %w", i18n.T("common.error.failed", map[string]any{"Action": "load config"}), err)
}
client := agentic.NewClientFromConfig(cfg)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
update := agentic.TaskUpdate{
Progress: taskUpdateProgress,
Notes: taskUpdateNotes,
}
if taskUpdateStatus != "" {
update.Status = agentic.TaskStatus(taskUpdateStatus)
}
if err := client.UpdateTask(ctx, taskID, update); err != nil {
return fmt.Errorf("%s: %w", i18n.T("common.error.failed", map[string]any{"Action": "update task"}), err)
}
fmt.Printf("%s %s\n", successStyle.Render(">>"), i18n.T("common.success.completed", map[string]any{"Action": "Task updated"}))
return nil
},
}
var taskCompleteCmd = &cobra.Command{
Use: "task:complete [task-id]",
Short: i18n.T("cmd.ai.task_complete.short"),
Long: i18n.T("cmd.ai.task_complete.long"),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
taskID := args[0]
cfg, err := agentic.LoadConfig("")
if err != nil {
return fmt.Errorf("%s: %w", i18n.T("common.error.failed", map[string]any{"Action": "load config"}), err)
}
client := agentic.NewClientFromConfig(cfg)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
result := agentic.TaskResult{
Success: !taskCompleteFailed,
Output: taskCompleteOutput,
ErrorMessage: taskCompleteErrorMsg,
}
if err := client.CompleteTask(ctx, taskID, result); err != nil {
return fmt.Errorf("%s: %w", i18n.T("common.error.failed", map[string]any{"Action": "complete task"}), err)
}
if taskCompleteFailed {
fmt.Printf("%s %s\n", errorStyle.Render(">>"), i18n.T("cmd.ai.task_complete.failed", map[string]interface{}{"ID": taskID}))
} else {
fmt.Printf("%s %s\n", successStyle.Render(">>"), i18n.T("common.success.completed", map[string]any{"Action": "Task completed"}))
}
return nil
},
}
func initUpdatesFlags() {
// task:update command flags
taskUpdateCmd.Flags().StringVar(&taskUpdateStatus, "status", "", i18n.T("cmd.ai.task_update.flag.status"))
taskUpdateCmd.Flags().IntVar(&taskUpdateProgress, "progress", 0, i18n.T("cmd.ai.task_update.flag.progress"))
taskUpdateCmd.Flags().StringVar(&taskUpdateNotes, "notes", "", i18n.T("cmd.ai.task_update.flag.notes"))
// task:complete command flags
taskCompleteCmd.Flags().StringVar(&taskCompleteOutput, "output", "", i18n.T("cmd.ai.task_complete.flag.output"))
taskCompleteCmd.Flags().BoolVar(&taskCompleteFailed, "failed", false, i18n.T("cmd.ai.task_complete.flag.failed"))
taskCompleteCmd.Flags().StringVar(&taskCompleteErrorMsg, "error", "", i18n.T("cmd.ai.task_complete.flag.error"))
}
func addTaskUpdateCommand(parent *cobra.Command) {
initUpdatesFlags()
parent.AddCommand(taskUpdateCmd)
}
func addTaskCompleteCommand(parent *cobra.Command) {
parent.AddCommand(taskCompleteCmd)
}