1package config
2
3import (
4 "fmt"
5 "os"
6 "path/filepath"
7 "strings"
8)
9
10const (
11 // InitFlagFilename is the name of the file that indicates whether the project has been initialized
12 InitFlagFilename = "init"
13)
14
15// ProjectInitFlag represents the initialization status for a project directory
16type ProjectInitFlag struct {
17 Initialized bool `json:"initialized"`
18}
19
20// ShouldShowInitDialog checks if the initialization dialog should be shown for the current directory
21func ShouldShowInitDialog() (bool, error) {
22 if cfg == nil {
23 return false, fmt.Errorf("config not loaded")
24 }
25
26 // Create the flag file path
27 flagFilePath := filepath.Join(cfg.Data.Directory, InitFlagFilename)
28
29 // Check if the flag file exists
30 _, err := os.Stat(flagFilePath)
31 if err == nil {
32 // File exists, don't show the dialog
33 return false, nil
34 }
35
36 // If the error is not "file not found", return the error
37 if !os.IsNotExist(err) {
38 return false, fmt.Errorf("failed to check init flag file: %w", err)
39 }
40
41 // Check if any variation of CRUSH.md already exists in working directory
42 crushExists, err := crushMdExists(WorkingDirectory())
43 if err != nil {
44 return false, fmt.Errorf("failed to check for CRUSH.md files: %w", err)
45 }
46 if crushExists {
47 // CRUSH.md already exists, don't show the dialog
48 return false, nil
49 }
50
51 // File doesn't exist, show the dialog
52 return true, nil
53}
54
55// crushMdExists checks if any case variation of crush.md exists in the directory
56func crushMdExists(dir string) (bool, error) {
57 entries, err := os.ReadDir(dir)
58 if err != nil {
59 return false, err
60 }
61
62 for _, entry := range entries {
63 if entry.IsDir() {
64 continue
65 }
66
67 name := strings.ToLower(entry.Name())
68 if name == "crush.md" {
69 return true, nil
70 }
71 }
72
73 return false, nil
74}
75
76// MarkProjectInitialized marks the current project as initialized
77func MarkProjectInitialized() error {
78 if cfg == nil {
79 return fmt.Errorf("config not loaded")
80 }
81 // Create the flag file path
82 flagFilePath := filepath.Join(cfg.Data.Directory, InitFlagFilename)
83
84 // Create an empty file to mark the project as initialized
85 file, err := os.Create(flagFilePath)
86 if err != nil {
87 return fmt.Errorf("failed to create init flag file: %w", err)
88 }
89 defer file.Close()
90
91 return nil
92}