manager/magefiles/files.go
Dan Anglin a909803b29
refactor: reorganise magefiles
Reorganise and refactor the magefiles to make it more manageable and
reduce duplication.
2024-09-14 05:58:43 +01:00

57 lines
1.3 KiB
Go

//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
}