From cef88ba317ed2fd70f88b4a8cf210b70687dc84d Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Wed, 11 Sep 2024 00:55:09 +0100 Subject: [PATCH] checkpoint: only manage the configs lists in host config --- hosts/falcon.json | 11 +++++++++++ hosts/sparrow.json | 9 +++++++++ magefiles/common.go | 11 +++++++++++ magefiles/config.go | 6 ++++-- magefiles/files.go | 19 +++++++++++++++++-- magefiles/templates.go | 14 ++++++++++++-- 6 files changed, 64 insertions(+), 6 deletions(-) diff --git a/hosts/falcon.json b/hosts/falcon.json index 01970ee..71d6251 100644 --- a/hosts/falcon.json +++ b/hosts/falcon.json @@ -16,6 +16,17 @@ "Projects" ] }, + "applicationConfigurations": [ + "alacritty", + "amfora", + "git", + "lf", + "logrotate", + "tmux", + "user-dirs.dirs", + "user-dirs.locale", + "zk" + ], "git": { "gpgSign": true, "user": { diff --git a/hosts/sparrow.json b/hosts/sparrow.json index a51deb5..a44fede 100644 --- a/hosts/sparrow.json +++ b/hosts/sparrow.json @@ -4,6 +4,15 @@ "includeXDGDirectories": true, "additionalDirectories": [], }, + "applicationConfigurations": [ + "git", + "lf", + "logrotate", + "river", + "tmux", + "user-dirs.dirs", + "user-dirs.locale" + ], "git": { "gpgSign": false, "user": { diff --git a/magefiles/common.go b/magefiles/common.go index 90208c3..efd933d 100644 --- a/magefiles/common.go +++ b/magefiles/common.go @@ -8,6 +8,7 @@ import ( "io/fs" "os" "path/filepath" + "slices" ) const ( @@ -119,3 +120,13 @@ func ensureSymlink(source, dest string) error { return nil } + +func applicationConfigurationSet(applicationConfigurationList []string) map[string]struct{} { + set := make(map[string]struct{}) + + for _, app := range slices.All(applicationConfigurationList) { + set[app] = struct{}{} + } + + return set +} diff --git a/magefiles/config.go b/magefiles/config.go index 178c355..b487c80 100644 --- a/magefiles/config.go +++ b/magefiles/config.go @@ -11,8 +11,9 @@ import ( ) type config struct { - Directories configDirectories `json:"directories"` - Git configGit `json:"git"` + Directories configDirectories `json:"directories"` + ApplicationConfigurations []string `json:"applicationConfigurations"` + Git configGit `json:"git"` } type configDirectories struct { @@ -85,5 +86,6 @@ func defaultConfig() config { SigningKey: "", }, }, + ApplicationConfigurations: []string{}, } } diff --git a/magefiles/files.go b/magefiles/files.go index 7859d64..7ef6a91 100644 --- a/magefiles/files.go +++ b/magefiles/files.go @@ -22,14 +22,21 @@ func Files() error { return fmt.Errorf("unable to get the user's home configuration directory: %w", err) } - if err = filepath.WalkDir(rootFilesDir, manageFilesFunc(homeConfigDirectory)); err != nil { + config, err := newConfig() + if err != nil { + return fmt.Errorf("unable to load the configuration: %w", err) + } + + appConfigSet := applicationConfigurationSet(config.ApplicationConfigurations) + + if err = filepath.WalkDir(rootFilesDir, manageFilesFunc(homeConfigDirectory, appConfigSet)); err != nil { return fmt.Errorf("received an error while processing the files: %w", err) } return nil } -func manageFilesFunc(homeConfigDirectory string) fs.WalkDirFunc { +func manageFilesFunc(homeConfigDirectory string, appConfigSet map[string]struct{}) fs.WalkDirFunc { return func(path string, d fs.DirEntry, err error) error { if err != nil { return err @@ -41,6 +48,12 @@ func manageFilesFunc(homeConfigDirectory string) fs.WalkDirFunc { relativePath := strings.TrimPrefix(path, rootFilesDir+"/") + appConfigName := strings.SplitN(relativePath, "/", 2)[0] + + if _, exists := appConfigSet[appConfigName]; !exists { + return nil + } + managedPath := filepath.Join(rootManagedDir, relativePath) configPath := filepath.Join(homeConfigDirectory, relativePath) @@ -56,6 +69,8 @@ func manageFilesFunc(homeConfigDirectory string) fs.WalkDirFunc { return nil } + fmt.Println("Processing file:", relativePath) + if err := sh.Copy(managedPath, path); err != nil { return fmt.Errorf("unable to copy %s to %s: %w", path, managedPath, err) } diff --git a/magefiles/templates.go b/magefiles/templates.go index e243c6f..521bc68 100644 --- a/magefiles/templates.go +++ b/magefiles/templates.go @@ -28,14 +28,16 @@ func Templates() error { return fmt.Errorf("unable to load the configuration: %w", err) } - if err = filepath.WalkDir(rootTemplateDir, manageTemplatesFunc(homeConfigDirectory, config)); err != nil { + appConfigSet := applicationConfigurationSet(config.ApplicationConfigurations) + + if err = filepath.WalkDir(rootTemplateDir, manageTemplatesFunc(homeConfigDirectory, config, appConfigSet)); err != nil { return fmt.Errorf("received an error while processing the templates: %w", err) } return nil } -func manageTemplatesFunc(homeConfigDirectory string, config config) fs.WalkDirFunc { +func manageTemplatesFunc(homeConfigDirectory string, config config, appConfigSet map[string]struct{}) fs.WalkDirFunc { funcMap := template.FuncMap{ "env": env, } @@ -51,6 +53,12 @@ func manageTemplatesFunc(homeConfigDirectory string, config config) fs.WalkDirFu relativePath := strings.TrimPrefix(templatePath, rootTemplateDir+"/") + appConfigName := strings.SplitN(relativePath, "/", 2)[0] + + if _, exists := appConfigSet[appConfigName]; !exists { + return nil + } + if d.IsDir() { managedDir := filepath.Join(rootManagedDir, relativePath) configDir := filepath.Join(homeConfigDirectory, relativePath) @@ -77,6 +85,8 @@ func manageTemplatesFunc(homeConfigDirectory string, config config) fs.WalkDirFu managedPath := filepath.Join(rootManagedDir, strings.TrimSuffix(relativePath, templateExtension)) configPath := filepath.Join(homeConfigDirectory, strings.TrimSuffix(relativePath, templateExtension)) + fmt.Println("Processing template:", relativePath) + if err := renderTemplate(config, templatePath, managedPath, funcMap); err != nil { return fmt.Errorf( "unable to generate %s from template %s: %w",