| 1 | package cmd |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "io" |
| 6 | "text/tabwriter" |
| 7 | |
| 8 | "the-valley/flow/internal/git" |
| 9 | "the-valley/flow/internal/project" |
| 10 | "the-valley/flow/internal/projectsync" |
| 11 | "github.com/spf13/cobra" |
| 12 | ) |
| 13 | |
| 14 | var syncCmd = &cobra.Command{ |
| 15 | Use: "sync", |
| 16 | Short: "Fetch and fast-forward every registered project", |
| 17 | Long: `Synchronously fetches every project in ~/.klaus/projects.json and fast-forwards |
| 18 | its current branch to the upstream when the working tree is clean. |
| 19 | |
| 20 | Projects with a dirty tree, diverged branch, detached HEAD, or missing upstream |
| 21 | are reported as skipped and left untouched. klaus never resets or force-updates |
| 22 | a working clone. |
| 23 | |
| 24 | Exit code is 0 if every project succeeded (up-to-date / pulled / fetched-only) |
| 25 | or was intentionally skipped. A non-zero exit code means at least one project |
| 26 | hit an error (typically a fetch failure).`, |
| 27 | Args: cobra.NoArgs, |
| 28 | RunE: func(cmd *cobra.Command, args []string) error { |
| 29 | reg, err := project.Load() |
| 30 | if err != nil { |
| 31 | return fmt.Errorf("loading project registry: %w", err) |
| 32 | } |
| 33 | if reg == nil || len(reg.Projects) == 0 { |
| 34 | fmt.Fprintln(cmd.OutOrStdout(), "No projects registered. Use 'klaus project add' to register one.") |
| 35 | return nil |
| 36 | } |
| 37 | |
| 38 | results := projectsync.Sync(cmd.Context(), reg, git.NewExecClient()) |
| 39 | // Also append to the log file so CLI invocations appear there. |
| 40 | projectsync.WriteLog("cli", results) |
| 41 | |
| 42 | writeSyncTable(cmd.OutOrStdout(), results) |
| 43 | |
| 44 | for _, r := range results { |
| 45 | if r.Status == projectsync.StatusError { |
| 46 | return fmt.Errorf("one or more projects failed to sync") |
| 47 | } |
| 48 | } |
| 49 | return nil |
| 50 | }, |
| 51 | } |
| 52 | |
| 53 | func writeSyncTable(w io.Writer, results []projectsync.SyncResult) { |
| 54 | tw := tabwriter.NewWriter(w, 0, 0, 2, ' ', 0) |
| 55 | fmt.Fprintln(tw, "PROJECT\tSTATUS\tBRANCH\tDETAIL") |
| 56 | for _, r := range results { |
| 57 | status := string(r.Status) |
| 58 | // Include the skip reason in the STATUS column for parity with the |
| 59 | // documented output format (e.g. "skipped: dirty tree"). |
| 60 | if r.Status == projectsync.StatusSkipped && r.Detail != "" { |
| 61 | status = string(r.Status) + ": " + r.Detail |
| 62 | } else if r.Status == projectsync.StatusError && r.Detail != "" { |
| 63 | status = string(r.Status) + ": " + r.Detail |
| 64 | } |
| 65 | detail := r.Detail |
| 66 | if r.Status == projectsync.StatusSkipped || r.Status == projectsync.StatusError { |
| 67 | detail = "" // already rolled into status |
| 68 | } |
| 69 | fmt.Fprintf(tw, "%s\t%s\t%s\t%s\n", r.Name, status, r.Branch, detail) |
| 70 | } |
| 71 | tw.Flush() |
| 72 | } |
| 73 | |
| 74 | func init() { |
| 75 | rootCmd.AddCommand(syncCmd) |
| 76 | } |