-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
155 lines (130 loc) · 3.55 KB
/
main.go
File metadata and controls
155 lines (130 loc) · 3.55 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
package main
import (
"flag"
"fmt"
"os"
"runtime/debug"
"github.com/emilrex/wt/internal/cmd"
)
var version = "dev"
func init() {
if version == "dev" {
if info, ok := debug.ReadBuildInfo(); ok && info.Main.Version != "" {
version = info.Main.Version
}
}
}
const usage = `wt - Manage isolated git worktrees for parallel Claude Code sessions
Usage:
wt <command> [arguments]
Commands:
new [name] [-b branch] Create a new worktree session and launch Claude Code
fg <session-name> Resume an existing session (foreground)
ls List all active sessions
rm <session-name> Remove a session
rm -a|--all Remove all sessions
cd <session-name> Open a shell in a session's worktree
Examples:
wt new # New session with auto-generated name
wt new auth-feature # New session named 'auth-feature'
wt new hotfix -b main # New session from main branch
wt fg auth-feature # Resume the auth-feature session
wt ls # List all sessions
wt rm auth-feature # Remove specific session
wt rm --all # Remove all sessions
wt cd auth-feature # Open shell in session directory
`
func main() {
if len(os.Args) < 2 {
fmt.Print(usage)
os.Exit(1)
}
command := os.Args[1]
switch command {
case "new":
runNew(os.Args[2:])
case "fg":
runFg(os.Args[2:])
case "ls":
runLs()
case "rm":
runRm(os.Args[2:])
case "cd":
runCd(os.Args[2:])
case "-h", "--help", "help":
fmt.Print(usage)
case "-v", "--version", "version":
fmt.Println(version)
default:
fmt.Fprintf(os.Stderr, "Unknown command: %s\n\n", command)
fmt.Print(usage)
os.Exit(1)
}
}
func runNew(args []string) {
fs := flag.NewFlagSet("new", flag.ExitOnError)
branch := fs.String("b", "", "Source branch to create worktree from")
fs.StringVar(branch, "branch", "", "Source branch to create worktree from")
_ = fs.Parse(args) // ExitOnError handles errors
opts := cmd.NewOptions{
SourceBranch: *branch,
}
// First non-flag argument is the session name
if fs.NArg() > 0 {
opts.Name = fs.Arg(0)
}
if err := cmd.RunNew(opts); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}
func runFg(args []string) {
if len(args) < 1 {
fmt.Fprintln(os.Stderr, "Error: session name required")
fmt.Fprintln(os.Stderr, "Usage: wt fg <session-name>")
os.Exit(1)
}
if err := cmd.RunFg(args[0]); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}
func runLs() {
if err := cmd.RunLs(); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}
func runRm(args []string) {
fs := flag.NewFlagSet("rm", flag.ExitOnError)
all := fs.Bool("a", false, "Remove all sessions")
fs.BoolVar(all, "all", false, "Remove all sessions")
_ = fs.Parse(args) // ExitOnError handles errors
opts := cmd.RmOptions{
All: *all,
}
if fs.NArg() > 0 {
opts.SessionName = fs.Arg(0)
}
if !opts.All && opts.SessionName == "" {
fmt.Fprintln(os.Stderr, "Error: session name required (or use --all)")
fmt.Fprintln(os.Stderr, "Usage: wt rm <session-name>")
fmt.Fprintln(os.Stderr, " wt rm -a|--all")
os.Exit(1)
}
if err := cmd.RunRm(opts); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}
func runCd(args []string) {
if len(args) < 1 {
fmt.Fprintln(os.Stderr, "Error: session name required")
fmt.Fprintln(os.Stderr, "Usage: wt cd <session-name>")
os.Exit(1)
}
if err := cmd.RunCd(args[0]); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}