Disable flag parsing for the querying functions so they can use exclusions + refactor redact to remove the --force flag

This commit is contained in:
David Dworken 2022-11-14 20:31:20 -08:00
parent 6ef9bb00d5
commit 0667494239
No known key found for this signature in database
4 changed files with 34 additions and 33 deletions

View File

@ -1531,27 +1531,27 @@ ls /tmp`, randomCmdUuid, randomCmdUuid)
}
// Redact foo
out = tester.RunInteractiveShell(t, `hishtory redact --force foo`)
out = tester.RunInteractiveShell(t, `HISHTORY_REDACT_FORCE=1 hishtory redact foo`)
if out != "Permanently deleting 2 entries\n" {
t.Fatalf("hishtory redact gave unexpected output=%#v", out)
}
// Check that the commands are redacted
out = tester.RunInteractiveShell(t, `hishtory export | grep -v pipefail`)
expectedOutput = fmt.Sprintf("echo %s-bas\nls /tmp\nhishtory redact --force foo\n", randomCmdUuid)
expectedOutput = fmt.Sprintf("echo %s-bas\nls /tmp\nHISHTORY_REDACT_FORCE=1 hishtory redact foo\n", randomCmdUuid)
if diff := cmp.Diff(expectedOutput, out); diff != "" {
t.Fatalf("hishtory export mismatch (-expected +got):\n%s\nout=%#v", diff, out)
}
// Redact s
out = tester.RunInteractiveShell(t, `hishtory redact --force s`)
out = tester.RunInteractiveShell(t, `HISHTORY_REDACT_FORCE=1 hishtory redact s`)
if out != "Permanently deleting 10 entries\n" && out != "Permanently deleting 11 entries\n" {
t.Fatalf("hishtory redact gave unexpected output=%#v", out)
}
// Check that the commands are redacted
out = tester.RunInteractiveShell(t, `hishtory export | grep -v pipefail`)
expectedOutput = "hishtory redact --force s\n"
expectedOutput = "HISHTORY_REDACT_FORCE=1 hishtory redact s\n"
if diff := cmp.Diff(expectedOutput, out); diff != "" {
t.Fatalf("hishtory export mismatch (-expected +got):\n%s\nout=%#v", diff, out)
}
@ -1559,12 +1559,12 @@ ls /tmp`, randomCmdUuid, randomCmdUuid)
// Record another command
tester.RunInteractiveShell(t, `echo hello`)
out = tester.RunInteractiveShell(t, `hishtory export | grep -v pipefail`)
expectedOutput = "hishtory redact --force s\necho hello\n"
expectedOutput = "HISHTORY_REDACT_FORCE=1 hishtory redact s\necho hello\n"
if diff := cmp.Diff(expectedOutput, out); diff != "" {
t.Fatalf("hishtory export mismatch (-expected +got):\n%s\nout=%#v", diff, out)
}
// Redact it without --force
// Redact it without HISHTORY_REDACT_FORCE
out, err := tester.RunInteractiveShellRelaxed(t, `yes | hishtory redact hello`)
testutils.Check(t, err)
if out != "This will permanently delete 1 entries, are you sure? [y/N]" {
@ -1573,7 +1573,7 @@ ls /tmp`, randomCmdUuid, randomCmdUuid)
// And check it was redacted
out = tester.RunInteractiveShell(t, `hishtory export | grep -v pipefail`)
expectedOutput = "hishtory redact --force s\nyes | hishtory redact hello\n"
expectedOutput = "HISHTORY_REDACT_FORCE=1 hishtory redact s\nyes | hishtory redact hello\n"
if diff := cmp.Diff(expectedOutput, out); diff != "" {
t.Fatalf("hishtory export mismatch (-expected +got):\n%s\nout=%#v", diff, out)
}
@ -1614,14 +1614,14 @@ ls /tmp`, randomCmdUuid, randomCmdUuid)
// Restore the first client, and redact some commands
restoreInstall2 := testutils.BackupAndRestoreWithId(t, "-2")
restoreInstall1()
out = tester.RunInteractiveShell(t, `hishtory redact --force `+randomCmdUuid)
out = tester.RunInteractiveShell(t, `HISHTORY_REDACT_FORCE=1 hishtory redact `+randomCmdUuid)
if out != "Permanently deleting 2 entries\n" {
t.Fatalf("hishtory redact gave unexpected output=%#v", out)
}
// Confirm that client1 doesn't have the commands
out = tester.RunInteractiveShell(t, `hishtory export | grep -v pipefail`)
expectedOutput = fmt.Sprintf("echo foo\nls /tmp\nhishtory redact --force %s\n", randomCmdUuid)
expectedOutput = fmt.Sprintf("echo foo\nls /tmp\nHISHTORY_REDACT_FORCE=1 hishtory redact %s\n", randomCmdUuid)
if diff := cmp.Diff(expectedOutput, out); diff != "" {
t.Fatalf("hishtory export mismatch (-expected +got):\n%s\nout=%#v", diff, out)
}
@ -2413,7 +2413,7 @@ func fuzzTest(t *testing.T, tester shellTester, input string) {
testutils.Check(t, err)
}
if op.redactQuery != "" {
_, err := tester.RunInteractiveShellRelaxed(t, `hishtory redact --force `+op.redactQuery)
_, err := tester.RunInteractiveShellRelaxed(t, `HISHTORY_REDACT_FORCE=1 hishtory redact `+op.redactQuery)
testutils.Check(t, err)
}
@ -2436,7 +2436,7 @@ func fuzzTest(t *testing.T, tester shellTester, input string) {
filteredLines = append(filteredLines, line)
}
val = strings.Join(filteredLines, "\n")
val += `hishtory redact --force ` + op.redactQuery + "\n"
val += `HISHTORY_REDACT_FORCE=1 hishtory redact ` + op.redactQuery + "\n"
}
keyToCommands[op.device.key] = val

