Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
172 lines (151 loc) · 3.47 KB
/
Copy pathmain.go
File metadata and controls
172 lines (151 loc) · 3.47 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
package cmd
import (
"fmt"
"os"
"path/filepath"
"github.com/yifanes/miniclawd/internal/app"
"github.com/yifanes/miniclawd/internal/config"
"github.com/yifanes/miniclawd/internal/gateway"
"github.com/yifanes/miniclawd/internal/hooks"
"github.com/yifanes/miniclawd/internal/logging"
)
var version = "dev"
func Run() int {
if len(os.Args) < 2 {
printUsage()
return 1
}
switch os.Args[1] {
case "start":
return runStart()
case "setup":
return runSetup()
case "doctor":
return runDoctor()
case "gateway":
return runGateway()
case "hooks":
return runHooks()
case "version":
fmt.Printf("miniclawd %s\n", version)
return 0
case "help", "-h", "--help":
printUsage()
return 0
default:
fmt.Fprintf(os.Stderr, "unknown command: %s\n", os.Args[1])
printUsage()
return 1
}
}
func printUsage() {
fmt.Println(`miniclawd - multi-platform AI chat bot
Usage:
miniclawd <command>
Commands:
start Start the bot
setup Interactive setup wizard
doctor Run preflight diagnostics
gateway Manage background gateway service
hooks Manage hooks (list, enable, disable)
version Print version
help Show this help`)
}
func runStart() int {
cfg, db, err := bootstrap()
if err != nil {
fmt.Fprintf(os.Stderr, "startup error: %v\n", err)
return 1
}
defer db.Close()
// In gateway mode, redirect logs to hourly-rotated files.
if os.Getenv("MINICLAWD_GATEWAY") != "" {
logsDir := filepath.Join(cfg.RuntimeDir(), "logs")
if err := logging.InitFileLogging(logsDir); err != nil {
fmt.Fprintf(os.Stderr, "logging init error: %v\n", err)
return 1
}
}
if err := app.Run(cfg, db); err != nil {
fmt.Fprintf(os.Stderr, "runtime error: %v\n", err)
return 1
}
return 0
}
func runGateway() int {
args := os.Args[2:]
if err := gateway.Run(args); err != nil {
fmt.Fprintf(os.Stderr, "gateway error: %v\n", err)
return 1
}
return 0
}
func runSetup() int {
if err := app.RunSetup(); err != nil {
fmt.Fprintf(os.Stderr, "setup error: %v\n", err)
return 1
}
return 0
}
func runDoctor() int {
cfg, err := config.LoadConfig()
if err != nil {
fmt.Fprintf(os.Stderr, "config error: %v\n", err)
fmt.Println("Run 'miniclawd setup' to create a config file.")
return 1
}
checks := app.RunDoctor(cfg)
fmt.Print(app.FormatChecks(checks))
for _, c := range checks {
if c.Status == "fail" {
return 1
}
}
return 0
}
func runHooks() int {
cfg, err := config.LoadConfig()
if err != nil {
fmt.Fprintf(os.Stderr, "config error: %v\n", err)
return 1
}
mgr := hooks.NewHookManager(cfg.DataDir)
hks := mgr.ListHooks()
if len(os.Args) < 3 || os.Args[2] == "list" {
if len(hks) == 0 {
fmt.Println("No hooks found.")
return 0
}
for _, h := range hks {
status := "enabled"
if !h.Enabled {
status = "disabled"
}
fmt.Printf(" %s [%s] event=%s cmd=%s\n", h.Name, status, h.Event, h.Command)
}
return 0
}
if len(os.Args) < 4 {
fmt.Fprintf(os.Stderr, "usage: miniclawd hooks <enable|disable> <hook-name>\n")
return 1
}
hookName := os.Args[3]
switch os.Args[2] {
case "enable":
if !mgr.EnableHook(hookName) {
fmt.Fprintf(os.Stderr, "hook %q not found\n", hookName)
return 1
}
fmt.Printf("Hook %q enabled.\n", hookName)
case "disable":
if !mgr.DisableHook(hookName) {
fmt.Fprintf(os.Stderr, "hook %q not found\n", hookName)
return 1
}
fmt.Printf("Hook %q disabled.\n", hookName)
default:
fmt.Fprintf(os.Stderr, "unknown hooks subcommand: %s\n", os.Args[2])
return 1
}
return 0
}