From 8263c92faadc3fcdb5cb49b5e97339ef8af8cdd0 Mon Sep 17 00:00:00 2001 From: TM90 Date: Tue, 9 Apr 2024 23:07:08 +0200 Subject: [PATCH] changed command db table to include a uuid which is set in logbook.bash --- logbook.bash | 4 +++- logbook.go | 20 +++++++++++--------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/logbook.bash b/logbook.bash index 29332b5..d672fc2 100755 --- a/logbook.bash +++ b/logbook.bash @@ -1,5 +1,7 @@ + alias logbook=${LOGBOOK_PATH}/logbook/logbook -PROMPT_COMMAND='logbook add -command "$(history 1)"' +export LOGBOOK_UUID=$(uuidgen -r) +PROMPT_COMMAND='logbook add --command "$(history 1)" -uuid $LOGBOOK_UUID' bind -m emacs-standard -x '"\C-r": xdotool type "$(logbook raw_query --query "SELECT * FROM command GROUP BY command_name ORDER BY id DESC" | fzf)"' bind -m vi-command -x '"\C-r": xdotool type "$(logbook raw_query --query "SELECT * FROM command GROUP BY command_name ORDER BY id DESC" | fzf)"' bind -m vi-insert -x '"\C-r": xdotool type "$(logbook raw_query --query "SELECT * FROM command GROUP BY command_name ORDER BY id DESC" | fzf)"' diff --git a/logbook.go b/logbook.go index fa8d990..6ac5d3a 100644 --- a/logbook.go +++ b/logbook.go @@ -5,6 +5,7 @@ import ( "flag" "fmt" "os" + "strconv" "strings" "time" @@ -19,16 +20,17 @@ func logbookOpen() (*sql.DB, error) { type logbookEntry struct { dbId int commandName string - historyId string + historyId int + uuid string execTime time.Time } func logbookRetrieveLastEntry(db *sql.DB) logbookEntry { var result logbookEntry row := db.QueryRow("SELECT * FROM command ORDER BY ID DESC LIMIT 1") - err := row.Scan(&result.dbId, &result.commandName, &result.historyId, &result.execTime) + err := row.Scan(&result.dbId, &result.commandName, &result.historyId, &result.uuid, &result.execTime) if err != nil { - result.historyId = "" + result.historyId = -1 result.commandName = "" } return result @@ -38,15 +40,15 @@ func logbookRetrieveLastEntry(db *sql.DB) logbookEntry { func printLogbookRows(rows *sql.Rows) { for rows.Next() { var result logbookEntry - rows.Scan(&result.dbId, &result.commandName, &result.historyId, &result.execTime) + rows.Scan(&result.dbId, &result.commandName, &result.historyId, &result.uuid, &result.execTime) fmt.Printf("%s\n", result.commandName) } } -func parseHistoryItem(raw_line string) (string, string) { +func parseHistoryItem(raw_line string) (string, int) { trimmedVal := strings.Trim(raw_line, " ") values := strings.Split(trimmedVal, " ") - id := values[0] + id, _ := strconv.Atoi(values[0]) command := strings.Trim(strings.Join(values[1:], " "), " ") return command, id } @@ -55,6 +57,7 @@ func main() { _ = flag.NewFlagSet("init", flag.ExitOnError) addCmd := flag.NewFlagSet("add", flag.ExitOnError) cmdName := addCmd.String("command", "", "command") + uuid := addCmd.String("uuid", "", "uuid") rawQueryCmd := flag.NewFlagSet("raw_query", flag.ExitOnError) rawQuery := rawQueryCmd.String("query", "", "query") switch os.Args[1] { @@ -74,7 +77,7 @@ func main() { fmt.Println(err) os.Exit(1) } - _, err = db.Exec("CREATE TABLE command (id INTEGER PRIMARY KEY AUTOINCREMENT, command_name TEXT, history_id INTEGER, exec_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL)") + _, err = db.Exec("CREATE TABLE command (id INTEGER PRIMARY KEY AUTOINCREMENT, command_name TEXT, history_id INTEGER, uuid TEXT, exec_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL)") if err != nil { fmt.Println(err.Error()) os.Exit(1) @@ -91,7 +94,7 @@ func main() { command, id := parseHistoryItem(*cmdName) lastEntry := logbookRetrieveLastEntry(db) if !(id == lastEntry.historyId && command == lastEntry.commandName) { - _, err = db.Exec("INSERT INTO command (command_name, history_id) VALUES (?, ?)", command, id) + _, err = db.Exec("INSERT INTO command (command_name, history_id, uuid) VALUES (?, ?, ?)", command, id, *uuid) if err != nil { fmt.Println(err.Error()) os.Exit(1) @@ -101,7 +104,6 @@ func main() { case "raw_query": rawQueryCmd.Parse(os.Args[2:]) - fmt.Printf("Query fo %s\n", *rawQuery) db, err := logbookOpen() if err != nil { fmt.Println(err.Error())