2024-09-10 11:32:13 +01:00
|
|
|
//go:build mage
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/fs"
|
|
|
|
"os"
|
2024-09-10 13:51:37 +01:00
|
|
|
"path/filepath"
|
2024-09-11 00:55:09 +01:00
|
|
|
"slices"
|
2024-09-10 11:32:13 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
dirModePerm fs.FileMode = 0o700
|
|
|
|
|
2024-09-10 14:21:03 +01:00
|
|
|
configDir string = "hosts"
|
2024-09-10 11:32:13 +01:00
|
|
|
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
|
|
|
|
}
|
2024-09-10 13:51:37 +01:00
|
|
|
|
|
|
|
func ensureSymlink(source, dest string) error {
|
|
|
|
absolutePathErrorMessageFormat := "unable to get the absolute path to %s: %w"
|
|
|
|
|
|
|
|
absoluteSourcePath, err := filepath.Abs(source)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(absolutePathErrorMessageFormat, source, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
absoluteDestPath, err := filepath.Abs(dest)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(absolutePathErrorMessageFormat, dest, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
destInfo, err := os.Lstat(absoluteDestPath)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
|
|
fmt.Printf("Linking %s to %s\n", absoluteDestPath, absoluteSourcePath)
|
|
|
|
|
|
|
|
if err := os.Symlink(absoluteSourcePath, absoluteDestPath); err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"unable to symlink %s to %s: %w",
|
|
|
|
absoluteDestPath,
|
|
|
|
absoluteSourcePath,
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("unable to get the file info for %s: %w", absoluteDestPath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if destInfo.Mode().Type() != fs.ModeSymlink {
|
|
|
|
return fmt.Errorf("the path %s exists but it is not a symlink", absoluteDestPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
destLinksTo, err := filepath.EvalSymlinks(absoluteDestPath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to evaluate the symlink %s: %w", absoluteDestPath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if destLinksTo == absoluteSourcePath {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf(
|
|
|
|
"%s should link back to %s but instead links back to %s\n",
|
|
|
|
absoluteDestPath,
|
|
|
|
absoluteSourcePath,
|
|
|
|
destLinksTo,
|
|
|
|
)
|
|
|
|
|
|
|
|
fmt.Println("Recreating:", absoluteDestPath)
|
|
|
|
|
|
|
|
if err := os.Remove(absoluteDestPath); err != nil {
|
|
|
|
return fmt.Errorf("unable to remove %s: %w", absoluteDestPath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.Symlink(absoluteSourcePath, absoluteDestPath); err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"unable to symlink %s to %s: %w",
|
|
|
|
absoluteDestPath,
|
|
|
|
absoluteSourcePath,
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-09-11 00:55:09 +01:00
|
|
|
|
2024-09-12 01:05:33 +01:00
|
|
|
func managedConfigSet(applicationConfigurationList []string) map[string]struct{} {
|
2024-09-11 00:55:09 +01:00
|
|
|
set := make(map[string]struct{})
|
|
|
|
|
|
|
|
for _, app := range slices.All(applicationConfigurationList) {
|
|
|
|
set[app] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return set
|
|
|
|
}
|