Files
Ayman BagabasandGitHub 5ff8d68760 refactor(config): introduce ConfigStore and Scope for better config m… (#2395)
* refactor(config): introduce ConfigStore and Scope for better config management

This makes config.Config immutable and introduces a ConfigStore that
manages the config and provides helper methods for accessing config
values with proper scoping (global, workspace). This allows us to avoid
passing around mutable config objects and ensures that all parts of the
code are accessing the most up-to-date config values. It also lays the
groundwork for future features like per-workspace config overrides.

* fixt: lint
2026-03-12 03:12:02 +03:00

128 lines
2.8 KiB
Go

package config
import (
"fmt"
"os"
"path/filepath"
"slices"
"strings"
"github.com/charmbracelet/crush/internal/fsext"
)
const (
InitFlagFilename = "init"
)
type ProjectInitFlag struct {
Initialized bool `json:"initialized"`
}
func Init(workingDir, dataDir string, debug bool) (*ConfigStore, error) {
store, err := Load(workingDir, dataDir, debug)
if err != nil {
return nil, err
}
return store, nil
}
func ProjectNeedsInitialization(store *ConfigStore) (bool, error) {
if store == nil {
return false, fmt.Errorf("config not loaded")
}
cfg := store.Config()
flagFilePath := filepath.Join(cfg.Options.DataDirectory, InitFlagFilename)
_, err := os.Stat(flagFilePath)
if err == nil {
return false, nil
}
if !os.IsNotExist(err) {
return false, fmt.Errorf("failed to check init flag file: %w", err)
}
someContextFileExists, err := contextPathsExist(store.WorkingDir())
if err != nil {
return false, fmt.Errorf("failed to check for context files: %w", err)
}
if someContextFileExists {
return false, nil
}
// If the working directory has no non-ignored files, skip initialization step
empty, err := dirHasNoVisibleFiles(store.WorkingDir())
if err != nil {
return false, fmt.Errorf("failed to check if directory is empty: %w", err)
}
if empty {
return false, nil
}
return true, nil
}
func contextPathsExist(dir string) (bool, error) {
entries, err := os.ReadDir(dir)
if err != nil {
return false, err
}
// Create a slice of lowercase filenames for lookup with slices.Contains
var files []string
for _, entry := range entries {
if !entry.IsDir() {
files = append(files, strings.ToLower(entry.Name()))
}
}
// Check if any of the default context paths exist in the directory
for _, path := range defaultContextPaths {
// Extract just the filename from the path
_, filename := filepath.Split(path)
filename = strings.ToLower(filename)
if slices.Contains(files, filename) {
return true, nil
}
}
return false, nil
}
// dirHasNoVisibleFiles returns true if the directory has no files/dirs after applying ignore rules.
func dirHasNoVisibleFiles(dir string) (bool, error) {
files, _, err := fsext.ListDirectory(dir, nil, 1, 1)
if err != nil {
return false, err
}
return len(files) == 0, nil
}
func MarkProjectInitialized(store *ConfigStore) error {
if store == nil {
return fmt.Errorf("config not loaded")
}
flagFilePath := filepath.Join(store.Config().Options.DataDirectory, InitFlagFilename)
file, err := os.Create(flagFilePath)
if err != nil {
return fmt.Errorf("failed to create init flag file: %w", err)
}
defer file.Close()
return nil
}
func HasInitialDataConfig(store *ConfigStore) bool {
if store == nil {
return false
}
cfgPath := GlobalConfigData()
if _, err := os.Stat(cfgPath); err != nil {
return false
}
return store.Config().IsConfigured()
}