//go:build mage package main import ( "fmt" "os" "path/filepath" "strings" "codeflow.dananglin.me.uk/linux-home/manager/magefiles/internal/config" "codeflow.dananglin.me.uk/linux-home/manager/magefiles/internal/utilities" "codeflow.dananglin.me.uk/linux-home/manager/magefiles/internal/walk" ) // Files ensure that the configuration files in the managed directory is up to date and // ensures that they are symlinked correctly to the files in the user's home configuration // directory. func Files() error { const rootFilesDir string = "files" homeConfigDir, err := os.UserConfigDir() if err != nil { return fmt.Errorf("unable to get the user's home configuration directory: %w", err) } cfg, err := config.NewConfig() if err != nil { return fmt.Errorf("unable to load the configuration: %w", err) } managedConfig := utilities.ManagedConfigSet(cfg.ManagedConfigurations) validationFunc := func(relativePath string) bool { split := strings.SplitN(relativePath, "/", 2) if len(split) < 1 { return false } appConfigName := split[0] _, exists := managedConfig[appConfigName] return exists } if err = filepath.WalkDir( rootFilesDir, walk.CopyFiles(homeConfigDir, rootFilesDir, rootManagedDir, validationFunc), ); err != nil { return fmt.Errorf("received an error while copying the files: %w", err) } return nil }