2024-09-11 07:54:13 +01:00
|
|
|
//go:build mage
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-09-12 03:37:00 +01:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2024-09-11 07:54:13 +01:00
|
|
|
"text/template"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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"
|
2024-09-12 03:37:00 +01:00
|
|
|
defaultFilename string = ".bash_profile"
|
2024-09-11 07:54:13 +01:00
|
|
|
)
|
|
|
|
|
2024-09-12 03:37:00 +01:00
|
|
|
homeDirectory, err := os.UserHomeDir()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to get the user's home configuration directory: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-09-11 07:54:13 +01:00
|
|
|
config, err := newConfig()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to load the configuration: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !config.BashProfile.Manage {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
funcMap := template.FuncMap{
|
|
|
|
"env": env,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := renderTemplate(config, bashProfileTemplateFile, managedBashProfile, funcMap); err != nil {
|
|
|
|
return fmt.Errorf("unable to generate the Bash Profile: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-09-12 03:37:00 +01:00
|
|
|
filename := config.BashProfile.Filename
|
|
|
|
if filename == "" {
|
|
|
|
filename = defaultFilename
|
|
|
|
}
|
|
|
|
|
|
|
|
symlinkPath := filepath.Join(homeDirectory, filename)
|
|
|
|
|
|
|
|
if err := ensureSymlink(managedBashProfile, symlinkPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-09-11 07:54:13 +01:00
|
|
|
return nil
|
|
|
|
}
|