| 1 | package cmd |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "fmt" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | |
| 9 | "github.com/spf13/cobra" |
| 10 | |
| 11 | "the-valley/flow/internal/flowenv" |
| 12 | ) |
| 13 | |
| 14 | var ( |
| 15 | version = "dev" |
| 16 | ) |
| 17 | |
| 18 | // helperName is the program name under which this binary is the valley |
| 19 | // client's flow verb: `valley flow` runs valley-flow from PATH, and the |
| 20 | // package installs it beside klaus as the same binary. |
| 21 | const helperName = "valley-flow" |
| 22 | |
| 23 | var rootCmd = &cobra.Command{ |
| 24 | Use: "klaus", |
| 25 | Short: "Multi-agent orchestrator for Claude Code, Codex, and agy", |
| 26 | Long: `flow orchestrates parallel coding agents using git worktrees and tmux panes. |
| 27 | |
| 28 | It launches autonomous agents in isolated worktrees, manages their lifecycle, |
| 29 | streams and formats their output, and tracks run state. |
| 30 | |
| 31 | Running 'valley flow' with no arguments resumes the most recent coordinator session. |
| 32 | If no session exists, a new one is created. Use 'valley flow new' to explicitly start |
| 33 | a fresh session.`, |
| 34 | Version: version, |
| 35 | RunE: func(cmd *cobra.Command, args []string) error { |
| 36 | return runSession(cmd, false) |
| 37 | }, |
| 38 | } |
| 39 | |
| 40 | func Execute() error { |
| 41 | nameRoot(invokedAs(os.Args[0])) |
| 42 | // Every process flow starts inherits this one's environment: under |
| 43 | // the bridge generation it holds each flow variable under both names. |
| 44 | flowenv.NormalizeProcess() |
| 45 | return rootCmd.Execute() |
| 46 | } |
| 47 | |
| 48 | // invokedAs is the program name argv0 starts this binary under: |
| 49 | // helperName, or klaus for any other name. |
| 50 | func invokedAs(argv0 string) string { |
| 51 | if filepath.Base(argv0) == helperName { |
| 52 | return helperName |
| 53 | } |
| 54 | return "klaus" |
| 55 | } |
| 56 | |
| 57 | // nameRoot has the root command name itself as program, which invokedAs |
| 58 | // gave: `valley flow` in usage, help and --version under helperName, and |
| 59 | // klaus otherwise. |
| 60 | func nameRoot(program string) { |
| 61 | name := "klaus" |
| 62 | if program == helperName { |
| 63 | name = "valley flow" |
| 64 | if rootCmd.Annotations == nil { |
| 65 | rootCmd.Annotations = map[string]string{} |
| 66 | } |
| 67 | rootCmd.Annotations[cobra.CommandDisplayNameAnnotation] = name |
| 68 | } else { |
| 69 | delete(rootCmd.Annotations, cobra.CommandDisplayNameAnnotation) |
| 70 | } |
| 71 | rootCmd.SetVersionTemplate(fmt.Sprintf("%s version %s\n", name, version)) |
| 72 | } |
| 73 | |
| 74 | // exitError is an error that sets klaus's exit status. |
| 75 | type exitError struct { |
| 76 | code int |
| 77 | err error |
| 78 | } |
| 79 | |
| 80 | func (e *exitError) Error() string { return e.err.Error() } |
| 81 | func (e *exitError) Unwrap() error { return e.err } |
| 82 | |
| 83 | // ExitCode is the exit status for an error Execute returned: the one the |
| 84 | // error asks for, else 1. |
| 85 | func ExitCode(err error) int { |
| 86 | var e *exitError |
| 87 | if errors.As(err, &e) { |
| 88 | return e.code |
| 89 | } |
| 90 | return 1 |
| 91 | } |
| 92 | |
| 93 | func init() { |
| 94 | nameRoot("klaus") |
| 95 | } |