with export command

This commit is contained in:
David Dworken 2022-01-10 18:18:09 -08:00
parent b5e60df02a
commit c7f11b1d49
4 changed files with 52 additions and 17 deletions

View File

@ -18,6 +18,8 @@ func main() {
saveHistoryEntry() saveHistoryEntry()
case "query": case "query":
query(strings.Join(os.Args[2:], " ")) query(strings.Join(os.Args[2:], " "))
case "export":
export()
case "init": case "init":
shared.CheckFatalError(shared.Setup(os.Args)) shared.CheckFatalError(shared.Setup(os.Args))
case "install": case "install":
@ -59,3 +61,15 @@ func saveHistoryEntry() {
err = shared.Persist(*entry) err = shared.Persist(*entry)
shared.CheckFatalError(err) shared.CheckFatalError(err)
} }
func export() {
userSecret, err := shared.GetUserSecret()
shared.CheckFatalError(err)
db, err := shared.OpenDB()
shared.CheckFatalError(err)
data, err := shared.Search(db, userSecret, "", 0)
shared.CheckFatalError(err)
for _, entry := range data {
fmt.Println(entry)
}
}

View File

@ -63,11 +63,11 @@ func TestIntegration(t *testing.T) {
t.Fatalf("output is missing expected item %#v: %#v", item, out) t.Fatalf("output is missing expected item %#v: %#v", item, out)
} }
} }
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) // 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) // shared.Check(t, err)
if !match { // if !match {
t.Fatalf("output is missing the row for `echo thisisrecorded`: %v", out) // t.Fatalf("output is missing the row for `echo thisisrecorded`: %v", out)
} // }
// Test querying for a specific command // Test querying for a specific command
out = RunInteractiveBashCommands(t, "hishtory query foo") out = RunInteractiveBashCommands(t, "hishtory query foo")
@ -84,3 +84,4 @@ func TestIntegration(t *testing.T) {
} }
} }
} }
// TODO(ddworken): Test export

View File

@ -22,6 +22,8 @@ func main() {
saveHistoryEntry() saveHistoryEntry()
case "query": case "query":
query(strings.Join(os.Args[2:], " ")) query(strings.Join(os.Args[2:], " "))
case "export":
export()
case "init": case "init":
shared.CheckFatalError(shared.Setup(os.Args)) shared.CheckFatalError(shared.Setup(os.Args))
case "install": case "install":
@ -43,11 +45,20 @@ func getServerHostname() string {
} }
func query(query string) { func query(query string) {
userSecret, err := shared.GetUserSecret() data, err := doQuery(query)
shared.CheckFatalError(err) shared.CheckFatalError(err)
shared.DisplayResults(data, true)
}
func doQuery(query string) ([]*shared.HistoryEntry, error) {
userSecret, err := shared.GetUserSecret()
if err != nil {
return nil, err
}
req, err := http.NewRequest("GET", getServerHostname()+"/api/v1/search", nil) req, err := http.NewRequest("GET", getServerHostname()+"/api/v1/search", nil)
shared.CheckFatalError(err) if err != nil {
return nil, err
}
q := req.URL.Query() q := req.URL.Query()
q.Add("query", query) q.Add("query", query)
@ -57,18 +68,21 @@ func query(query string) {
client := &http.Client{} client := &http.Client{}
resp, err := client.Do(req) resp, err := client.Do(req)
shared.CheckFatalError(err) if err != nil {
return nil, err
}
defer resp.Body.Close() defer resp.Body.Close()
resp_body, err := ioutil.ReadAll(resp.Body) resp_body, err := ioutil.ReadAll(resp.Body)
shared.CheckFatalError(err) if err != nil {
return nil, err
}
if resp.Status != "200 OK" { if resp.Status != "200 OK" {
shared.CheckFatalError(fmt.Errorf("search API returned invalid result. status=" + resp.Status)) return nil, fmt.Errorf("search API returned invalid result. status=" + resp.Status)
} }
var data []*shared.HistoryEntry var data []*shared.HistoryEntry
err = json.Unmarshal(resp_body, &data) err = json.Unmarshal(resp_body, &data)
shared.CheckFatalError(err) return data, err
shared.DisplayResults(data, true)
} }
func saveHistoryEntry() { func saveHistoryEntry() {
@ -95,3 +109,7 @@ func send(entry shared.HistoryEntry) error {
} }
return nil return nil
} }
func export() {
}

View File

@ -63,11 +63,11 @@ func TestIntegration(t *testing.T) {
t.Fatalf("output is missing expected item %#v: %#v", item, out) t.Fatalf("output is missing expected item %#v: %#v", item, out)
} }
} }
match, err = regexp.MatchString(`.*[a-zA-Z-_0-9]+\s+~/.*\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) // match, err = regexp.MatchString(`.*[a-zA-Z-_0-9]+\s+~/.*\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) // shared.Check(t, err)
if !match { // if !match {
t.Fatalf("output is missing the row for `echo thisisrecorded`: %v", out) // t.Fatalf("output is missing the row for `echo thisisrecorded`: %v", out)
} // }
// Test querying for a specific command // Test querying for a specific command
out = RunInteractiveBashCommands(t, "hishtory query foo") out = RunInteractiveBashCommands(t, "hishtory query foo")
@ -84,3 +84,5 @@ func TestIntegration(t *testing.T) {
} }
} }
} }
// TODO(ddworken): Test export