mirror of
https://github.com/ddworken/hishtory.git
synced 2025-01-29 17:51:21 +01:00
* Add ability to skip config modifications * Update golden names to fork on OS * Remove incorrect newline in golden
This commit is contained in:
parent
0df63aa7c0
commit
c10afa40c3
@ -3201,4 +3201,29 @@ func TestChangeSyncingStatus(t *testing.T) {
|
||||
)
|
||||
}
|
||||
|
||||
func TestInstallSkipConfigModification(t *testing.T) {
|
||||
markTestForSharding(t, 14)
|
||||
defer testutils.BackupAndRestore(t)()
|
||||
tester := zshTester{}
|
||||
|
||||
// Install and check that it gave info on how to configure the shell
|
||||
out := tester.RunInteractiveShell(t, ` /tmp/client install --skip-config-modification | grep -v "secret hishtory key"`)
|
||||
testutils.CompareGoldens(t, out, "TestInstallSkipConfigModification-InstallOutput-"+runtime.GOOS)
|
||||
|
||||
// Check that the shell config files weren't configured
|
||||
homedir, err := os.UserHomeDir()
|
||||
require.NoError(t, err)
|
||||
shellConfigFiles := []string{
|
||||
path.Join(homedir, ".zshrc"),
|
||||
path.Join(homedir, ".bashrc"),
|
||||
path.Join(homedir, ".bash_profile"),
|
||||
path.Join(homedir, ".config/fish/config.fish"),
|
||||
}
|
||||
for _, file := range shellConfigFiles {
|
||||
fileContents, err := os.ReadFile(file)
|
||||
require.NoError(t, err)
|
||||
require.NotContains(t, fileContents, "hishtory")
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: somehow test/confirm that hishtory works even if only bash/only zsh is installed
|
||||
|
@ -26,6 +26,7 @@ import (
|
||||
var offlineInit *bool
|
||||
var forceInit *bool
|
||||
var offlineInstall *bool
|
||||
var skipConfigModification *bool
|
||||
|
||||
var installCmd = &cobra.Command{
|
||||
Use: "install",
|
||||
@ -37,7 +38,7 @@ var installCmd = &cobra.Command{
|
||||
if len(args) > 0 {
|
||||
secretKey = args[0]
|
||||
}
|
||||
lib.CheckFatalError(install(secretKey, *offlineInstall))
|
||||
lib.CheckFatalError(install(secretKey, *offlineInstall, *skipConfigModification))
|
||||
if os.Getenv("HISHTORY_SKIP_INIT_IMPORT") == "" {
|
||||
db, err := hctx.OpenLocalSqliteDb()
|
||||
lib.CheckFatalError(err)
|
||||
@ -145,7 +146,7 @@ func warnIfUnsupportedBashVersion() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func install(secretKey string, offline bool) error {
|
||||
func install(secretKey string, offline, skipConfigModification bool) error {
|
||||
homedir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get user's home directory: %w", err)
|
||||
@ -158,15 +159,15 @@ func install(secretKey string, offline bool) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = configureBashrc(homedir, path)
|
||||
err = configureBashrc(homedir, path, skipConfigModification)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = configureZshrc(homedir, path)
|
||||
err = configureZshrc(homedir, path, skipConfigModification)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = configureFish(homedir, path)
|
||||
err = configureFish(homedir, path, skipConfigModification)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -254,7 +255,7 @@ func getFishConfigPath(homedir string) string {
|
||||
return path.Join(homedir, data.GetHishtoryPath(), "config.fish")
|
||||
}
|
||||
|
||||
func configureFish(homedir, binaryPath string) error {
|
||||
func configureFish(homedir, binaryPath string, skipConfigModification bool) error {
|
||||
// Check if fish is installed
|
||||
_, err := exec.LookPath("fish")
|
||||
if err != nil {
|
||||
@ -282,11 +283,15 @@ func configureFish(homedir, binaryPath string) error {
|
||||
return nil
|
||||
}
|
||||
// Add to fishrc
|
||||
if _, err := exec.LookPath("fish"); err != nil && skipConfigModification {
|
||||
// fish is not installed, so avoid prompting the user to configure fish
|
||||
return nil
|
||||
}
|
||||
err = os.MkdirAll(path.Join(homedir, ".config/fish"), 0o744)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create fish config directory: %w", err)
|
||||
}
|
||||
return addToShellConfig(path.Join(homedir, ".config/fish/config.fish"), getFishConfigFragment(homedir))
|
||||
return addToShellConfig(path.Join(homedir, ".config/fish/config.fish"), getFishConfigFragment(homedir), skipConfigModification)
|
||||
}
|
||||
|
||||
func getFishConfigFragment(homedir string) string {
|
||||
@ -309,7 +314,7 @@ func getZshConfigPath(homedir string) string {
|
||||
return path.Join(homedir, data.GetHishtoryPath(), "config.zsh")
|
||||
}
|
||||
|
||||
func configureZshrc(homedir, binaryPath string) error {
|
||||
func configureZshrc(homedir, binaryPath string, skipConfigModification bool) error {
|
||||
// Create the file we're going to source in our zshrc. Do this no matter what in case there are updates to it.
|
||||
configContents := lib.ConfigZshContents
|
||||
if os.Getenv("HISHTORY_TEST") != "" {
|
||||
@ -332,7 +337,7 @@ func configureZshrc(homedir, binaryPath string) error {
|
||||
return nil
|
||||
}
|
||||
// Add to zshrc
|
||||
return addToShellConfig(getZshRcPath(homedir), getZshConfigFragment(homedir))
|
||||
return addToShellConfig(getZshRcPath(homedir), getZshConfigFragment(homedir), skipConfigModification)
|
||||
}
|
||||
|
||||
func getZshRcPath(homedir string) string {
|
||||
@ -362,7 +367,7 @@ func getBashConfigPath(homedir string) string {
|
||||
return path.Join(homedir, data.GetHishtoryPath(), "config.sh")
|
||||
}
|
||||
|
||||
func configureBashrc(homedir, binaryPath string) error {
|
||||
func configureBashrc(homedir, binaryPath string, skipConfigModification bool) error {
|
||||
// Create the file we're going to source in our bashrc. Do this no matter what in case there are updates to it.
|
||||
configContents := lib.ConfigShContents
|
||||
if os.Getenv("HISHTORY_TEST") != "" {
|
||||
@ -382,7 +387,7 @@ func configureBashrc(homedir, binaryPath string) error {
|
||||
return fmt.Errorf("failed to check ~/.bashrc: %w", err)
|
||||
}
|
||||
if !bashRcIsConfigured {
|
||||
err = addToShellConfig(path.Join(homedir, ".bashrc"), getBashConfigFragment(homedir))
|
||||
err = addToShellConfig(path.Join(homedir, ".bashrc"), getBashConfigFragment(homedir), skipConfigModification)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -394,7 +399,7 @@ func configureBashrc(homedir, binaryPath string) error {
|
||||
return fmt.Errorf("failed to check ~/.bash_profile: %w", err)
|
||||
}
|
||||
if !bashProfileIsConfigured {
|
||||
err = addToShellConfig(path.Join(homedir, ".bash_profile"), getBashConfigFragment(homedir))
|
||||
err = addToShellConfig(path.Join(homedir, ".bash_profile"), getBashConfigFragment(homedir), skipConfigModification)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -403,7 +408,11 @@ func configureBashrc(homedir, binaryPath string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func addToShellConfig(shellConfigPath, configFragment string) error {
|
||||
func addToShellConfig(shellConfigPath, configFragment string, skipConfigModification bool) error {
|
||||
if skipConfigModification {
|
||||
fmt.Printf("Please edit %q to add:\n\n```\n%s\n```\n\n", convertToRelativePath(shellConfigPath), strings.TrimSpace(configFragment))
|
||||
return nil
|
||||
}
|
||||
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: %w", shellConfigPath, err)
|
||||
@ -416,6 +425,17 @@ func addToShellConfig(shellConfigPath, configFragment string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func convertToRelativePath(path string) string {
|
||||
homedir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return path
|
||||
}
|
||||
if strings.HasPrefix(path, homedir) {
|
||||
return strings.Replace(path, homedir, "~", 1)
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
func getBashConfigFragment(homedir string) string {
|
||||
return "\n# Hishtory Config:\nexport PATH=\"$PATH:" + path.Join(homedir, data.GetHishtoryPath()) + "\"\nsource " + getBashConfigPath(homedir) + "\n"
|
||||
}
|
||||
@ -642,5 +662,6 @@ func init() {
|
||||
|
||||
offlineInit = initCmd.Flags().Bool("offline", false, "Install hiSHtory in offline mode wiht all syncing capabilities disabled")
|
||||
forceInit = initCmd.Flags().Bool("force", false, "Force re-init without any prompts")
|
||||
offlineInstall = installCmd.Flags().Bool("offline", false, "Install hiSHtory in offline mode wiht all syncing capabilities disabled")
|
||||
offlineInstall = installCmd.Flags().Bool("offline", false, "Install hiSHtory in offline mode with all syncing capabilities disabled")
|
||||
skipConfigModification = installCmd.Flags().Bool("skip-config-modification", false, "Skip modifying shell configs and instead instruct the user on how to modify their configs")
|
||||
}
|
||||
|
24
client/testdata/TestInstallSkipConfigModification-InstallOutput-darwin
vendored
Normal file
24
client/testdata/TestInstallSkipConfigModification-InstallOutput-darwin
vendored
Normal file
@ -0,0 +1,24 @@
|
||||
Please edit "~/.bashrc" to add:
|
||||
|
||||
```
|
||||
# Hishtory Config:
|
||||
export PATH="$PATH:/Users/runner/.hishtory"
|
||||
source /Users/runner/.hishtory/config.sh
|
||||
```
|
||||
|
||||
Please edit "~/.bash_profile" to add:
|
||||
|
||||
```
|
||||
# Hishtory Config:
|
||||
export PATH="$PATH:/Users/runner/.hishtory"
|
||||
source /Users/runner/.hishtory/config.sh
|
||||
```
|
||||
|
||||
Please edit "~/.zshrc" to add:
|
||||
|
||||
```
|
||||
# Hishtory Config:
|
||||
export PATH="$PATH:/Users/runner/.hishtory"
|
||||
source /Users/runner/.hishtory/config.zsh
|
||||
```
|
||||
|
24
client/testdata/TestInstallSkipConfigModification-InstallOutput-linux
vendored
Normal file
24
client/testdata/TestInstallSkipConfigModification-InstallOutput-linux
vendored
Normal file
@ -0,0 +1,24 @@
|
||||
Please edit "~/.bashrc" to add:
|
||||
|
||||
```
|
||||
# Hishtory Config:
|
||||
export PATH="$PATH:/home/runner/.hishtory"
|
||||
source /home/runner/.hishtory/config.sh
|
||||
```
|
||||
|
||||
Please edit "~/.bash_profile" to add:
|
||||
|
||||
```
|
||||
# Hishtory Config:
|
||||
export PATH="$PATH:/home/runner/.hishtory"
|
||||
source /home/runner/.hishtory/config.sh
|
||||
```
|
||||
|
||||
Please edit "~/.zshrc" to add:
|
||||
|
||||
```
|
||||
# Hishtory Config:
|
||||
export PATH="$PATH:/home/runner/.hishtory"
|
||||
source /home/runner/.hishtory/config.zsh
|
||||
```
|
||||
|
Loading…
Reference in New Issue
Block a user