From 201fce87477b59c2bdef8bde1ddcdf716752538e Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Tue, 10 Sep 2024 11:32:13 +0100 Subject: [PATCH] checkpoint: copy source files to managed dir --- {unprocessed => files}/river/init | 0 magefiles/common.go | 49 +++++++++++++++++++++ magefiles/const.go | 8 ---- magefiles/directories.go | 38 +--------------- magefiles/files.go | 73 +++++++++++++++++++++++++++++++ magefiles/go.mod | 2 + magefiles/go.sum | 2 + 7 files changed, 127 insertions(+), 45 deletions(-) rename {unprocessed => files}/river/init (100%) create mode 100644 magefiles/common.go delete mode 100644 magefiles/const.go create mode 100644 magefiles/files.go create mode 100644 magefiles/go.sum diff --git a/unprocessed/river/init b/files/river/init similarity index 100% rename from unprocessed/river/init rename to files/river/init diff --git a/magefiles/common.go b/magefiles/common.go new file mode 100644 index 0000000..3ca3a9f --- /dev/null +++ b/magefiles/common.go @@ -0,0 +1,49 @@ +//go:build mage + +package main + +import ( + "errors" + "fmt" + "io/fs" + "os" +) + +const ( + dirModePerm fs.FileMode = 0o700 + + configDir string = "config" + rootManagedDir string = "managed" + rootFilesDir string = "files" + rootTemplateDir string = "templates" +) + +func ensureDirectory(path string) error { + info, err := os.Stat(path) + if err != nil { + if errors.Is(err, os.ErrNotExist) { + if err := os.Mkdir(path, dirModePerm); err != nil { + return fmt.Errorf("unable to create the directory: %w", err) + } + + return nil + } + + return fmt.Errorf( + "received an unexpected error after attempting to get the directory information: %w", + err, + ) + } + + if !info.IsDir() { + return errors.New("the path exists but it is not a directory") + } + + if info.Mode().Perm() != dirModePerm { + if err := os.Chmod(path, dirModePerm); err != nil { + return fmt.Errorf("unable to update the directory's mode to %d: %w", dirModePerm, err) + } + } + + return nil +} diff --git a/magefiles/const.go b/magefiles/const.go deleted file mode 100644 index 5d4709d..0000000 --- a/magefiles/const.go +++ /dev/null @@ -1,8 +0,0 @@ -//go:build mage - -package main - -const ( - configDir string = "./config" - rootManagedDir string = "./managed" -) diff --git a/magefiles/directories.go b/magefiles/directories.go index a7ac4d4..a2caf11 100644 --- a/magefiles/directories.go +++ b/magefiles/directories.go @@ -3,19 +3,13 @@ package main import ( - "errors" "fmt" - "io/fs" "os" "path/filepath" "slices" ) -const ( - dirModePerm fs.FileMode = 0o700 -) - -// Directories ensure that the directories specified in the configuration is present within the home directory. +// Directories ensure that the specified home directories are present. func Directories() error { config, err := newConfig() if err != nil { @@ -54,36 +48,6 @@ func Directories() error { return nil } -func ensureDirectory(path string) error { - info, err := os.Stat(path) - if err != nil { - if errors.Is(err, os.ErrNotExist) { - if err := os.Mkdir(path, dirModePerm); err != nil { - return fmt.Errorf("unable to create the directory: %w", err) - } - - return nil - } - - return fmt.Errorf( - "received an unexpected error after attempting to get the directory information: %w", - err, - ) - } - - if !info.IsDir() { - return errors.New("the path exists but it is not a directory") - } - - if info.Mode().Perm() != dirModePerm { - if err := os.Chmod(path, dirModePerm); err != nil { - return fmt.Errorf("unable to update the directory's mode to %d: %w", dirModePerm, err) - } - } - - return nil -} - func defaultDirectories() []string { return []string{ ".local", diff --git a/magefiles/files.go b/magefiles/files.go new file mode 100644 index 0000000..47776e4 --- /dev/null +++ b/magefiles/files.go @@ -0,0 +1,73 @@ +//go:build mage + +package main + +import ( + "fmt" + "io/fs" + "os" + "path/filepath" + "slices" + "strings" + + "github.com/magefile/mage/sh" +) + +// 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 { + homeConfigDirectory, err := os.UserConfigDir() + if err != nil { + return fmt.Errorf("unable to get the user's home configuration directory: %w", err) + } + + if err = filepath.WalkDir(rootFilesDir, manageFilesFunc(homeConfigDirectory)); err != nil { + return fmt.Errorf("received an error while processing the files: %w", err) + } + + return nil +} + +func manageFilesFunc(homeConfigDirectory string) fs.WalkDirFunc { + return func(path string, d fs.DirEntry, err error) error { + if err != nil { + return err + } + + if path == rootFilesDir { + return nil + } + + relativePath := strings.TrimPrefix(path, rootFilesDir+"/") + + managedPath := filepath.Join(rootManagedDir, relativePath) + configPath := filepath.Join(homeConfigDirectory, relativePath) + + if d.IsDir() { + dirs := []string{managedPath, configPath} + + for _, dir := range slices.All(dirs) { + if err := ensureDirectory(dir); err != nil { + return fmt.Errorf("unable to ensure the existence of the directory %q: %w", dir, err) + } + } + + return nil + } + + if err := sh.Copy(managedPath, path); err != nil { + return fmt.Errorf("unable to copy %s to %s: %w", path, managedPath, err) + } + + if err := ensureSymlink(managedPath, configPath); err != nil { + return err + } + + return nil + } +} + +func ensureSymlink(source, dest string) error { + return nil +} diff --git a/magefiles/go.mod b/magefiles/go.mod index 5a692b5..93056e3 100644 --- a/magefiles/go.mod +++ b/magefiles/go.mod @@ -1,3 +1,5 @@ module codeflow.dananglin.me.uk/linux-home/manager/magefiles go 1.23.1 + +require github.com/magefile/mage v1.15.0 diff --git a/magefiles/go.sum b/magefiles/go.sum new file mode 100644 index 0000000..4ee1b87 --- /dev/null +++ b/magefiles/go.sum @@ -0,0 +1,2 @@ +github.com/magefile/mage v1.15.0 h1:BvGheCMAsG3bWUDbZ8AyXXpCNwU9u5CB6sM+HNb9HYg= +github.com/magefile/mage v1.15.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=