Add 'delete' subcommand to delete matching records from log

This commit is contained in:
lawl 2021-04-05 10:29:50 +02:00
parent 8477ba5bfe
commit b02911c9b4

28
main.go
View File

@ -77,6 +77,21 @@ func search(conn *sql.DB, q string) {
} }
} }
func delete(conn *sql.DB, q string) {
queryStmt := "DELETE FROM history WHERE command LIKE ?"
_, err := conn.Exec(queryStmt, "%"+q+"%")
if err != nil {
log.Panic(err)
}
_, err = conn.Exec("VACUUM")
if err != nil {
log.Panic(err)
}
}
func add(conn *sql.DB, cmd string) { func add(conn *sql.DB, cmd string) {
user := os.Getenv("USER") user := os.Getenv("USER")
hostname, err := os.Hostname() hostname, err := os.Hostname()
@ -126,6 +141,7 @@ func printUsage() {
func main() { func main() {
addCmd := flag.NewFlagSet("add", flag.ExitOnError) addCmd := flag.NewFlagSet("add", flag.ExitOnError)
searchCmd := flag.NewFlagSet("search", flag.ExitOnError) searchCmd := flag.NewFlagSet("search", flag.ExitOnError)
deleteCmd := flag.NewFlagSet("delete", flag.ExitOnError)
if len(os.Args) < 1 { if len(os.Args) < 1 {
printUsage() printUsage()
@ -169,6 +185,18 @@ func main() {
q := strings.Join(args, " ") q := strings.Join(args, " ")
search(conn, q) search(conn, q)
os.Exit(23) os.Exit(23)
case "delete":
deleteCmd.Parse(globalargs)
args := deleteCmd.Args()
if len(args) < 1 {
fmt.Fprint(os.Stderr, "Error: You need to provide a search query for records to delete")
}
q := strings.Join(args, " ")
delete(conn, q)
//we do not want to leak what we just deleted :^)
os.Exit(23)
case "init": case "init":
err := os.MkdirAll(filepath.Dir(databaseLocation()), 0755) err := os.MkdirAll(filepath.Dir(databaseLocation()), 0755)
if err != nil { if err != nil {