Prompt people if they run hishtory init and already have a bunch of entries + fix tests + add TODOs + add hishtory version to requests

This commit is contained in:
David Dworken 2022-10-01 09:50:06 -07:00
parent 4f94698ca6
commit 757ebb9547
5 changed files with 38 additions and 7 deletions

View File

@ -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)
}

View File

@ -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

View File

@ -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)

View File

@ -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)
}

View File

@ -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