View File

@ -24,10 +24,11 @@ var EXAMPLE_QUERIES string = `Example queries:
var GROUP_ID_QUERYING string = "group_id:querying"
var queryCmd = &cobra.Command{
Use: "query",
Short: "Query your shell history and display the results in an ASCII art table",
GroupID: GROUP_ID_QUERYING,
Long: strings.ReplaceAll(EXAMPLE_QUERIES, "SUBCOMMAND", "query"),
Use: "query",
Short: "Query your shell history and display the results in an ASCII art table",
GroupID: GROUP_ID_QUERYING,
Long: strings.ReplaceAll(EXAMPLE_QUERIES, "SUBCOMMAND", "query"),
DisableFlagParsing: true,
Run: func(cmd *cobra.Command, args []string) {
ctx := hctx.MakeContext()
lib.CheckFatalError(lib.ProcessDeletionRequests(ctx))
@ -36,10 +37,11 @@ var queryCmd = &cobra.Command{
}
var tqueryCmd = &cobra.Command{
Use: "tquery",
Short: "Interactively query your shell history in a TUI interface",
GroupID: GROUP_ID_QUERYING,
Long: strings.ReplaceAll(EXAMPLE_QUERIES, "SUBCOMMAND", "tquery"),
Use: "tquery",
Short: "Interactively query your shell history in a TUI interface",
GroupID: GROUP_ID_QUERYING,
Long: strings.ReplaceAll(EXAMPLE_QUERIES, "SUBCOMMAND", "tquery"),
DisableFlagParsing: true,
Run: func(cmd *cobra.Command, args []string) {
ctx := hctx.MakeContext()
lib.CheckFatalError(lib.TuiQuery(ctx, strings.Join(args, " ")))
@ -47,10 +49,11 @@ var tqueryCmd = &cobra.Command{
}
var exportCmd = &cobra.Command{
Use: "export",
Short: "Export your shell history and display just the raw commands",
GroupID: GROUP_ID_QUERYING,
Long: strings.ReplaceAll(EXAMPLE_QUERIES, "SUBCOMMAND", "export"),
Use: "export",
Short: "Export your shell history and display just the raw commands",
GroupID: GROUP_ID_QUERYING,
Long: strings.ReplaceAll(EXAMPLE_QUERIES, "SUBCOMMAND", "export"),
DisableFlagParsing: true,
Run: func(cmd *cobra.Command, args []string) {
ctx := hctx.MakeContext()
lib.CheckFatalError(lib.ProcessDeletionRequests(ctx))

View File

@ -1,6 +1,7 @@
package cmd
import (
"os"
"strings"
"github.com/ddworken/hishtory/client/hctx"
@ -8,26 +9,24 @@ import (
"github.com/spf13/cobra"
)
var force *bool
var GROUP_ID_MANAGEMENT string = "group_id_management"
var redactCmd = &cobra.Command{
Use: "redact",
Aliases: []string{"delete"},
Short: "Query for matching commands and remove them from your shell history",
Long: "This removes history entries on the current machine and on all remote machines. Supports the same query format as 'hishtory query'.",
GroupID: GROUP_ID_MANAGEMENT,
Use: "redact",
Aliases: []string{"delete"},
Short: "Query for matching commands and remove them from your shell history",
Long: "This removes history entries on the current machine and on all remote machines. Supports the same query format as 'hishtory query'.",
GroupID: GROUP_ID_MANAGEMENT,
DisableFlagParsing: true,
Run: func(cmd *cobra.Command, args []string) {
ctx := hctx.MakeContext()
lib.CheckFatalError(lib.RetrieveAdditionalEntriesFromRemote(ctx))
lib.CheckFatalError(lib.ProcessDeletionRequests(ctx))
query := strings.Join(args, " ")
lib.CheckFatalError(lib.Redact(ctx, query, *force))
lib.CheckFatalError(lib.Redact(ctx, query, os.Getenv("HISHTORY_REDACT_FORCE") != ""))
},
}
func init() {
rootCmd.AddCommand(redactCmd)
force = redactCmd.Flags().Bool("force", false, "Force redaction with no confirmation prompting")
}

View File

@ -12,6 +12,5 @@ func main() {
/*
Remaining things:
* Support exclusions in searches
* Acutally migrate saveHistoryEntry to cobra
*/