mirror of
https://github.com/quitesimpleorg/hs9001.git
synced 2024-11-22 09:47:51 +01:00
search: Allow searching for cwd
This commit is contained in:
parent
250af52750
commit
595595c4cb
29
main.go
29
main.go
@ -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)
|
||||||
|
Loading…
Reference in New Issue
Block a user