search: Allow searching for cwd

This commit is contained in:
Albert S. 2021-04-05 13:15:20 +02:00
parent 250af52750
commit 595595c4cb

37
main.go
View File

@ -16,11 +16,11 @@ import (
) )
type HistoryEntry struct { type HistoryEntry struct {
id uint32 id uint32
cmd string cmd string
cwd string cwd string
hostname string hostname string
user string user string
} }
func databaseLocation() string { func databaseLocation() string {
@ -110,10 +110,10 @@ func NewHistoryEntry(cmd string) HistoryEntry {
log.Panic(err) log.Panic(err)
} }
return HistoryEntry{ return HistoryEntry{
user : os.Getenv("USER"), user: os.Getenv("USER"),
hostname : hostname, hostname: hostname,
cmd : cmd, cmd: cmd,
cwd : wd, cwd: wd,
} }
} }
@ -137,10 +137,10 @@ func importFromStdin(conn *sql.DB) {
} }
} }
func search(conn *sql.DB, q string) list.List { func search(conn *sql.DB, q string, workdir string) list.List {
queryStmt := "SELECT id, command, workdir, user, hostname FROM history WHERE command LIKE ? ORDER BY timestamp ASC" queryStmt := "SELECT id, command, workdir, user, hostname FROM history WHERE command LIKE ? AND workdir LIKE ? ORDER BY timestamp ASC"
rows, err := conn.Query(queryStmt, "%"+q+"%") rows, err := conn.Query(queryStmt, q, workdir)
if err != nil { if err != nil {
log.Panic(err) log.Panic(err)
} }
@ -167,7 +167,7 @@ func delete(conn *sql.DB, entryId uint32) {
} }
} }
func add(conn *sql.DB, entry HistoryEntry ) { func add(conn *sql.DB, entry HistoryEntry) {
stmt, err := conn.Prepare("INSERT INTO history (user, command, hostname, workdir) VALUES (?, ?, ?, ?)") stmt, err := conn.Prepare("INSERT INTO history (user, command, hostname, workdir) VALUES (?, ?, ?, ?)")
if err != nil { if err != nil {
log.Panic(err) log.Panic(err)
@ -255,18 +255,17 @@ func main() {
if len(rs) == 2 { if len(rs) == 2 {
add(conn, NewHistoryEntry(rs[1])) add(conn, NewHistoryEntry(rs[1]))
} }
case "search": fallthrough; case "search":
fallthrough
case "delete": case "delete":
var workDir string
searchCmd.StringVar(&workDir, "workdir", "%", "Search only within this workdir")
searchCmd.Parse(globalargs) searchCmd.Parse(globalargs)
args := searchCmd.Args() args := searchCmd.Args()
if len(args) < 1 {
fmt.Fprint(os.Stderr, "Please provide the query\n")
os.Exit(1)
}
q := strings.Join(args, " ") q := strings.Join(args, " ")
results := search(conn, q) results := search(conn, "%"+q+"%", workDir)
for e := results.Front(); e != nil; e = e.Next() { for e := results.Front(); e != nil; e = e.Next() {
entry, ok := e.Value.(*HistoryEntry) entry, ok := e.Value.(*HistoryEntry)