Add support for forcing init without prompting via --force flag for #198

This commit is contained in:
David Dworken 2024-04-13 09:51:07 -07:00
parent 68b3311a07
commit 3589e8cf49
No known key found for this signature in database
2 changed files with 24 additions and 1 deletions

View File

@ -3034,4 +3034,25 @@ func TestWebUi(t *testing.T) {
require.Equal(t, 401, resp.StatusCode) require.Equal(t, 401, resp.StatusCode)
} }
func TestForceInit(t *testing.T) {
markTestForSharding(t, 13)
defer testutils.BackupAndRestore(t)()
tester := zshTester{}
initialSecret := installHishtory(t, tester, "")
secondaryUserSecret := initialSecret + "-second"
// Run a commands to search for and confirm it was recorded
tester.RunInteractiveShell(t, `echo foobar`)
require.Equal(t, "echo foobar\n", tester.RunInteractiveShell(t, `hishtory export -pipefail -export`))
// Init as the other user with --force
out, err := tester.RunInteractiveShellRelaxed(t, ` export HISHTORY_SKIP_INIT_IMPORT=1
hishtory init --force `+secondaryUserSecret)
require.NoError(t, err)
require.Contains(t, out, "Setting secret hishtory key to "+secondaryUserSecret, "Failed to re-init with the user secret")
// Check that the history was cleared
require.NotContains(t, tester.RunInteractiveShell(t, `hishtory export`), "echo foobar")
}
// 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

@ -24,6 +24,7 @@ import (
) )
var offlineInit *bool var offlineInit *bool
var forceInit *bool
var offlineInstall *bool var offlineInstall *bool
var installCmd = &cobra.Command{ var installCmd = &cobra.Command{
@ -66,7 +67,7 @@ var initCmd = &cobra.Command{
lib.CheckFatalError(err) lib.CheckFatalError(err)
count, err := lib.CountStoredEntries(db) count, err := lib.CountStoredEntries(db)
lib.CheckFatalError(err) lib.CheckFatalError(err)
if count > 0 { if count > 0 && !(*forceInit) {
fmt.Printf("Your current hishtory profile has saved history entries, are you sure you want to run `init` and reset?\nNote: This won't clear any imported history entries from your existing shell\n[y/N]") fmt.Printf("Your current hishtory profile has saved history entries, are you sure you want to run `init` and reset?\nNote: This won't clear any imported history entries from your existing shell\n[y/N]")
reader := bufio.NewReader(os.Stdin) reader := bufio.NewReader(os.Stdin)
resp, err := reader.ReadString('\n') resp, err := reader.ReadString('\n')
@ -637,5 +638,6 @@ func init() {
rootCmd.AddCommand(uninstallCmd) rootCmd.AddCommand(uninstallCmd)
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")
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 wiht all syncing capabilities disabled")
} }