init.go

 1package config
 2
 3import (
 4	"fmt"
 5	"os"
 6	"path/filepath"
 7)
 8
 9const (
10	// InitFlagFilename is the name of the file that indicates whether the project has been initialized
11	InitFlagFilename = "init"
12)
13
14// ProjectInitFlag represents the initialization status for a project directory
15type ProjectInitFlag struct {
16	Initialized bool `json:"initialized"`
17}
18
19// ShouldShowInitDialog checks if the initialization dialog should be shown for the current directory
20func ShouldShowInitDialog() (bool, error) {
21	if cfg == nil {
22		return false, fmt.Errorf("config not loaded")
23	}
24
25	// Create the flag file path
26	flagFilePath := filepath.Join(cfg.Data.Directory, InitFlagFilename)
27
28	// Check if the flag file exists
29	_, err := os.Stat(flagFilePath)
30	if err == nil {
31		// File exists, don't show the dialog
32		return false, nil
33	}
34
35	// If the error is not "file not found", return the error
36	if !os.IsNotExist(err) {
37		return false, fmt.Errorf("failed to check init flag file: %w", err)
38	}
39
40	// File doesn't exist, show the dialog
41	return true, nil
42}
43
44// MarkProjectInitialized marks the current project as initialized
45func MarkProjectInitialized() error {
46	if cfg == nil {
47		return fmt.Errorf("config not loaded")
48	}
49	// Create the flag file path
50	flagFilePath := filepath.Join(cfg.Data.Directory, InitFlagFilename)
51
52	// Create an empty file to mark the project as initialized
53	file, err := os.Create(flagFilePath)
54	if err != nil {
55		return fmt.Errorf("failed to create init flag file: %w", err)
56	}
57	defer file.Close()
58
59	return nil
60}
61