manager/magefiles/bash_profile.go

55 lines
1.2 KiB
Bash
Raw Normal View History

//go:build mage
package main
import (
"fmt"
"os"
"path/filepath"
"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"
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)
}
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)
}
filename := config.BashProfile.Filename
if filename == "" {
filename = defaultFilename
}
symlinkPath := filepath.Join(homeDirectory, filename)
if err := ensureSymlink(managedBashProfile, symlinkPath); err != nil {
return err
}
return nil
}