Half implement querying based on relative and non-relative paths

This commit is contained in:
David Dworken 2022-06-12 20:28:33 -07:00
parent 334fbdcd03
commit af05d823fe
4 changed files with 35 additions and 25 deletions

View File

@ -1142,4 +1142,6 @@ func testHelpCommand(t *testing.T, tester shellTester) {
}
}
// TODO: test for relative CWD and for non-relative CWD
// 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

@ -26,14 +26,15 @@ const (
)
type HistoryEntry struct {
LocalUsername string `json:"local_username" gorm:"uniqueIndex:compositeindex"`
Hostname string `json:"hostname" gorm:"uniqueIndex:compositeindex"`
Command string `json:"command" gorm:"uniqueIndex:compositeindex"`
CurrentWorkingDirectory string `json:"current_working_directory" gorm:"uniqueIndex:compositeindex"`
ExitCode int `json:"exit_code" gorm:"uniqueIndex:compositeindex"`
StartTime time.Time `json:"start_time" gorm:"uniqueIndex:compositeindex"`
EndTime time.Time `json:"end_time" gorm:"uniqueIndex:compositeindex"`
DeviceId string `json:"device_id" gorm:"uniqueIndex:compositeindex"`
LocalUsername string `json:"local_username" gorm:"uniqueIndex:compositeindex"`
Hostname string `json:"hostname" gorm:"uniqueIndex:compositeindex"`
Command string `json:"command" gorm:"uniqueIndex:compositeindex"`
CurrentWorkingDirectory string `json:"current_working_directory" gorm:"uniqueIndex:compositeindex"`
HomeRelativeCurrentWorkingDirectory string `json:"relative_cwd" gorm:"uniqueIndex:compositeindex"`
ExitCode int `json:"exit_code" gorm:"uniqueIndex:compositeindex"`
StartTime time.Time `json:"start_time" gorm:"uniqueIndex:compositeindex"`
EndTime time.Time `json:"end_time" gorm:"uniqueIndex:compositeindex"`
DeviceId string `json:"device_id" gorm:"uniqueIndex:compositeindex"`
}
func sha256hmac(key, additionalData string) []byte {
@ -180,7 +181,7 @@ func Search(db *gorm.DB, query string, limit int) ([]*HistoryEntry, error) {
func parseNonAtomizedToken(token string) (string, interface{}, interface{}, interface{}, error) {
wildcardedToken := "%" + token + "%"
return "(command LIKE ? OR hostname LIKE ? OR current_working_directory LIKE ?)", wildcardedToken, wildcardedToken, wildcardedToken, nil
return "(command LIKE ? OR hostname LIKE ? OR current_working_directory LIKE ? OR relative_cwd LIKE ?)", wildcardedToken, wildcardedToken, wildcardedToken, nil
}
func parseAtomizedToken(token string) (string, interface{}, error) {
@ -196,7 +197,8 @@ 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)", strings.TrimSuffix(val, "/"), nil
trimmed := strings.TrimSuffix(val, "/")
return "((instr(current_working_directory, ?) > 0) OR (instr(relative_cwd, ?) > 0))", trimmed, trimmed, nil
case "exit_code":
return "(exit_code = ?)", val, nil
case "before":
@ -228,6 +230,7 @@ func EntryEquals(entry1, entry2 HistoryEntry) bool {
entry1.Hostname == entry2.Hostname &&
entry1.Command == entry2.Command &&
entry1.CurrentWorkingDirectory == entry2.CurrentWorkingDirectory &&
entry1.HomeRelativeCurrentWorkingDirectory == entry2.HomeRelativeCurrentWorkingDirectory &&
entry1.ExitCode == entry2.ExitCode &&
entry1.StartTime.Format(time.RFC3339) == entry2.StartTime.Format(time.RFC3339) &&
entry1.EndTime.Format(time.RFC3339) == entry2.EndTime.Format(time.RFC3339)
@ -235,12 +238,13 @@ func EntryEquals(entry1, entry2 HistoryEntry) bool {
func MakeFakeHistoryEntry(command string) HistoryEntry {
return HistoryEntry{
LocalUsername: "david",
Hostname: "localhost",
Command: command,
CurrentWorkingDirectory: "/tmp/",
ExitCode: 2,
StartTime: time.Now(),
EndTime: time.Now(),
LocalUsername: "david",
Hostname: "localhost",
Command: command,
CurrentWorkingDirectory: "/tmp/",
HomeRelativeCurrentWorkingDirectory: "/tmp/",
ExitCode: 2,
StartTime: time.Now(),
EndTime: time.Now(),
}
}

View File

@ -49,22 +49,22 @@ var TestConfigZshContents string
var Version string = "Unknown"
func getCwd() (string, error) {
func getCwds() (string, string, error) {
cwd, err := os.Getwd()
if err != nil {
return "", fmt.Errorf("failed to get cwd for last command: %v", err)
return "", "", fmt.Errorf("failed to get cwd for last command: %v", err)
}
homedir, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("failed to get user's home directory: %v", err)
return "", "", fmt.Errorf("failed to get user's home directory: %v", err)
}
if cwd == homedir {
return "~/", nil
return cwd, "~/" nil
}
if strings.HasPrefix(cwd, homedir) {
return strings.Replace(cwd, homedir, "~", 1), nil
return cwd, strings.Replace(cwd, homedir, "~", 1), nil
}
return cwd, nil
return cwd, cwd, nil
}
func BuildHistoryEntry(args []string) (*data.HistoryEntry, error) {
@ -91,11 +91,12 @@ func BuildHistoryEntry(args []string) (*data.HistoryEntry, error) {
entry.LocalUsername = user.Username
// cwd
cwd, err := getCwd()
cwd, relativeCwd, err := getCwds()
if err != nil {
return nil, fmt.Errorf("failed to build history entry: %v", err)
}
entry.CurrentWorkingDirectory = cwd
entry.HomeRelativeCurrentWorkingDirectory = relativeCwd
// start time
seconds, err := parseCrossPlatformInt(args[5])
@ -238,6 +239,7 @@ func AddToDbIfNew(db *gorm.DB, entry data.HistoryEntry) {
tx = tx.Where("hostname = ?", entry.Hostname)
tx = tx.Where("command = ?", entry.Command)
tx = tx.Where("current_working_directory = ?", entry.CurrentWorkingDirectory)
tx = tx.Where("relative_cwd = ?", entry.HomeRelativeCurrentWorkingDirectory)
tx = tx.Where("exit_code = ?", entry.ExitCode)
tx = tx.Where("start_time = ?", entry.StartTime)
tx = tx.Where("end_time = ?", entry.EndTime)
@ -256,7 +258,7 @@ func DisplayResults(results []*data.HistoryEntry) {
for _, result := range results {
timestamp := result.StartTime.Format("Jan 2 2006 15:04:05 MST")
duration := result.EndTime.Sub(result.StartTime).Round(time.Millisecond).String()
tbl.AddRow(result.Hostname, result.CurrentWorkingDirectory, timestamp, duration, result.ExitCode, result.Command)
tbl.AddRow(result.Hostname, result.HomeRelativeCurrentWorkingDirectory, timestamp, duration, result.ExitCode, result.Command)
}
tbl.Print()

View File

@ -52,6 +52,7 @@ func TestBuildHistoryEntry(t *testing.T) {
if !strings.HasPrefix(entry.CurrentWorkingDirectory, "/") && !strings.HasPrefix(entry.CurrentWorkingDirectory, "~/") {
t.Fatalf("history entry has unexpected cwd: %v", entry.CurrentWorkingDirectory)
}
// TODO: test relative_cwd
if entry.Command != "ls /foo" {
t.Fatalf("history entry has unexpected command: %v", entry.Command)
}
@ -74,6 +75,7 @@ func TestBuildHistoryEntry(t *testing.T) {
if !strings.HasPrefix(entry.CurrentWorkingDirectory, "/") && !strings.HasPrefix(entry.CurrentWorkingDirectory, "~/") {
t.Fatalf("history entry has unexpected cwd: %v", entry.CurrentWorkingDirectory)
}
// TODO: test relative_cwd
if entry.Command != "ls /foo" {
t.Fatalf("history entry has unexpected command: %v", entry.Command)
}