mirror of
https://github.com/ddworken/hishtory.git
synced 2025-02-17 02:50:49 +01:00
Half implement querying based on relative and non-relative paths
This commit is contained in:
parent
334fbdcd03
commit
af05d823fe
@ -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
|
// 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
|
||||||
|
@ -26,14 +26,15 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type HistoryEntry struct {
|
type HistoryEntry struct {
|
||||||
LocalUsername string `json:"local_username" gorm:"uniqueIndex:compositeindex"`
|
LocalUsername string `json:"local_username" gorm:"uniqueIndex:compositeindex"`
|
||||||
Hostname string `json:"hostname" gorm:"uniqueIndex:compositeindex"`
|
Hostname string `json:"hostname" gorm:"uniqueIndex:compositeindex"`
|
||||||
Command string `json:"command" gorm:"uniqueIndex:compositeindex"`
|
Command string `json:"command" gorm:"uniqueIndex:compositeindex"`
|
||||||
CurrentWorkingDirectory string `json:"current_working_directory" gorm:"uniqueIndex:compositeindex"`
|
CurrentWorkingDirectory string `json:"current_working_directory" gorm:"uniqueIndex:compositeindex"`
|
||||||
ExitCode int `json:"exit_code" gorm:"uniqueIndex:compositeindex"`
|
HomeRelativeCurrentWorkingDirectory string `json:"relative_cwd" gorm:"uniqueIndex:compositeindex"`
|
||||||
StartTime time.Time `json:"start_time" gorm:"uniqueIndex:compositeindex"`
|
ExitCode int `json:"exit_code" gorm:"uniqueIndex:compositeindex"`
|
||||||
EndTime time.Time `json:"end_time" gorm:"uniqueIndex:compositeindex"`
|
StartTime time.Time `json:"start_time" gorm:"uniqueIndex:compositeindex"`
|
||||||
DeviceId string `json:"device_id" 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 {
|
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) {
|
func parseNonAtomizedToken(token string) (string, interface{}, interface{}, interface{}, error) {
|
||||||
wildcardedToken := "%" + token + "%"
|
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) {
|
func parseAtomizedToken(token string) (string, interface{}, error) {
|
||||||
@ -196,7 +197,8 @@ func parseAtomizedToken(token string) (string, interface{}, error) {
|
|||||||
return "(instr(hostname, ?) > 0)", val, nil
|
return "(instr(hostname, ?) > 0)", val, nil
|
||||||
case "cwd":
|
case "cwd":
|
||||||
// TODO: Can I make this support querying via ~/ too?
|
// 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":
|
case "exit_code":
|
||||||
return "(exit_code = ?)", val, nil
|
return "(exit_code = ?)", val, nil
|
||||||
case "before":
|
case "before":
|
||||||
@ -228,6 +230,7 @@ func EntryEquals(entry1, entry2 HistoryEntry) bool {
|
|||||||
entry1.Hostname == entry2.Hostname &&
|
entry1.Hostname == entry2.Hostname &&
|
||||||
entry1.Command == entry2.Command &&
|
entry1.Command == entry2.Command &&
|
||||||
entry1.CurrentWorkingDirectory == entry2.CurrentWorkingDirectory &&
|
entry1.CurrentWorkingDirectory == entry2.CurrentWorkingDirectory &&
|
||||||
|
entry1.HomeRelativeCurrentWorkingDirectory == entry2.HomeRelativeCurrentWorkingDirectory &&
|
||||||
entry1.ExitCode == entry2.ExitCode &&
|
entry1.ExitCode == entry2.ExitCode &&
|
||||||
entry1.StartTime.Format(time.RFC3339) == entry2.StartTime.Format(time.RFC3339) &&
|
entry1.StartTime.Format(time.RFC3339) == entry2.StartTime.Format(time.RFC3339) &&
|
||||||
entry1.EndTime.Format(time.RFC3339) == entry2.EndTime.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 {
|
func MakeFakeHistoryEntry(command string) HistoryEntry {
|
||||||
return HistoryEntry{
|
return HistoryEntry{
|
||||||
LocalUsername: "david",
|
LocalUsername: "david",
|
||||||
Hostname: "localhost",
|
Hostname: "localhost",
|
||||||
Command: command,
|
Command: command,
|
||||||
CurrentWorkingDirectory: "/tmp/",
|
CurrentWorkingDirectory: "/tmp/",
|
||||||
ExitCode: 2,
|
HomeRelativeCurrentWorkingDirectory: "/tmp/",
|
||||||
StartTime: time.Now(),
|
ExitCode: 2,
|
||||||
EndTime: time.Now(),
|
StartTime: time.Now(),
|
||||||
|
EndTime: time.Now(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -49,22 +49,22 @@ var TestConfigZshContents string
|
|||||||
|
|
||||||
var Version string = "Unknown"
|
var Version string = "Unknown"
|
||||||
|
|
||||||
func getCwd() (string, error) {
|
func getCwds() (string, string, error) {
|
||||||
cwd, err := os.Getwd()
|
cwd, err := os.Getwd()
|
||||||
if err != nil {
|
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()
|
homedir, err := os.UserHomeDir()
|
||||||
if err != nil {
|
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 {
|
if cwd == homedir {
|
||||||
return "~/", nil
|
return cwd, "~/" nil
|
||||||
}
|
}
|
||||||
if strings.HasPrefix(cwd, homedir) {
|
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) {
|
func BuildHistoryEntry(args []string) (*data.HistoryEntry, error) {
|
||||||
@ -91,11 +91,12 @@ func BuildHistoryEntry(args []string) (*data.HistoryEntry, error) {
|
|||||||
entry.LocalUsername = user.Username
|
entry.LocalUsername = user.Username
|
||||||
|
|
||||||
// cwd
|
// cwd
|
||||||
cwd, err := getCwd()
|
cwd, relativeCwd, err := getCwds()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to build history entry: %v", err)
|
return nil, fmt.Errorf("failed to build history entry: %v", err)
|
||||||
}
|
}
|
||||||
entry.CurrentWorkingDirectory = cwd
|
entry.CurrentWorkingDirectory = cwd
|
||||||
|
entry.HomeRelativeCurrentWorkingDirectory = relativeCwd
|
||||||
|
|
||||||
// start time
|
// start time
|
||||||
seconds, err := parseCrossPlatformInt(args[5])
|
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("hostname = ?", entry.Hostname)
|
||||||
tx = tx.Where("command = ?", entry.Command)
|
tx = tx.Where("command = ?", entry.Command)
|
||||||
tx = tx.Where("current_working_directory = ?", entry.CurrentWorkingDirectory)
|
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("exit_code = ?", entry.ExitCode)
|
||||||
tx = tx.Where("start_time = ?", entry.StartTime)
|
tx = tx.Where("start_time = ?", entry.StartTime)
|
||||||
tx = tx.Where("end_time = ?", entry.EndTime)
|
tx = tx.Where("end_time = ?", entry.EndTime)
|
||||||
@ -256,7 +258,7 @@ func DisplayResults(results []*data.HistoryEntry) {
|
|||||||
for _, result := range results {
|
for _, result := range results {
|
||||||
timestamp := result.StartTime.Format("Jan 2 2006 15:04:05 MST")
|
timestamp := result.StartTime.Format("Jan 2 2006 15:04:05 MST")
|
||||||
duration := result.EndTime.Sub(result.StartTime).Round(time.Millisecond).String()
|
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()
|
tbl.Print()
|
||||||
|
@ -52,6 +52,7 @@ func TestBuildHistoryEntry(t *testing.T) {
|
|||||||
if !strings.HasPrefix(entry.CurrentWorkingDirectory, "/") && !strings.HasPrefix(entry.CurrentWorkingDirectory, "~/") {
|
if !strings.HasPrefix(entry.CurrentWorkingDirectory, "/") && !strings.HasPrefix(entry.CurrentWorkingDirectory, "~/") {
|
||||||
t.Fatalf("history entry has unexpected cwd: %v", entry.CurrentWorkingDirectory)
|
t.Fatalf("history entry has unexpected cwd: %v", entry.CurrentWorkingDirectory)
|
||||||
}
|
}
|
||||||
|
// TODO: test relative_cwd
|
||||||
if entry.Command != "ls /foo" {
|
if entry.Command != "ls /foo" {
|
||||||
t.Fatalf("history entry has unexpected command: %v", entry.Command)
|
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, "~/") {
|
if !strings.HasPrefix(entry.CurrentWorkingDirectory, "/") && !strings.HasPrefix(entry.CurrentWorkingDirectory, "~/") {
|
||||||
t.Fatalf("history entry has unexpected cwd: %v", entry.CurrentWorkingDirectory)
|
t.Fatalf("history entry has unexpected cwd: %v", entry.CurrentWorkingDirectory)
|
||||||
}
|
}
|
||||||
|
// TODO: test relative_cwd
|
||||||
if entry.Command != "ls /foo" {
|
if entry.Command != "ls /foo" {
|
||||||
t.Fatalf("history entry has unexpected command: %v", entry.Command)
|
t.Fatalf("history entry has unexpected command: %v", entry.Command)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user