From 757ebb9547ce1e02eb826d82f50192339ed1383f Mon Sep 17 00:00:00 2001 From: David Dworken Date: Sat, 1 Oct 2022 09:50:06 -0700 Subject: [PATCH] Prompt people if they run hishtory init and already have a bunch of entries + fix tests + add TODOs + add hishtory version to requests --- backend/server/server.go | 2 +- backend/server/server_test.go | 2 +- client/client_test.go | 5 +++-- client/lib/lib.go | 19 +++++++++++++++++-- hishtory.go | 17 ++++++++++++++++- 5 files changed, 38 insertions(+), 7 deletions(-) diff --git a/backend/server/server.go b/backend/server/server.go index 1cc8ab7..cf7224c 100644 --- a/backend/server/server.go +++ b/backend/server/server.go @@ -585,7 +585,7 @@ func withLogging(h func(http.ResponseWriter, *http.Request)) http.Handler { h(&lrw, r) duration := time.Since(start) - fmt.Printf("%s %s %#v %s %s\n", r.RemoteAddr, r.Method, r.RequestURI, duration.String(), byteCountToString(responseData.size)) + fmt.Printf("%s %s %#v %s %s %s\n", r.RemoteAddr, r.Method, r.RequestURI, r.Header.Get("X-Hishtory-Version"), duration.String(), byteCountToString(responseData.size)) } return http.HandlerFunc(logFn) } diff --git a/backend/server/server_test.go b/backend/server/server_test.go index 579653a..ed438be 100644 --- a/backend/server/server_test.go +++ b/backend/server/server_test.go @@ -204,7 +204,7 @@ func TestDumpRequestAndResponse(t *testing.T) { shared.Check(t, err) reqBody, err := json.Marshal([]shared.EncHistoryEntry{entry1, entry2}) shared.Check(t, err) - submitReq := httptest.NewRequest(http.MethodPost, "/?user_id="+userId+"&requesting_device_id="+devId2, bytes.NewReader(reqBody)) + submitReq := httptest.NewRequest(http.MethodPost, "/?user_id="+userId+"&requesting_device_id="+devId2+"&source_device_id="+devId1, bytes.NewReader(reqBody)) apiSubmitDumpHandler(nil, submitReq) // Check that the dump request is no longer there for userId for either device ID diff --git a/client/client_test.go b/client/client_test.go index acb44cf..eeae246 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -203,7 +203,8 @@ func testIntegrationWithNewDevice(t *testing.T, tester shellTester) { } // Set the secret key to the previous secret key - out = tester.RunInteractiveShell(t, `hishtory init `+userSecret) + out, err := tester.RunInteractiveShellRelaxed(t, `yes | hishtory init `+userSecret) + shared.Check(t, err) if !strings.Contains(out, "Setting secret hishtory key to "+userSecret) { t.Fatalf("Failed to re-init with the user secret: %v", out) } @@ -249,7 +250,7 @@ func testIntegrationWithNewDevice(t *testing.T, tester shellTester) { if strings.Contains(out, "thisisnotrecorded") { t.Fatalf("hishtory export contains a command that should not have been recorded, out=%#v", out) } - expectedOutputWithoutKey := "hishtory status\nhishtory query\nls /a\nls /bar\nls /foo\necho foo\necho bar\nhishtory enable\necho thisisrecorded\nhishtory query\nhishtory query foo\necho hello | grep complex | sed s/h/i/g; echo baz && echo \"fo 'o\"\nhishtory query complex\nhishtory query\necho mynewcommand\nhishtory query\nhishtory init %s\nhishtory query\necho mynewercommand\nhishtory query\nothercomputer\nhishtory query\n" + expectedOutputWithoutKey := "hishtory status\nhishtory query\nls /a\nls /bar\nls /foo\necho foo\necho bar\nhishtory enable\necho thisisrecorded\nhishtory query\nhishtory query foo\necho hello | grep complex | sed s/h/i/g; echo baz && echo \"fo 'o\"\nhishtory query complex\nhishtory query\necho mynewcommand\nhishtory query\nyes | hishtory init %s\nhishtory query\necho mynewercommand\nhishtory query\nothercomputer\nhishtory query\n" expectedOutput := fmt.Sprintf(expectedOutputWithoutKey, userSecret) if diff := cmp.Diff(expectedOutput, out); diff != "" { t.Fatalf("hishtory export mismatch (-expected +got):\n%s\nout=%#v", diff, out) diff --git a/client/lib/lib.go b/client/lib/lib.go index e9b07a8..ae10a26 100644 --- a/client/lib/lib.go +++ b/client/lib/lib.go @@ -898,12 +898,21 @@ func getServerHostname() string { return "https://api.hishtory.dev" } +func httpClient() *http.Client { + return &http.Client{} +} + func ApiGet(path string) ([]byte, error) { if os.Getenv("HISHTORY_SIMULATE_NETWORK_ERROR") != "" { return nil, fmt.Errorf("simulated network error: dial tcp: lookup api.hishtory.dev") } start := time.Now() - resp, err := http.Get(getServerHostname() + path) + req, err := http.NewRequest("GET", getServerHostname()+path, nil) + if err != nil { + return nil, fmt.Errorf("failed to create GET: %v", err) + } + req.Header.Set("X-Hishtory-Version", "v0."+Version) + resp, err := httpClient().Do(req) if err != nil { return nil, fmt.Errorf("failed to GET %s%s: %v", getServerHostname(), path, err) } @@ -925,7 +934,13 @@ func ApiPost(path, contentType string, data []byte) ([]byte, error) { return nil, fmt.Errorf("simulated network error: dial tcp: lookup api.hishtory.dev") } start := time.Now() - resp, err := http.Post(getServerHostname()+path, contentType, bytes.NewBuffer(data)) + req, err := http.NewRequest("POST", getServerHostname()+path, bytes.NewBuffer(data)) + if err != nil { + return nil, fmt.Errorf("failed to create POST: %v", err) + } + req.Header.Set("Content-Type", contentType) + req.Header.Set("X-Hishtory-Version", "v0."+Version) + resp, err := httpClient().Do(req) if err != nil { return nil, fmt.Errorf("failed to POST %s: %v", path, err) } diff --git a/hishtory.go b/hishtory.go index d31929a..d36ea49 100644 --- a/hishtory.go +++ b/hishtory.go @@ -1,6 +1,7 @@ package main import ( + "bufio" "context" "encoding/json" "fmt" @@ -49,7 +50,20 @@ func main() { } lib.CheckFatalError(lib.Redact(ctx, query, force)) case "init": - // TODO: prompt people if they run hishtory init and already have a bunch of history entries + db, err := hctx.OpenLocalSqliteDb() + lib.CheckFatalError(err) + data, err := data.Search(db, "", 10) + lib.CheckFatalError(err) + if len(data) > 0 { + fmt.Printf("Your current hishtory profile has saved history entries, are you sure you want to run `init` and reset? [y/N]") + reader := bufio.NewReader(os.Stdin) + resp, err := reader.ReadString('\n') + lib.CheckFatalError(err) + if strings.TrimSpace(resp) != "y" { + fmt.Printf("Aborting init per user response of %#v\n", strings.TrimSpace(resp)) + return + } + } lib.CheckFatalError(lib.Setup(os.Args)) case "install": lib.CheckFatalError(lib.Install()) @@ -91,6 +105,7 @@ func main() { } fmt.Printf("Commit Hash: %s\n", GitCommit) case "update": + // TODO: Add banner integration to update lib.CheckFatalError(lib.Update(hctx.MakeContext())) case "-h": fallthrough