Write the config fragment to the bash_profile for MacOs to fix #14

This commit is contained in:
David Dworken 2022-11-03 21:01:57 -07:00
parent a4d6232d8a
commit c1d17842c4
No known key found for this signature in database
3 changed files with 51 additions and 30 deletions

View File

@ -1,5 +1,10 @@
# This script should be sourced inside of .bashrc to integrate bash with hishtory # This script should be sourced inside of .bashrc to integrate bash with hishtory
# Include guard. This file is sourced in multiple places, but we want it to only execute once.
# This trick is from https://stackoverflow.com/questions/7518584/is-there-any-mechanism-in-shell-script-alike-include-guard-in-c
if [ -n "$__hishtory_bash_config_sourced" ]; then return; fi
__hishtory_bash_config_sourced=`date`
# Implementation of running before/after every command based on https://jichu4n.com/posts/debug-trap-and-prompt_command-in-bash/ # Implementation of running before/after every command based on https://jichu4n.com/posts/debug-trap-and-prompt_command-in-bash/
function __hishtory_precommand() { function __hishtory_precommand() {
if [ -z "$HISHTORY_AT_PROMPT" ]; then if [ -z "$HISHTORY_AT_PROMPT" ]; then

View File

@ -730,16 +730,7 @@ func configureFish(homedir, binaryPath string) error {
if err != nil { if err != nil {
return fmt.Errorf("failed to create fish config directory: %v", err) return fmt.Errorf("failed to create fish config directory: %v", err)
} }
f, err := os.OpenFile(path.Join(homedir, ".config/fish/config.fish"), os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0o644) return addToShellConfig(path.Join(homedir, ".config/fish/config.fish"), getFishConfigFragment(homedir))
if err != nil {
return fmt.Errorf("failed to append to ~/.config/fish/config.fish: %v", err)
}
defer f.Close()
_, err = f.WriteString(getFishConfigFragment(homedir))
if err != nil {
return fmt.Errorf("failed to append to zshrc: %v", err)
}
return nil
} }
func getFishConfigFragment(homedir string) string { func getFishConfigFragment(homedir string) string {
@ -785,16 +776,7 @@ func configureZshrc(homedir, binaryPath string) error {
return nil return nil
} }
// Add to zshrc // Add to zshrc
f, err := os.OpenFile(path.Join(homedir, ".zshrc"), os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0o644) return addToShellConfig(path.Join(homedir, ".zshrc"), getZshConfigFragment(homedir))
if err != nil {
return fmt.Errorf("failed to append to zshrc: %v", err)
}
defer f.Close()
_, err = f.WriteString(getZshConfigFragment(homedir))
if err != nil {
return fmt.Errorf("failed to append to zshrc: %v", err)
}
return nil
} }
func getZshConfigFragment(homedir string) string { func getZshConfigFragment(homedir string) string {
@ -831,23 +813,40 @@ func configureBashrc(homedir, binaryPath string) error {
if err != nil { if err != nil {
return fmt.Errorf("failed to write config.sh file: %v", err) return fmt.Errorf("failed to write config.sh file: %v", err)
} }
// Check if we need to configure the bashrc // Check if we need to configure the bashrc and configure it if so
bashIsConfigured, err := isBashConfigured(homedir) bashRcIsConfigured, err := isBashRcConfigured(homedir)
if err != nil { if err != nil {
return fmt.Errorf("failed to check ~/.bashrc: %v", err) return fmt.Errorf("failed to check ~/.bashrc: %v", err)
} }
if bashIsConfigured { if !bashRcIsConfigured {
return nil err = addToShellConfig(path.Join(homedir, ".bashrc"), getBashConfigFragment(homedir))
if err != nil {
return err
}
} }
// Add to bashrc // Check if we need to configure the bash_profile and configure it if so
f, err := os.OpenFile(path.Join(homedir, ".bashrc"), os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0o644) bashProfileIsConfigured, err := isBashProfileConfigured(homedir)
if err != nil { if err != nil {
return fmt.Errorf("failed to append to bashrc: %v", err) return fmt.Errorf("failed to check ~/.bash_profile: %v", err)
}
if !bashProfileIsConfigured {
err = addToShellConfig(path.Join(homedir, ".bash_profile"), getBashConfigFragment(homedir))
if err != nil {
return err
}
}
return nil
}
func addToShellConfig(shellConfigPath, configFragment string) error {
f, err := os.OpenFile(shellConfigPath, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0o644)
if err != nil {
return fmt.Errorf("failed to append to %s: %v", shellConfigPath, err)
} }
defer f.Close() defer f.Close()
_, err = f.WriteString(getBashConfigFragment(homedir)) _, err = f.WriteString(configFragment)
if err != nil { if err != nil {
return fmt.Errorf("failed to append to bashrc: %v", err) return fmt.Errorf("failed to append to %s: %v", shellConfigPath, err)
} }
return nil return nil
} }
@ -856,7 +855,7 @@ func getBashConfigFragment(homedir string) string {
return "\n# Hishtory Config:\nexport PATH=\"$PATH:" + path.Join(homedir, data.HISHTORY_PATH) + "\"\nsource " + getBashConfigPath(homedir) + "\n" return "\n# Hishtory Config:\nexport PATH=\"$PATH:" + path.Join(homedir, data.HISHTORY_PATH) + "\"\nsource " + getBashConfigPath(homedir) + "\n"
} }
func isBashConfigured(homedir string) (bool, error) { func isBashRcConfigured(homedir string) (bool, error) {
_, err := os.Stat(path.Join(homedir, ".bashrc")) _, err := os.Stat(path.Join(homedir, ".bashrc"))
if errors.Is(err, os.ErrNotExist) { if errors.Is(err, os.ErrNotExist) {
return false, nil return false, nil
@ -868,6 +867,22 @@ func isBashConfigured(homedir string) (bool, error) {
return strings.Contains(string(bashrc), "# Hishtory Config:"), nil return strings.Contains(string(bashrc), "# Hishtory Config:"), nil
} }
func isBashProfileConfigured(homedir string) (bool, error) {
if runtime.GOOS != "darwin" {
// We only need to configure the bash_profile for macos. See https://github.com/ddworken/hishtory/issues/14
return true, nil
}
_, err := os.Stat(path.Join(homedir, ".bash_profile"))
if errors.Is(err, os.ErrNotExist) {
return false, nil
}
bashrc, err := ioutil.ReadFile(path.Join(homedir, ".bash_profile"))
if err != nil {
return false, fmt.Errorf("failed to read bash_profile: %v", err)
}
return strings.Contains(string(bashrc), "# Hishtory Config:"), nil
}
func installBinary(homedir string) (string, error) { func installBinary(homedir string) (string, error) {
clientPath, err := exec.LookPath("hishtory") clientPath, err := exec.LookPath("hishtory")
if err != nil { if err != nil {

View File

@ -70,6 +70,7 @@ func BackupAndRestoreWithId(t *testing.T, id string) func() {
copyFiles := []string{ copyFiles := []string{
path.Join(homedir, ".zshrc"), path.Join(homedir, ".zshrc"),
path.Join(homedir, ".bashrc"), path.Join(homedir, ".bashrc"),
path.Join(homedir, ".bash_profile"),
} }
for _, file := range copyFiles { for _, file := range copyFiles {
touchFile(file) touchFile(file)