From ad7e4128552a32832877e98de188d40d1e9ebfa3 Mon Sep 17 00:00:00 2001 From: David Dworken Date: Sun, 5 Jun 2022 18:26:02 -0700 Subject: [PATCH] Add help command --- client/client_test.go | 17 +++++++++++++++++ hishtory.go | 25 +++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/client/client_test.go b/client/client_test.go index 1d72d88..5c1fb08 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -124,6 +124,7 @@ func TestParameterized(t *testing.T) { t.Run("testRequestAndReceiveDbDump/"+tester.ShellName(), func(t *testing.T) { testRequestAndReceiveDbDump(t, tester) }) t.Run("testInstallViaPythonScript/"+tester.ShellName(), func(t *testing.T) { testInstallViaPythonScript(t, tester) }) t.Run("testExportWithQuery/"+tester.ShellName(), func(t *testing.T) { testExportWithQuery(t, tester) }) + t.Run("testHelpCommand/"+tester.ShellName(), func(t *testing.T) { testHelpCommand(t, tester) }) } } @@ -1125,4 +1126,20 @@ echo thisisrecorded`) } } +func testHelpCommand(t *testing.T, tester shellTester) { + // Setup + defer shared.BackupAndRestore(t)() + installHishtory(t, tester, "") + + // Test the help command + out := tester.RunInteractiveShell(t, `hishtory help`) + if !strings.HasPrefix(out, "hishtory: Better shell history\n\nSupported commands:\n") { + t.Fatalf("expected hishtory help to contain intro, actual=%#v", out) + } + out2 := tester.RunInteractiveShell(t, `hishtory -h`) + if out != out2 { + t.Fatalf("expected hishtory -h to equal help") + } +} + // TODO: write a test that runs hishtroy export | grep -v pipefail and then see if that shows up in query/export, I think there is weird behavior here diff --git a/hishtory.go b/hishtory.go index 451198c..04ed062 100644 --- a/hishtory.go +++ b/hishtory.go @@ -51,6 +51,31 @@ func main() { fmt.Printf("Commit Hash: %s\n", GitCommit) case "update": lib.CheckFatalError(lib.Update()) + case "-h": + fallthrough + case "help": + fmt.Print(`hishtory: Better shell history + +Supported commands: + 'hishtory query': Query for matching commands and display them in a table. Examples: + 'hishtory query apt-get' # Find shell commands containing 'apt-get' + 'hishtory query apt-get install' # Find shell commands containing 'apt-get' and 'install' + 'hishtory query curl cwd:/tmp/' # Find shell commands containing 'curl' run in '/tmp/' + 'hishtory query curl user:david' # Find shell commands containing 'curl' run by 'david' + 'hishtory query curl host:x1' # Find shell commands containing 'curl' run on 'x1' + 'hishtory query exit_code:1' # Find shell commands that exited with status code 1 + 'hishtory query before:2022-02-01' # Find shell commands run before 2022-02-01 + 'hishtory export': Query for matching commands and display them in list without any other + metadata. Supports the same query format as 'hishtory query'. + 'hishtory update': Securely update hishtory to the latest version. + 'hishtory disable': Stop recording shell commands + 'hishtory enable': Start recording shell commands + 'hishtory status': View status info including the secret key which is needed to sync shell + history from another machine. + 'hishtory init': Set the secret key to enable syncing shell commands from another + machine with a matching secret key. + 'hishtory help': View this help page +`) default: lib.CheckFatalError(fmt.Errorf("unknown command: %s", os.Args[1])) }