gunk-dev/the-valley

root.go

source4cbb5ec9c5dbmain's published tip history
2.5 KiB
1package cmd
2
3import (
4 "errors"
5 "fmt"
6 "os"
7 "path/filepath"
8
9 "github.com/spf13/cobra"
10
11 "the-valley/flow/internal/flowenv"
12)
13
14var (
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.
21const helperName = "valley-flow"
22
23var 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
28It launches autonomous agents in isolated worktrees, manages their lifecycle,
29streams and formats their output, and tracks run state.
30
31Running 'valley flow' with no arguments resumes the most recent coordinator session.
32If no session exists, a new one is created. Use 'valley flow new' to explicitly start
33a fresh session.`,
34 Version: version,
35 RunE: func(cmd *cobra.Command, args []string) error {
36 return runSession(cmd, false)
37 },
38}
39
40func 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.
50func 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.
60func 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.
75type exitError struct {
76 code int
77 err error
78}
79
80func (e *exitError) Error() string { return e.err.Error() }
81func (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.
85func ExitCode(err error) int {
86 var e *exitError
87 if errors.As(err, &e) {
88 return e.code
89 }
90 return 1
91}
92
93func init() {
94 nameRoot("klaus")
95}