-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
171 lines (142 loc) · 3.83 KB
/
main.go
File metadata and controls
171 lines (142 loc) · 3.83 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
// Lorah - Simple infinite loop harness for Claude Code
//
// Usage: lorah <command> [arguments]
//
// Runs Claude Code CLI in a continuous loop with formatted output.
package main
import (
"fmt"
"os"
"strings"
"github.com/cpplain/lorah/internal/loop"
"github.com/cpplain/lorah/internal/task"
)
// Version is set via ldflags during build. Default is "dev" for local builds.
var Version = "dev"
func printUsage() {
fmt.Fprint(os.Stderr, `Usage: lorah [--dir=DIR] <command> [arguments]
Simple infinite-loop harness for Claude Code.
Commands:
run Run Claude Code CLI in an infinite loop
task Manage tasks
Flags:
--dir Lorah data directory (default .lorah)
-V, --version Print version and exit
-h, --help Show this help message
Run 'lorah <command> --help' for command-specific help.
`)
}
func printRunUsage() {
fmt.Fprint(os.Stderr, `Usage: lorah run <prompt-file> [claude-flags...]
Run Claude Code CLI in a continuous loop with formatted output.
Retries automatically on error with a 5-second delay.
Arguments:
<prompt-file> Path to prompt file (required)
[claude-flags...] Flags passed directly to claude CLI
Examples:
lorah run prompt.md
lorah run task.txt --settings .lorah/settings.json
lorah run instructions.md --model claude-opus-4-6 --max-turns 50
Flags:
-h, --help Show this help message
`)
}
func printTaskUsage() {
fmt.Fprint(os.Stderr, `Usage: lorah task <subcommand> [args...] [flags...]
Manage tasks stored in .lorah/tasks.json.
Subcommands:
list List tasks
get Get task details
create Create a new task
update Update a task
delete Delete a task
export Export tasks to markdown
Flags:
-h, --help Show this help message
Run 'lorah task <subcommand> --help' for subcommand-specific help.
`)
}
// route dispatches CLI arguments to the appropriate handler and returns an exit code.
func route(args []string, version string, runFn func(string, []string), taskFn func(string, []string) error) int {
if len(args) == 0 {
printUsage()
return 1
}
switch args[0] {
case "--version", "-version", "-V":
fmt.Printf("lorah %s\n", version)
return 0
case "--help", "-help", "-h":
printUsage()
return 0
}
// Extract --dir before command dispatch.
dir := ".lorah"
var remaining []string
for i := 0; i < len(args); i++ {
if args[i] == "--dir" && i+1 < len(args) {
dir = args[i+1]
i++
} else if strings.HasPrefix(args[i], "--dir=") {
dir = strings.TrimPrefix(args[i], "--dir=")
} else {
remaining = append(remaining, args[i])
}
}
if len(remaining) == 0 {
printUsage()
return 1
}
switch remaining[0] {
case "run":
runArgs := remaining[1:]
if len(runArgs) == 0 {
printRunUsage()
return 1
}
if runArgs[0] == "--help" || runArgs[0] == "-help" || runArgs[0] == "-h" {
printRunUsage()
return 0
}
promptFile := runArgs[0]
claudeFlags := runArgs[1:]
runFn(promptFile, claudeFlags)
return 0
case "task":
taskArgs := remaining[1:]
if len(taskArgs) == 0 {
printTaskUsage()
return 1
}
if taskArgs[0] == "--help" || taskArgs[0] == "-help" || taskArgs[0] == "-h" {
printTaskUsage()
return 0
}
if err := taskFn(dir, taskArgs); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
return 1
}
return 0
default:
fmt.Fprintf(os.Stderr, "Unknown command: %s\n", remaining[0])
printUsage()
return 1
}
}
func runCmd(promptFile string, claudeFlags []string) {
loop.Run(promptFile, claudeFlags)
}
func taskCmd(dir string, args []string) error {
if err := os.MkdirAll(dir, 0755); err != nil {
return fmt.Errorf("creating storage directory: %w", err)
}
storage := task.NewJSONStorage(dir)
code := task.HandleTask(args, os.Stdout, storage)
if code != 0 {
return fmt.Errorf("task command failed")
}
return nil
}
func main() {
os.Exit(route(os.Args[1:], Version, runCmd, taskCmd))
}