MCPcopy Create free account
hub / github.com/LegacyCodeHQ/clarity-cli / writeAgentsFile

Function writeAgentsFile

cmd/setup/setup_cmd.go:49–84  ·  view source on GitHub ↗
(filename string)

Source from the content-addressed store, hash-verified

47}
48
49func writeAgentsFile(filename string) (bool, bool, error) {
50 _, err := filepath.Abs(filename)
51 if err != nil {
52 return false, false, fmt.Errorf("failed to get absolute path: %w", err)
53 }
54
55 // Check if file exists
56 _, err = os.Stat(filename)
57 if err != nil && !os.IsNotExist(err) {
58 return false, false, fmt.Errorf("failed to stat %s: %w", filename, err)
59 }
60 fileExists := !os.IsNotExist(err)
61
62 if fileExists {
63 existing, err := os.ReadFile(filename)
64 if err != nil {
65 return false, false, fmt.Errorf("failed to read %s: %w", filename, err)
66 }
67
68 if hasSetupBlock(existing) {
69 return false, false, nil
70 }
71
72 updatedContent := appendSetupBlock(string(existing))
73 if err := os.WriteFile(filename, []byte(updatedContent), 0644); err != nil {
74 return false, false, fmt.Errorf("failed to update %s: %w", filename, err)
75 }
76 } else {
77 // Create new file or overwrite
78 if err := os.WriteFile(filename, []byte(buildSetupBlock(true)), 0644); err != nil {
79 return false, false, fmt.Errorf("failed to write %s: %w", filename, err)
80 }
81 }
82
83 return !fileExists, true, nil
84}
85
86func hasSetupBlock(contents []byte) bool {
87 lower := strings.ToLower(string(contents))

Calls 3

hasSetupBlockFunction · 0.85
appendSetupBlockFunction · 0.85
buildSetupBlockFunction · 0.85