gunk-dev/the-valley

root.go

sourcecaaa5bee5e11main's published tip history
2.3 KiB
1package cmd
2
3import (
4 "errors"
5 "fmt"
6 "os"
7 "path/filepath"
8
9 "github.com/spf13/cobra"
10)
11
12var (
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.
19const helperName = "valley-flow"
20
21var rootCmd = &cobra.Command{
22 Use: "klaus",
23 Short: "Multi-agent orchestrator for Claude Code, Codex, and agy",
24 Long: `flow orchestrates parallel coding agents using git worktrees and tmux panes.
25
26It launches autonomous agents in isolated worktrees, manages their lifecycle,
27streams and formats their output, and tracks run state.
28
29Running 'valley flow' with no arguments resumes the most recent coordinator session.
30If no session exists, a new one is created. Use 'valley flow new' to explicitly start
31a fresh session.`,
32 Version: version,
33 RunE: func(cmd *cobra.Command, args []string) error {
34 return runSession(cmd, false)
35 },
36}
37
38func 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.
45func 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.
55func 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.
70type exitError struct {
71 code int
72 err error
73}
74
75func (e *exitError) Error() string { return e.err.Error() }
76func (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.
80func ExitCode(err error) int {
81 var e *exitError
82 if errors.As(err, &e) {
83 return e.code
84 }
85 return 1
86}
87
88func init() {
89 nameRoot("klaus")
90}