Revert "Half implement querying based on relative and non-relative paths"

This reverts commit af05d823fe. I will pick up work on this later.
This commit is contained in:
David Dworken 2022-06-12 20:28:40 -07:00
parent af05d823fe
commit 3f32891469
4 changed files with 25 additions and 35 deletions

View File

@ -1142,6 +1142,4 @@ 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,15 +26,14 @@ 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"`
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"`
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"`
}
func sha256hmac(key, additionalData string) []byte {
@ -181,7 +180,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 ? OR relative_cwd LIKE ?)", wildcardedToken, wildcardedToken, wildcardedToken, nil
return "(command LIKE ? OR hostname LIKE ? OR current_working_directory LIKE ?)", wildcardedToken, wildcardedToken, wildcardedToken, nil
}
func parseAtomizedToken(token string) (string, interface{}, error) {
@ -197,8 +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?
trimmed := strings.TrimSuffix(val, "/")
return "((instr(current_working_directory, ?) > 0) OR (instr(relative_cwd, ?) > 0))", trimmed, trimmed, nil
return "(instr(current_working_directory, ?) > 0)", strings.TrimSuffix(val, "/"), nil
case "exit_code":
return "(exit_code = ?)", val, nil
case "before":
@ -230,7 +228,6 @@ 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)
@ -238,13 +235,12 @@ func EntryEquals(entry1, entry2 HistoryEntry) bool {
func MakeFakeHistoryEntry(command string) HistoryEntry {
return HistoryEntry{
LocalUsername: "david",
Hostname: "localhost",
Command: command,
CurrentWorkingDirectory: "/tmp/",
HomeRelativeCurrentWorkingDirectory: "/tmp/",
ExitCode: 2,
StartTime: time.Now(),
EndTime: time.Now(),
LocalUsername: "david",
Hostname: "localhost",
Command: command,
CurrentWorkingDirectory: "/tmp/",
ExitCode: 2,
StartTime: time.Now(),
EndTime: time.Now(),
}
}

View File

@ -49,22 +49,22 @@ var TestConfigZshContents string
var Version string = "Unknown"
func getCwds() (string, string, error) {
func getCwd() (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 cwd, "~/" nil
return "~/", nil
}
if strings.HasPrefix(cwd, homedir) {
return cwd, strings.Replace(cwd, homedir, "~", 1), nil
return strings.Replace(cwd, homedir, "~", 1), nil
}
return cwd, cwd, nil
return cwd, nil
}
func BuildHistoryEntry(args []string) (*data.HistoryEntry, error) {
@ -91,12 +91,11 @@ func BuildHistoryEntry(args []string) (*data.HistoryEntry, error) {
entry.LocalUsername = user.Username
// cwd
cwd, relativeCwd, err := getCwds()
cwd, err := getCwd()
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])
@ -239,7 +238,6 @@ 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)
@ -258,7 +256,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.HomeRelativeCurrentWorkingDirectory, timestamp, duration, result.ExitCode, result.Command)
tbl.AddRow(result.Hostname, result.CurrentWorkingDirectory, timestamp, duration, result.ExitCode, result.Command)
}
tbl.Print()

View File

@ -52,7 +52,6 @@ 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)
}
@ -75,7 +74,6 @@ 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)
}