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