Skip to content

Commit

Permalink
added exit code as field in command table
Browse files Browse the repository at this point in the history
  • Loading branch information
TM90 committed Apr 11, 2024
1 parent 8263c92 commit 389b17c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
2 changes: 1 addition & 1 deletion logbook.bash
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

alias logbook=${LOGBOOK_PATH}/logbook/logbook
export LOGBOOK_UUID=$(uuidgen -r)
PROMPT_COMMAND='logbook add --command "$(history 1)" -uuid $LOGBOOK_UUID'
PROMPT_COMMAND='logbook add --exit-code=$? --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)"'
13 changes: 7 additions & 6 deletions logbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ type logbookEntry struct {
dbId int
commandName string
historyId int
exitCode 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.uuid, &result.execTime)
err := row.Scan(&result.dbId, &result.commandName, &result.historyId, &result.exitCode, &result.uuid, &result.execTime)
if err != nil {
result.historyId = -1
result.commandName = ""
Expand All @@ -40,7 +41,7 @@ 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.uuid, &result.execTime)
rows.Scan(&result.dbId, &result.commandName, &result.historyId, &result.exitCode, &result.uuid, &result.execTime)
fmt.Printf("%s\n", result.commandName)
}
}
Expand All @@ -57,13 +58,12 @@ func main() {
_ = flag.NewFlagSet("init", flag.ExitOnError)
addCmd := flag.NewFlagSet("add", flag.ExitOnError)
cmdName := addCmd.String("command", "", "command")
exitCode := addCmd.Int("exit-code", 0, "exit-code")
uuid := addCmd.String("uuid", "", "uuid")
rawQueryCmd := flag.NewFlagSet("raw_query", flag.ExitOnError)
rawQuery := rawQueryCmd.String("query", "", "query")
switch os.Args[1] {
case "init":
fmt.Println("Creating sqlite3 db at: " + os.Getenv("HOME") + "/.logbook/logbook.sql")

os.MkdirAll(os.Getenv("HOME")+"/.logbook", 0775)
os.Create(os.Getenv("HOME") + "/.logbook/logbook.sql")
db, err := logbookOpen()
Expand All @@ -77,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, uuid TEXT, 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, exit_code INTEGER, uuid TEXT, exec_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL)")
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
Expand All @@ -93,8 +93,9 @@ func main() {
addCmd.Parse(os.Args[2:])
command, id := parseHistoryItem(*cmdName)
lastEntry := logbookRetrieveLastEntry(db)
fmt.Println(*exitCode)
if !(id == lastEntry.historyId && command == lastEntry.commandName) {
_, err = db.Exec("INSERT INTO command (command_name, history_id, uuid) VALUES (?, ?, ?)", command, id, *uuid)
_, err = db.Exec("INSERT INTO command (command_name, history_id, exit_code, uuid) VALUES (?, ?, ?, ?)", command, id, *exitCode, *uuid)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
Expand Down

0 comments on commit 389b17c

Please sign in to comment.