init.go 1.5 KB

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