manager/magefiles/bash_profile.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

52 lines
1.2 KiB
Go

//go:build mage
package main
import (
"fmt"
"os"
"path/filepath"
"codeflow.dananglin.me.uk/linux-home/manager/magefiles/internal/config"
"codeflow.dananglin.me.uk/linux-home/manager/magefiles/internal/utilities"
)
// BashProfile manages the user's Bash Profile using their configuration and the Bash Profile template.
func BashProfile() error {
const (
bashProfileTemplateFile string = "bash/profile.gotmpl"
managedBashProfile string = "managed/bash_profile"
defaultFilename string = ".bash_profile"
)
homeDirectory, err := os.UserHomeDir()
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)
}
if !cfg.BashProfile.Manage {
return nil
}
if err := utilities.RenderTemplate(cfg, bashProfileTemplateFile, managedBashProfile); err != nil {
return fmt.Errorf("unable to generate the Bash Profile: %w", err)
}
filename := cfg.BashProfile.Filename
if filename == "" {
filename = defaultFilename
}
symlinkPath := filepath.Join(homeDirectory, filename)
if err := utilities.EnsureSymlink(managedBashProfile, symlinkPath); err != nil {
return err
}
return nil
}