302 lines
8.1 KiB
Go
302 lines
8.1 KiB
Go
package setup
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/steveyegge/beads/internal/templates/agents"
|
|
)
|
|
|
|
const (
|
|
codexInstructionMarker = "BEADS CODEX SETUP"
|
|
codexBeginMarker = "<!-- BEGIN " + codexInstructionMarker + ": generated by bd setup codex -->"
|
|
codexEndMarker = "<!-- END " + codexInstructionMarker + " -->"
|
|
codexInstructionsFile = "AGENTS.md"
|
|
codexConfigDir = ".codex"
|
|
codexHomeEnvVar = "CODEX_HOME"
|
|
)
|
|
|
|
var (
|
|
codexEnvProvider = defaultCodexEnv
|
|
errCodexInstructionsMissing = errors.New("codex instructions not installed")
|
|
errCodexInstructionsStale = errors.New("codex instructions are stale")
|
|
errCodexInstructionsNoMarker = errors.New("codex instructions section missing")
|
|
)
|
|
|
|
type codexEnv struct {
|
|
stdout io.Writer
|
|
stderr io.Writer
|
|
homeDir string
|
|
projectDir string
|
|
ensureDir func(string, os.FileMode) error
|
|
readFile func(string) ([]byte, error)
|
|
writeFile func(string, []byte) error
|
|
removeFile func(string) error
|
|
getenv func(string) string
|
|
}
|
|
|
|
func defaultCodexEnv() (codexEnv, error) {
|
|
home, err := os.UserHomeDir()
|
|
if err != nil {
|
|
return codexEnv{}, fmt.Errorf("home directory: %w", err)
|
|
}
|
|
workDir, err := os.Getwd()
|
|
if err != nil {
|
|
return codexEnv{}, fmt.Errorf("working directory: %w", err)
|
|
}
|
|
return codexEnv{
|
|
stdout: os.Stdout,
|
|
stderr: os.Stderr,
|
|
homeDir: home,
|
|
projectDir: workDir,
|
|
ensureDir: EnsureDir,
|
|
readFile: os.ReadFile,
|
|
writeFile: func(path string, data []byte) error {
|
|
return atomicWriteFile(path, data)
|
|
},
|
|
removeFile: os.Remove,
|
|
getenv: os.Getenv,
|
|
}, nil
|
|
}
|
|
|
|
// InstallCodex installs Codex integration.
|
|
func InstallCodex(global bool) {
|
|
env, err := codexEnvProvider()
|
|
if err != nil {
|
|
_, _ = fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
|
setupExit(1)
|
|
return
|
|
}
|
|
if err := installCodex(env, global); err != nil {
|
|
setupExit(1)
|
|
}
|
|
}
|
|
|
|
func installCodex(env codexEnv, global bool) error {
|
|
if err := installAgentSkill(codexAgentSkillEnv(env, global)); err != nil {
|
|
return err
|
|
}
|
|
return installCodexInstructions(env, global)
|
|
}
|
|
|
|
// CheckCodex checks if Codex integration is installed.
|
|
func CheckCodex(global bool) {
|
|
env, err := codexEnvProvider()
|
|
if err != nil {
|
|
_, _ = fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
|
setupExit(1)
|
|
return
|
|
}
|
|
if err := checkCodex(env, global); err != nil {
|
|
setupExit(1)
|
|
}
|
|
}
|
|
|
|
func checkCodex(env codexEnv, global bool) error {
|
|
if err := checkAgentSkill(codexAgentSkillEnv(env, global), codexSetupCommand(global)); err != nil {
|
|
return err
|
|
}
|
|
return checkCodexInstructions(env, global)
|
|
}
|
|
|
|
// RemoveCodex removes Codex integration.
|
|
func RemoveCodex(global bool) {
|
|
env, err := codexEnvProvider()
|
|
if err != nil {
|
|
_, _ = fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
|
setupExit(1)
|
|
return
|
|
}
|
|
if err := removeCodex(env, global); err != nil {
|
|
setupExit(1)
|
|
}
|
|
}
|
|
|
|
func removeCodex(env codexEnv, global bool) error {
|
|
if err := removeAgentSkill(codexAgentSkillEnv(env, global)); err != nil {
|
|
return err
|
|
}
|
|
return removeCodexInstructions(env, global)
|
|
}
|
|
|
|
func codexSetupCommand(global bool) string {
|
|
if global {
|
|
return "bd setup codex --global"
|
|
}
|
|
return "bd setup codex"
|
|
}
|
|
|
|
func codexRootDir(env codexEnv, global bool) string {
|
|
if global {
|
|
return env.homeDir
|
|
}
|
|
return env.projectDir
|
|
}
|
|
|
|
func codexHomeDir(env codexEnv) string {
|
|
if env.getenv != nil {
|
|
if path := env.getenv(codexHomeEnvVar); path != "" {
|
|
return path
|
|
}
|
|
}
|
|
return filepath.Join(env.homeDir, codexConfigDir)
|
|
}
|
|
|
|
func codexInstructionsPath(env codexEnv, global bool) string {
|
|
if global {
|
|
return filepath.Join(codexHomeDir(env), codexInstructionsFile)
|
|
}
|
|
return filepath.Join(env.projectDir, codexInstructionsFile)
|
|
}
|
|
|
|
func codexAgentSkillEnv(env codexEnv, global bool) agentSkillEnv {
|
|
return agentSkillEnv{
|
|
stdout: env.stdout,
|
|
stderr: env.stderr,
|
|
projectDir: codexRootDir(env, global),
|
|
ensureDir: env.ensureDir,
|
|
readFile: env.readFile,
|
|
writeFile: env.writeFile,
|
|
removeFile: env.removeFile,
|
|
}
|
|
}
|
|
|
|
func codexManagedSection() string {
|
|
return codexBeginMarker + "\n" + agents.CodexSectionBody() + "\n" + codexEndMarker
|
|
}
|
|
|
|
func installCodexInstructions(env codexEnv, global bool) error {
|
|
path := codexInstructionsPath(env, global)
|
|
if global {
|
|
_, _ = fmt.Fprintln(env.stdout, "Installing Codex instructions globally...")
|
|
} else {
|
|
_, _ = fmt.Fprintln(env.stdout, "Installing Codex instructions for this project...")
|
|
}
|
|
|
|
if err := env.ensureDir(filepath.Dir(path), 0o755); err != nil {
|
|
_, _ = fmt.Fprintf(env.stderr, "Error: %v\n", err)
|
|
return err
|
|
}
|
|
|
|
var current string
|
|
data, err := env.readFile(path)
|
|
switch {
|
|
case err == nil:
|
|
current = string(data)
|
|
case os.IsNotExist(err):
|
|
default:
|
|
_, _ = fmt.Fprintf(env.stderr, "Error: read %s: %v\n", path, err)
|
|
return err
|
|
}
|
|
|
|
next := upsertCodexManagedSection(current)
|
|
if err := env.writeFile(path, []byte(next)); err != nil {
|
|
_, _ = fmt.Fprintf(env.stderr, "Error: write %s: %v\n", path, err)
|
|
return err
|
|
}
|
|
|
|
_, _ = fmt.Fprintln(env.stdout, "✓ Codex instructions installed")
|
|
_, _ = fmt.Fprintf(env.stdout, " File: %s\n", path)
|
|
_, _ = fmt.Fprintln(env.stdout, "Restart Codex if it is already running.")
|
|
return nil
|
|
}
|
|
|
|
func checkCodexInstructions(env codexEnv, global bool) error {
|
|
path := codexInstructionsPath(env, global)
|
|
data, err := env.readFile(path)
|
|
if os.IsNotExist(err) {
|
|
_, _ = fmt.Fprintf(env.stdout, "✗ Codex instructions not found: %s\n", path)
|
|
_, _ = fmt.Fprintf(env.stdout, " Run: %s\n", codexSetupCommand(global))
|
|
return errCodexInstructionsMissing
|
|
}
|
|
if err != nil {
|
|
_, _ = fmt.Fprintf(env.stderr, "Error: read %s: %v\n", path, err)
|
|
return err
|
|
}
|
|
|
|
section, ok := extractCodexManagedSection(string(data))
|
|
if !ok {
|
|
_, _ = fmt.Fprintf(env.stdout, "⚠ Codex instructions exist but no beads section found: %s\n", path)
|
|
_, _ = fmt.Fprintf(env.stdout, " Run: %s\n", codexSetupCommand(global))
|
|
return errCodexInstructionsNoMarker
|
|
}
|
|
if section != codexManagedSection() {
|
|
_, _ = fmt.Fprintf(env.stdout, "⚠ Codex instructions installed but stale: %s\n", path)
|
|
_, _ = fmt.Fprintf(env.stdout, " Run: %s\n", codexSetupCommand(global))
|
|
return errCodexInstructionsStale
|
|
}
|
|
|
|
_, _ = fmt.Fprintf(env.stdout, "✓ Codex instructions installed: %s\n", path)
|
|
return nil
|
|
}
|
|
|
|
func removeCodexInstructions(env codexEnv, global bool) error {
|
|
path := codexInstructionsPath(env, global)
|
|
_, _ = fmt.Fprintln(env.stdout, "Removing Codex instructions...")
|
|
data, err := env.readFile(path)
|
|
if os.IsNotExist(err) {
|
|
_, _ = fmt.Fprintln(env.stdout, "No Codex instructions file found")
|
|
return nil
|
|
}
|
|
if err != nil {
|
|
_, _ = fmt.Fprintf(env.stderr, "Error: read %s: %v\n", path, err)
|
|
return err
|
|
}
|
|
|
|
next, ok := removeCodexManagedSection(string(data))
|
|
if !ok {
|
|
_, _ = fmt.Fprintln(env.stdout, "No beads Codex section found")
|
|
return nil
|
|
}
|
|
if err := env.writeFile(path, []byte(next)); err != nil {
|
|
_, _ = fmt.Fprintf(env.stderr, "Error: write %s: %v\n", path, err)
|
|
return err
|
|
}
|
|
|
|
_, _ = fmt.Fprintln(env.stdout, "✓ Codex instructions removed")
|
|
return nil
|
|
}
|
|
|
|
func upsertCodexManagedSection(content string) string {
|
|
section := codexManagedSection()
|
|
if _, ok := extractCodexManagedSection(content); ok {
|
|
start := strings.Index(content, codexBeginMarker)
|
|
end := strings.Index(content, codexEndMarker)
|
|
end += len(codexEndMarker)
|
|
return content[:start] + section + content[end:]
|
|
}
|
|
if strings.TrimSpace(content) == "" {
|
|
return "# Codex Instructions\n\n" + section + "\n"
|
|
}
|
|
if !strings.HasSuffix(content, "\n") {
|
|
content += "\n"
|
|
}
|
|
return content + "\n" + section + "\n"
|
|
}
|
|
|
|
func extractCodexManagedSection(content string) (string, bool) {
|
|
start := strings.Index(content, codexBeginMarker)
|
|
end := strings.Index(content, codexEndMarker)
|
|
if start == -1 || end == -1 || start > end {
|
|
return "", false
|
|
}
|
|
end += len(codexEndMarker)
|
|
return content[start:end], true
|
|
}
|
|
|
|
func removeCodexManagedSection(content string) (string, bool) {
|
|
start := strings.Index(content, codexBeginMarker)
|
|
end := strings.Index(content, codexEndMarker)
|
|
if start == -1 || end == -1 || start > end {
|
|
return content, false
|
|
}
|
|
end += len(codexEndMarker)
|
|
if end < len(content) && content[end] == '\n' {
|
|
end++
|
|
}
|
|
return content[:start] + content[end:], true
|
|
}
|