-
Notifications
You must be signed in to change notification settings - Fork 595
Expand file tree
/
Copy pathmain.go
More file actions
175 lines (149 loc) · 4.42 KB
/
Copy pathmain.go
File metadata and controls
175 lines (149 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package main
import (
"claude-squad/app"
cmd2 "claude-squad/cmd"
"claude-squad/config"
"claude-squad/daemon"
"claude-squad/log"
"claude-squad/session"
"claude-squad/session/git"
"claude-squad/session/tmux"
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"github.com/spf13/cobra"
)
var (
version = "1.0.19"
programFlag string
autoYesFlag bool
daemonFlag bool
binName string
rootCmd = &cobra.Command{
Use: "claude-squad",
Short: "Claude Squad - Manage multiple AI agents like Claude Code, Aider, Codex, and Amp.",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
log.Initialize(daemonFlag)
defer log.Close()
if daemonFlag {
cfg := config.LoadConfig()
err := daemon.RunDaemon(cfg)
log.ErrorLog.Printf("failed to start daemon %v", err)
return err
}
// Check if we're in a git repository
currentDir, err := filepath.Abs(".")
if err != nil {
return fmt.Errorf("failed to get current directory: %w", err)
}
if !git.IsGitRepo(currentDir) {
return fmt.Errorf("error: %s must be run from within a git repository", binName)
}
cfg := config.LoadConfig()
// Program flag overrides config
program := cfg.GetProgram()
if programFlag != "" {
program = programFlag
}
// AutoYes flag overrides config
autoYes := cfg.AutoYes
if autoYesFlag {
autoYes = true
}
if autoYes {
defer func() {
if err := daemon.LaunchDaemon(); err != nil {
log.ErrorLog.Printf("failed to launch daemon: %v", err)
}
}()
}
// Kill any daemon that's running.
if err := daemon.StopDaemon(); err != nil {
log.ErrorLog.Printf("failed to stop daemon: %v", err)
}
return app.Run(ctx, program, autoYes)
},
}
resetCmd = &cobra.Command{
Use: "reset",
Short: "Reset all stored instances",
RunE: func(cmd *cobra.Command, args []string) error {
log.Initialize(false)
defer log.Close()
state := config.LoadState()
storage, err := session.NewStorage(state)
if err != nil {
return fmt.Errorf("failed to initialize storage: %w", err)
}
if err := storage.DeleteAllInstances(); err != nil {
return fmt.Errorf("failed to reset storage: %w", err)
}
fmt.Println("Storage has been reset successfully")
if err := tmux.CleanupSessions(cmd2.MakeExecutor()); err != nil {
return fmt.Errorf("failed to cleanup tmux sessions: %w", err)
}
fmt.Println("Tmux sessions have been cleaned up")
if err := git.CleanupWorktrees(); err != nil {
return fmt.Errorf("failed to cleanup worktrees: %w", err)
}
fmt.Println("Worktrees have been cleaned up")
// Kill any daemon that's running.
if err := daemon.StopDaemon(); err != nil {
return err
}
fmt.Println("daemon has been stopped")
return nil
},
}
debugCmd = &cobra.Command{
Use: "debug",
Short: "Print debug information like config paths",
RunE: func(cmd *cobra.Command, args []string) error {
log.Initialize(false)
defer log.Close()
cfg := config.LoadConfig()
configDir, err := config.GetConfigDir()
if err != nil {
return fmt.Errorf("failed to get config directory: %w", err)
}
configJson, _ := json.MarshalIndent(cfg, "", " ")
fmt.Printf("Config: %s\n%s\n", filepath.Join(configDir, config.ConfigFileName), configJson)
return nil
},
}
versionCmd = &cobra.Command{
Use: "version",
Short: "Print the version number",
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("%s version %s\n", binName, version)
fmt.Printf("https://github.com/smtg-ai/claude-squad/releases/tag/v%s\n", version)
},
}
)
func init() {
rootCmd.Flags().StringVarP(&programFlag, "program", "p", "",
"Program to run in new instances (e.g. 'aider --model ollama_chat/gemma3:1b')")
rootCmd.Flags().BoolVarP(&autoYesFlag, "autoyes", "y", false,
"[experimental] If enabled, all instances will automatically accept prompts")
rootCmd.Flags().BoolVar(&daemonFlag, "daemon", false, "Run a program that loads all sessions"+
" and runs autoyes mode on them.")
// Hide the daemonFlag as it's only for internal use
err := rootCmd.Flags().MarkHidden("daemon")
if err != nil {
panic(err)
}
rootCmd.AddCommand(debugCmd)
rootCmd.AddCommand(versionCmd)
rootCmd.AddCommand(resetCmd)
}
func main() {
// Extract the binary name from how this was invoked
binName = filepath.Base(os.Args[0])
rootCmd.Use = binName
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
}
}