mirror of
https://github.com/ddworken/hishtory.git
synced 2024-11-22 16:24:00 +01:00
Write the config fragment to the bash_profile for MacOs to fix #14
This commit is contained in:
parent
a4d6232d8a
commit
c1d17842c4
@ -1,5 +1,10 @@
|
||||
# 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/
|
||||
function __hishtory_precommand() {
|
||||
if [ -z "$HISHTORY_AT_PROMPT" ]; then
|
||||
|
@ -730,16 +730,7 @@ func configureFish(homedir, binaryPath string) error {
|
||||
if err != nil {
|
||||
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)
|
||||
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
|
||||
return addToShellConfig(path.Join(homedir, ".config/fish/config.fish"), getFishConfigFragment(homedir))
|
||||
}
|
||||
|
||||
func getFishConfigFragment(homedir string) string {
|
||||
@ -785,16 +776,7 @@ func configureZshrc(homedir, binaryPath string) error {
|
||||
return nil
|
||||
}
|
||||
// Add to zshrc
|
||||
f, err := os.OpenFile(path.Join(homedir, ".zshrc"), os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0o644)
|
||||
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
|
||||
return addToShellConfig(path.Join(homedir, ".zshrc"), getZshConfigFragment(homedir))
|
||||
}
|
||||
|
||||
func getZshConfigFragment(homedir string) string {
|
||||
@ -831,23 +813,40 @@ func configureBashrc(homedir, binaryPath string) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to write config.sh file: %v", err)
|
||||
}
|
||||
// Check if we need to configure the bashrc
|
||||
bashIsConfigured, err := isBashConfigured(homedir)
|
||||
// Check if we need to configure the bashrc and configure it if so
|
||||
bashRcIsConfigured, err := isBashRcConfigured(homedir)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to check ~/.bashrc: %v", err)
|
||||
}
|
||||
if bashIsConfigured {
|
||||
return nil
|
||||
if !bashRcIsConfigured {
|
||||
err = addToShellConfig(path.Join(homedir, ".bashrc"), getBashConfigFragment(homedir))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// Add to bashrc
|
||||
f, err := os.OpenFile(path.Join(homedir, ".bashrc"), os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0o644)
|
||||
// Check if we need to configure the bash_profile and configure it if so
|
||||
bashProfileIsConfigured, err := isBashProfileConfigured(homedir)
|
||||
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()
|
||||
_, err = f.WriteString(getBashConfigFragment(homedir))
|
||||
_, err = f.WriteString(configFragment)
|
||||
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
|
||||
}
|
||||
@ -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"
|
||||
}
|
||||
|
||||
func isBashConfigured(homedir string) (bool, error) {
|
||||
func isBashRcConfigured(homedir string) (bool, error) {
|
||||
_, err := os.Stat(path.Join(homedir, ".bashrc"))
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return false, nil
|
||||
@ -868,6 +867,22 @@ func isBashConfigured(homedir string) (bool, error) {
|
||||
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) {
|
||||
clientPath, err := exec.LookPath("hishtory")
|
||||
if err != nil {
|
||||
|
@ -70,6 +70,7 @@ func BackupAndRestoreWithId(t *testing.T, id string) func() {
|
||||
copyFiles := []string{
|
||||
path.Join(homedir, ".zshrc"),
|
||||
path.Join(homedir, ".bashrc"),
|
||||
path.Join(homedir, ".bash_profile"),
|
||||
}
|
||||
for _, file := range copyFiles {
|
||||
touchFile(file)
|
||||
|
Loading…
Reference in New Issue
Block a user