diff --git a/client/client_test.go b/client/client_test.go index d611e94..3487904 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -3034,4 +3034,25 @@ func TestWebUi(t *testing.T) { 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 diff --git a/client/cmd/install.go b/client/cmd/install.go index 4fe55f5..999f0ec 100644 --- a/client/cmd/install.go +++ b/client/cmd/install.go @@ -25,6 +25,7 @@ import ( ) var offlineInit *bool +var forceInit *bool var offlineInstall *bool var installCmd = &cobra.Command{ @@ -67,7 +68,7 @@ var initCmd = &cobra.Command{ lib.CheckFatalError(err) count, err := countStoredEntries(db) 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]") reader := bufio.NewReader(os.Stdin) resp, err := reader.ReadString('\n') @@ -645,5 +646,6 @@ func init() { rootCmd.AddCommand(uninstallCmd) 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") }