checkpoint: copy source files to managed dir
This commit is contained in:
parent
6d24e187fe
commit
201fce8747
7 changed files with 127 additions and 45 deletions
49
magefiles/common.go
Normal file
49
magefiles/common.go
Normal file
|
@ -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
|
||||||
|
}
|
|
@ -1,8 +0,0 @@
|
||||||
//go:build mage
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
const (
|
|
||||||
configDir string = "./config"
|
|
||||||
rootManagedDir string = "./managed"
|
|
||||||
)
|
|
|
@ -3,19 +3,13 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/fs"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"slices"
|
"slices"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
// Directories ensure that the specified home directories are present.
|
||||||
dirModePerm fs.FileMode = 0o700
|
|
||||||
)
|
|
||||||
|
|
||||||
// Directories ensure that the directories specified in the configuration is present within the home directory.
|
|
||||||
func Directories() error {
|
func Directories() error {
|
||||||
config, err := newConfig()
|
config, err := newConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -54,36 +48,6 @@ func Directories() error {
|
||||||
return nil
|
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 {
|
func defaultDirectories() []string {
|
||||||
return []string{
|
return []string{
|
||||||
".local",
|
".local",
|
||||||
|
|
73
magefiles/files.go
Normal file
73
magefiles/files.go
Normal file
|
@ -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
|
||||||
|
}
|
|
@ -1,3 +1,5 @@
|
||||||
module codeflow.dananglin.me.uk/linux-home/manager/magefiles
|
module codeflow.dananglin.me.uk/linux-home/manager/magefiles
|
||||||
|
|
||||||
go 1.23.1
|
go 1.23.1
|
||||||
|
|
||||||
|
require github.com/magefile/mage v1.15.0
|
||||||
|
|
2
magefiles/go.sum
Normal file
2
magefiles/go.sum
Normal file
|
@ -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=
|
Loading…
Reference in a new issue