mirror of
https://github.com/ddworken/hishtory.git
synced 2024-12-23 23:39:02 +01:00
Enable control-r binding for new installs + prompt upgrades on whether they want to enable control-r bindings
This commit is contained in:
parent
467fd68ddb
commit
253f2fdc27
@ -146,6 +146,7 @@ func TestParameterized(t *testing.T) {
|
||||
t.Run("testMultipleUsers/"+tester.ShellName(), func(t *testing.T) { testMultipleUsers(t, tester) })
|
||||
t.Run("testConfigGetSet/"+tester.ShellName(), func(t *testing.T) { testConfigGetSet(t, tester) })
|
||||
t.Run("testControlR/"+tester.ShellName(), func(t *testing.T) { testControlR(t, tester, tester.ShellName()) })
|
||||
t.Run("testUpgradePromptControlR/"+tester.ShellName(), func(t *testing.T) { testUpgradePromptControlR(t, tester) })
|
||||
// TODO: Add a test for multi-line history entries
|
||||
}
|
||||
t.Run("testControlR/fish", func(t *testing.T) { testControlR(t, bashTester{}, "fish") })
|
||||
@ -1561,8 +1562,15 @@ func testConfigGetSet(t *testing.T, tester shellTester) {
|
||||
defer shared.BackupAndRestore(t)()
|
||||
installHishtory(t, tester, "")
|
||||
|
||||
// Initially is false
|
||||
// Initially is true
|
||||
out := tester.RunInteractiveShell(t, `hishtory config-get enable-control-r`)
|
||||
if out != "true" {
|
||||
t.Fatalf("unexpected config-get output: %#v", out)
|
||||
}
|
||||
|
||||
// Set to false and check
|
||||
tester.RunInteractiveShell(t, `hishtory config-set enable-control-r false`)
|
||||
out = tester.RunInteractiveShell(t, `hishtory config-get enable-control-r`)
|
||||
if out != "false" {
|
||||
t.Fatalf("unexpected config-get output: %#v", out)
|
||||
}
|
||||
@ -1573,9 +1581,65 @@ func testConfigGetSet(t *testing.T, tester shellTester) {
|
||||
if out != "true" {
|
||||
t.Fatalf("unexpected config-get output: %#v", out)
|
||||
}
|
||||
}
|
||||
|
||||
// Set to false and check
|
||||
tester.RunInteractiveShell(t, `hishtory config-set enable-control-r false`)
|
||||
func clearControlRSearchFromConfig(t *testing.T) {
|
||||
configContents, err := hctx.GetConfigContents()
|
||||
shared.Check(t, err)
|
||||
configContents = []byte(strings.ReplaceAll(string(configContents), "enable_control_r_search", "something-else"))
|
||||
homedir, err := os.UserHomeDir()
|
||||
shared.Check(t, err)
|
||||
err = os.WriteFile(path.Join(homedir, shared.HISHTORY_PATH, shared.CONFIG_PATH), configContents, 0o600)
|
||||
shared.Check(t, err)
|
||||
}
|
||||
|
||||
func testUpgradePromptControlR(t *testing.T, tester shellTester) {
|
||||
// Setup
|
||||
defer shared.BackupAndRestore(t)()
|
||||
installHishtory(t, tester, "")
|
||||
|
||||
// Install, and there is no prompt since the config mentions control-r
|
||||
tester.RunInteractiveShell(t, `/tmp/client install`)
|
||||
tester.RunInteractiveShell(t, `hishtory disable`)
|
||||
|
||||
// Ensure that the config doesn't mention control-r
|
||||
clearControlRSearchFromConfig(t)
|
||||
|
||||
// And check that hishtory says it is false by default
|
||||
out := tester.RunInteractiveShell(t, `hishtory config-get enable-control-r`)
|
||||
if out != "false" {
|
||||
t.Fatalf("unexpected config-get output: %#v", out)
|
||||
}
|
||||
|
||||
// And install again, this time we should get a prompt
|
||||
clearControlRSearchFromConfig(t)
|
||||
out, err := tester.RunInteractiveShellRelaxed(t, `yes | /tmp/client install`)
|
||||
shared.Check(t, err)
|
||||
expected := "The new version of hishtory supports binding to your shell's control-R key binding for searching. Do you want to enable this? [y/N] Enabled the control-R key binding, please restart your shell for this to take effect\n"
|
||||
if !strings.HasPrefix(out, expected) {
|
||||
t.Fatalf("hishtory install mismatch, out=%#v", out)
|
||||
}
|
||||
|
||||
// Now it should be enabled
|
||||
out = tester.RunInteractiveShell(t, `hishtory config-get enable-control-r`)
|
||||
if out != "true" {
|
||||
t.Fatalf("unexpected config-get output: %#v", out)
|
||||
}
|
||||
|
||||
// Clear it again, and this time we'll respond no
|
||||
clearControlRSearchFromConfig(t)
|
||||
out = tester.RunInteractiveShell(t, `hishtory config-get enable-control-r`)
|
||||
if out != "false" {
|
||||
t.Fatalf("unexpected config-get output: %#v", out)
|
||||
}
|
||||
clearControlRSearchFromConfig(t)
|
||||
out, err = tester.RunInteractiveShellRelaxed(t, `echo n | /tmp/client install`)
|
||||
shared.Check(t, err)
|
||||
if strings.Contains(out, expected) || strings.Contains(out, "Enabled the control-R key binding") {
|
||||
t.Fatalf("hishtory install mismatch, out=%#v", out)
|
||||
}
|
||||
|
||||
// Now it should be disabled
|
||||
out = tester.RunInteractiveShell(t, `hishtory config-get enable-control-r`)
|
||||
if out != "false" {
|
||||
t.Fatalf("unexpected config-get output: %#v", out)
|
||||
|
@ -160,23 +160,31 @@ type ClientConfig struct {
|
||||
ControlRSearchEnabled bool `json:"enable_control_r_search"`
|
||||
}
|
||||
|
||||
func GetConfig() (ClientConfig, error) {
|
||||
func GetConfigContents() ([]byte, error) {
|
||||
homedir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return ClientConfig{}, fmt.Errorf("failed to retrieve homedir: %v", err)
|
||||
return nil, fmt.Errorf("failed to retrieve homedir: %v", err)
|
||||
}
|
||||
data, err := os.ReadFile(path.Join(homedir, shared.HISHTORY_PATH, shared.CONFIG_PATH))
|
||||
if err != nil {
|
||||
files, err := ioutil.ReadDir(path.Join(homedir, shared.HISHTORY_PATH))
|
||||
if err != nil {
|
||||
return ClientConfig{}, fmt.Errorf("failed to read config file (and failed to list too): %v", err)
|
||||
return nil, fmt.Errorf("failed to read config file (and failed to list too): %v", err)
|
||||
}
|
||||
filenames := ""
|
||||
for _, file := range files {
|
||||
filenames += file.Name()
|
||||
filenames += ", "
|
||||
}
|
||||
return ClientConfig{}, fmt.Errorf("failed to read config file (files in ~/.hishtory/: %s): %v", filenames, err)
|
||||
return nil, fmt.Errorf("failed to read config file (files in ~/.hishtory/: %s): %v", filenames, err)
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func GetConfig() (ClientConfig, error) {
|
||||
data, err := GetConfigContents()
|
||||
if err != nil {
|
||||
return ClientConfig{}, err
|
||||
}
|
||||
var config ClientConfig
|
||||
err = json.Unmarshal(data, &config)
|
||||
|
@ -318,6 +318,7 @@ func Setup(args []string) error {
|
||||
config.UserSecret = userSecret
|
||||
config.IsEnabled = true
|
||||
config.DeviceId = uuid.Must(uuid.NewRandom()).String()
|
||||
config.ControlRSearchEnabled = true
|
||||
err := hctx.SetConfig(config)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to persist config to disk: %v", err)
|
||||
@ -555,6 +556,10 @@ func Install() error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = promptOnUpgradedFeatures()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = hctx.GetConfig()
|
||||
if err != nil {
|
||||
// No config, so set up a new installation
|
||||
@ -563,6 +568,37 @@ func Install() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func promptOnUpgradedFeatures() error {
|
||||
configConents, err := hctx.GetConfigContents()
|
||||
if err != nil {
|
||||
// No config, so this is a new install and thus there is nothing to prompt on
|
||||
return nil
|
||||
}
|
||||
if strings.Contains(string(configConents), "enable_control_r_search") {
|
||||
// control-r search is already configured, so there is nothing to prompt on
|
||||
return nil
|
||||
}
|
||||
fmt.Printf("The new version of hishtory supports binding to your shell's control-R key binding for searching. Do you want to enable this? [y/N] ")
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
resp, err := reader.ReadString('\n')
|
||||
CheckFatalError(err)
|
||||
if strings.TrimSpace(resp) != "y" {
|
||||
// They don't want it, so nothing to do
|
||||
return nil
|
||||
}
|
||||
config, err := hctx.GetConfig()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
config.ControlRSearchEnabled = true
|
||||
err = hctx.SetConfig(config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf("Enabled the control-R key binding, please restart your shell for this to take effect\n")
|
||||
return nil
|
||||
}
|
||||
|
||||
// TODO: deduplicate shell config code
|
||||
|
||||
func configureFish(homedir, binaryPath string) error {
|
||||
|
Loading…
Reference in New Issue
Block a user