Support queries as part of the export command + more info sent to the banner endpoint

This commit is contained in:
David Dworken 2022-06-05 18:05:06 -07:00
parent 5fdb922801
commit 5e44e7ef36
3 changed files with 82 additions and 5 deletions

View File

@ -123,6 +123,7 @@ func TestParameterized(t *testing.T) {
t.Run("testTimestampsAreReasonablyCorrect/"+tester.ShellName(), func(t *testing.T) { testTimestampsAreReasonablyCorrect(t, tester) })
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) })
}
}
@ -302,6 +303,7 @@ echo thisisrecorded`)
t.Fatalf("output is missing expected item %#v: %#v", item, out)
}
}
// TODO
// match, err = regexp.MatchString(`.*~/.*\s+[a-zA-Z]{3} \d+ 2022 \d\d:\d\d:\d\d PST\s+\d{1,2}ms\s+0\s+echo thisisrecorded.*`, out)
// shared.Check(t, err)
// if !match {
@ -379,6 +381,17 @@ hishtory disable`)
if strings.Count(out, "\n") != 3 {
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
}
// And again, but with a strailing slash
out = hishtoryQuery(t, tester, `cwd:/tmp/`)
if !strings.Contains(out, "echo querybydir") {
t.Fatalf("hishtory query doesn't contain result matching cwd:/tmp, out=%#v", out)
}
if strings.Contains(out, "nevershouldappear") {
t.Fatalf("hishtory query contains unexpected entry, out=%#v", out)
}
if strings.Count(out, "\n") != 3 {
t.Fatalf("hishtory query has the wrong number of lines=%d, out=%#v", strings.Count(out, "\n"), out)
}
// Query based on cwd without the slash
out = hishtoryQuery(t, tester, `cwd:tmp`)
@ -1048,4 +1061,68 @@ func testInstallViaPythonScript(t *testing.T, tester shellTester) {
}
}
func testExportWithQuery(t *testing.T, tester shellTester) {
// Setup
defer shared.BackupAndRestore(t)()
installHishtory(t, tester, "")
// Test recording commands
out, err := tester.RunInteractiveShellRelaxed(t, `ls /a
ls /bar
ls /foo
echo foo
echo bar
hishtory disable
echo thisisnotrecorded
sleep 0.5
cd /tmp/
hishtory enable
echo thisisrecorded`)
if err != nil {
t.Fatal(err)
}
if out != "foo\nbar\nthisisnotrecorded\nthisisrecorded\n" {
t.Fatalf("unexpected output from running commands: %#v", out)
}
// Test querying for all commands
out = hishtoryQuery(t, tester, "")
expected := []string{"echo thisisrecorded", "hishtory enable", "echo bar", "echo foo", "ls /foo", "ls /bar", "ls /a"}
for _, item := range expected {
if !strings.Contains(out, item) {
t.Fatalf("output is missing expected item %#v: %#v", item, out)
}
}
// Test querying for a specific command
out = hishtoryQuery(t, tester, "foo")
expected = []string{"echo foo", "ls /foo"}
unexpected := []string{"echo thisisrecorded", "hishtory enable", "echo bar", "ls /bar", "ls /a"}
for _, item := range expected {
if !strings.Contains(out, item) {
t.Fatalf("output is missing expected item %#v: %#v", item, out)
}
if strings.Count(out, item) != 1 {
t.Fatalf("output has %#v in it multiple times! out=%#v", item, out)
}
}
for _, item := range unexpected {
if strings.Contains(out, item) {
t.Fatalf("output is containing unexpected item %#v: %#v", item, out)
}
}
// Test using export with a query
out = tester.RunInteractiveShell(t, `hishtory export foo`)
if out != "ls /foo\necho foo\nhishtory query foo\n" {
t.Fatalf("expected hishtory export to equal out=%#v", out)
}
// Test a more complex query with export
out = tester.RunInteractiveShell(t, `hishtory export cwd:/tmp/`)
if out != "hishtory enable\necho thisisrecorded\n" {
t.Fatalf("expected hishtory export to equal out=%#v", out)
}
}
// 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

View File

@ -196,7 +196,7 @@ func parseAtomizedToken(token string) (string, interface{}, error) {
return "(instr(hostname, ?) > 0)", val, nil
case "cwd":
// TODO: Can I make this support querying via ~/ too?
return "(instr(current_working_directory, ?) > 0)", val, nil
return "(instr(current_working_directory, ?) > 0)", strings.TrimSuffix(val, "/"), nil
case "exit_code":
return "(exit_code = ?)", val, nil
case "before":

View File

@ -27,7 +27,7 @@ func main() {
case "query":
query(strings.Join(os.Args[2:], " "))
case "export":
export()
export(strings.Join(os.Args[2:], " "))
case "init":
lib.CheckFatalError(lib.Setup(os.Args))
case "install":
@ -122,7 +122,7 @@ func displayBannerIfSet() error {
if err != nil {
return fmt.Errorf("failed to get config: %v", err)
}
url := "/api/v1/banner?commit_hash=" + GitCommit + "&device_id=" + config.DeviceId + "&forced_banner=" + os.Getenv("FORCED_BANNER")
url := "/api/v1/banner?commit_hash=" + GitCommit + "&user_id=" + data.UserId(config.UserSecret) + "&device_id=" + config.DeviceId + "&version=" + lib.Version + "&forced_banner=" + os.Getenv("FORCED_BANNER")
respBody, err := lib.ApiGet(url)
if err != nil {
return err
@ -201,7 +201,7 @@ func saveHistoryEntry() {
}
}
func export() {
func export(query string) {
db, err := lib.OpenLocalSqliteDb()
lib.CheckFatalError(err)
err = retrieveAdditionalEntriesFromRemote(db)
@ -212,7 +212,7 @@ func export() {
lib.CheckFatalError(err)
}
}
data, err := data.Search(db, "", 0)
data, err := data.Search(db, query, 0)
lib.CheckFatalError(err)
for i := len(data) - 1; i >= 0; i-- {
fmt.Println(data[i].Command)