2024-09-12 16:35:06 +01:00
|
|
|
//go:build mage
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2024-09-14 05:58:43 +01:00
|
|
|
|
|
|
|
"codeflow.dananglin.me.uk/linux-home/manager/magefiles/internal/config"
|
|
|
|
"codeflow.dananglin.me.uk/linux-home/manager/magefiles/internal/utilities"
|
2024-09-12 16:35:06 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2024-09-14 05:58:43 +01:00
|
|
|
cfg, err := config.NewConfig()
|
2024-09-12 16:35:06 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to load the configuration: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-09-14 05:58:43 +01:00
|
|
|
if !cfg.BashProfile.Manage {
|
2024-09-12 16:35:06 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-09-14 05:58:43 +01:00
|
|
|
if err := utilities.RenderTemplate(cfg, bashProfileTemplateFile, managedBashProfile); err != nil {
|
2024-09-12 16:35:06 +01:00
|
|
|
return fmt.Errorf("unable to generate the Bash Profile: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-09-14 05:58:43 +01:00
|
|
|
filename := cfg.BashProfile.Filename
|
2024-09-12 16:35:06 +01:00
|
|
|
if filename == "" {
|
|
|
|
filename = defaultFilename
|
|
|
|
}
|
|
|
|
|
|
|
|
symlinkPath := filepath.Join(homeDirectory, filename)
|
|
|
|
|
2024-09-14 05:58:43 +01:00
|
|
|
if err := utilities.EnsureSymlink(managedBashProfile, symlinkPath); err != nil {
|
2024-09-12 16:35:06 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|