//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" ) // Templates generates the configuration files in the managed directory from the templates and // ensures that they the generated files are symlinked correctly to the files in the user's home // configuration directory. func Templates() error { const rootTemplateDir string = "templates" homeConfigDirectory, 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( rootTemplateDir, walk.RenderTemplates( cfg, homeConfigDirectory, rootTemplateDir, rootManagedDir, validationFunc, ), ); err != nil { return fmt.Errorf("received an error while rendering the templates: %w", err) } return nil }