-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
77 lines (61 loc) · 1.6 KB
/
Copy pathmain.go
File metadata and controls
77 lines (61 loc) · 1.6 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
package main
import (
"context"
"embed"
"flag"
"fmt"
"io/fs"
"log"
"os"
"os/signal"
"syscall"
"github.com/ai-shift/opencode"
)
//go:embed .opencode
var configFS embed.FS
func main() {
dir := flag.String("dir", "", "Directory for opencode to operate in (defaults to current directory)")
flag.Parse()
// Get current working directory as default
cwd, err := os.Getwd()
if err != nil {
log.Fatalf("Failed to get current directory: %v", err)
}
// Determine session directory (defaults to current directory)
sessionDir := *dir
if sessionDir == "" {
sessionDir = cwd
}
// Ensure session directory exists
if err := os.MkdirAll(sessionDir, 0755); err != nil {
log.Fatalf("Failed to create directory %s: %v", sessionDir, err)
}
// Create sub FS for config files
subFS, err := fs.Sub(configFS, ".opencode")
if err != nil {
log.Fatalf("Failed to create sub FS: %v", err)
}
cfg := opencode.Config{
CWD: sessionDir,
ConfigFS: subFS,
}
oc := opencode.New(cfg)
fmt.Printf("Starting OpenCode server in directory: %s\n", sessionDir)
go func() {
if err := oc.Start(); err != nil {
log.Fatalf("Failed to start opencode: %v", err)
}
}()
defer oc.Stop()
defer oc.Cleanup()
if err := oc.WaitForReady(context.Background()); err != nil {
log.Fatalf("Failed to connect to opencode: %v", err)
}
fmt.Printf("OpenCode server is ready at: http://%s\n", oc.Addr())
fmt.Println("Press Ctrl+C to stop the server")
// Wait for interrupt signal
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
<-sigChan
fmt.Println("\nStopping OpenCode server...")
}