Skip to content

Commit

Permalink
changed command db table to include a uuid which is set in logbook.bash
Browse files Browse the repository at this point in the history
  • Loading branch information
TM90 committed Apr 9, 2024
1 parent fd14f28 commit 8263c92
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
4 changes: 3 additions & 1 deletion logbook.bash
Original file line number Diff line number Diff line change
@@ -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)"'
20 changes: 11 additions & 9 deletions logbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"flag"
"fmt"
"os"
"strconv"
"strings"
"time"

Expand All @@ -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
Expand All @@ -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
}
Expand All @@ -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] {
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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())
Expand Down

0 comments on commit 8263c92

Please sign in to comment.