Add ability to skip config modifications for #212 (#216)

* Add ability to skip config modifications

* Update golden names to fork on OS

* Remove incorrect newline in golden
This commit is contained in:
David Dworken 2024-06-13 21:16:17 -07:00
parent 0df63aa7c0
commit c10afa40c3
No known key found for this signature in database
4 changed files with 108 additions and 14 deletions

View File

@ -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 // TODO: somehow test/confirm that hishtory works even if only bash/only zsh is installed

View File

@ -26,6 +26,7 @@ import (
var offlineInit *bool var offlineInit *bool
var forceInit *bool var forceInit *bool
var offlineInstall *bool var offlineInstall *bool
var skipConfigModification *bool
var installCmd = &cobra.Command{ var installCmd = &cobra.Command{
Use: "install", Use: "install",
@ -37,7 +38,7 @@ var installCmd = &cobra.Command{
if len(args) > 0 { if len(args) > 0 {
secretKey = args[0] secretKey = args[0]
} }
lib.CheckFatalError(install(secretKey, *offlineInstall)) lib.CheckFatalError(install(secretKey, *offlineInstall, *skipConfigModification))
if os.Getenv("HISHTORY_SKIP_INIT_IMPORT") == "" { if os.Getenv("HISHTORY_SKIP_INIT_IMPORT") == "" {
db, err := hctx.OpenLocalSqliteDb() db, err := hctx.OpenLocalSqliteDb()
lib.CheckFatalError(err) lib.CheckFatalError(err)
@ -145,7 +146,7 @@ func warnIfUnsupportedBashVersion() error {
return nil return nil
} }
func install(secretKey string, offline bool) error { func install(secretKey string, offline, skipConfigModification bool) error {
homedir, err := os.UserHomeDir() homedir, err := os.UserHomeDir()
if err != nil { if err != nil {
return fmt.Errorf("failed to get user's home directory: %w", err) 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 { if err != nil {
return err return err
} }
err = configureBashrc(homedir, path) err = configureBashrc(homedir, path, skipConfigModification)
if err != nil { if err != nil {
return err return err
} }
err = configureZshrc(homedir, path) err = configureZshrc(homedir, path, skipConfigModification)
if err != nil { if err != nil {
return err return err
} }
err = configureFish(homedir, path) err = configureFish(homedir, path, skipConfigModification)
if err != nil { if err != nil {
return err return err
} }
@ -254,7 +255,7 @@ func getFishConfigPath(homedir string) string {
return path.Join(homedir, data.GetHishtoryPath(), "config.fish") 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 // Check if fish is installed
_, err := exec.LookPath("fish") _, err := exec.LookPath("fish")
if err != nil { if err != nil {
@ -282,11 +283,15 @@ func configureFish(homedir, binaryPath string) error {
return nil return nil
} }
// Add to fishrc // 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) err = os.MkdirAll(path.Join(homedir, ".config/fish"), 0o744)
if err != nil { if err != nil {
return fmt.Errorf("failed to create fish config directory: %w", err) 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 { func getFishConfigFragment(homedir string) string {
@ -309,7 +314,7 @@ func getZshConfigPath(homedir string) string {
return path.Join(homedir, data.GetHishtoryPath(), "config.zsh") 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. // 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 configContents := lib.ConfigZshContents
if os.Getenv("HISHTORY_TEST") != "" { if os.Getenv("HISHTORY_TEST") != "" {
@ -332,7 +337,7 @@ func configureZshrc(homedir, binaryPath string) error {
return nil return nil
} }
// Add to zshrc // Add to zshrc
return addToShellConfig(getZshRcPath(homedir), getZshConfigFragment(homedir)) return addToShellConfig(getZshRcPath(homedir), getZshConfigFragment(homedir), skipConfigModification)
} }
func getZshRcPath(homedir string) string { func getZshRcPath(homedir string) string {
@ -362,7 +367,7 @@ func getBashConfigPath(homedir string) string {
return path.Join(homedir, data.GetHishtoryPath(), "config.sh") 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. // 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 configContents := lib.ConfigShContents
if os.Getenv("HISHTORY_TEST") != "" { if os.Getenv("HISHTORY_TEST") != "" {
@ -382,7 +387,7 @@ func configureBashrc(homedir, binaryPath string) error {
return fmt.Errorf("failed to check ~/.bashrc: %w", err) return fmt.Errorf("failed to check ~/.bashrc: %w", err)
} }
if !bashRcIsConfigured { if !bashRcIsConfigured {
err = addToShellConfig(path.Join(homedir, ".bashrc"), getBashConfigFragment(homedir)) err = addToShellConfig(path.Join(homedir, ".bashrc"), getBashConfigFragment(homedir), skipConfigModification)
if err != nil { if err != nil {
return err return err
} }
@ -394,7 +399,7 @@ func configureBashrc(homedir, binaryPath string) error {
return fmt.Errorf("failed to check ~/.bash_profile: %w", err) return fmt.Errorf("failed to check ~/.bash_profile: %w", err)
} }
if !bashProfileIsConfigured { if !bashProfileIsConfigured {
err = addToShellConfig(path.Join(homedir, ".bash_profile"), getBashConfigFragment(homedir)) err = addToShellConfig(path.Join(homedir, ".bash_profile"), getBashConfigFragment(homedir), skipConfigModification)
if err != nil { if err != nil {
return err return err
} }
@ -403,7 +408,11 @@ func configureBashrc(homedir, binaryPath string) error {
return nil 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) f, err := os.OpenFile(shellConfigPath, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0o644)
if err != nil { if err != nil {
return fmt.Errorf("failed to append to %s: %w", shellConfigPath, err) return fmt.Errorf("failed to append to %s: %w", shellConfigPath, err)
@ -416,6 +425,17 @@ func addToShellConfig(shellConfigPath, configFragment string) error {
return nil 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 { func getBashConfigFragment(homedir string) string {
return "\n# Hishtory Config:\nexport PATH=\"$PATH:" + path.Join(homedir, data.GetHishtoryPath()) + "\"\nsource " + getBashConfigPath(homedir) + "\n" 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") 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") 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")
} }

View 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
```

View 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
```