2021-03-20 19:39:18 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-03-20 20:47:05 +01:00
|
|
|
"bufio"
|
2021-04-05 12:49:13 +02:00
|
|
|
"container/list"
|
2021-03-20 19:39:18 +01:00
|
|
|
"database/sql"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
2021-03-20 20:18:27 +01:00
|
|
|
"path/filepath"
|
2021-03-20 19:39:18 +01:00
|
|
|
"regexp"
|
2021-06-05 19:59:47 +02:00
|
|
|
"strconv"
|
2021-03-22 19:43:53 +01:00
|
|
|
"strings"
|
2021-04-05 14:00:51 +02:00
|
|
|
"time"
|
2021-03-20 20:18:27 +01:00
|
|
|
|
2021-06-05 19:59:47 +02:00
|
|
|
"hs9001/liner"
|
|
|
|
|
2021-04-05 14:00:51 +02:00
|
|
|
"github.com/tj/go-naturaldate"
|
2021-03-20 20:18:27 +01:00
|
|
|
_ "modernc.org/sqlite"
|
2021-03-20 19:39:18 +01:00
|
|
|
)
|
|
|
|
|
2021-04-05 12:49:13 +02:00
|
|
|
type HistoryEntry struct {
|
2021-04-05 14:00:51 +02:00
|
|
|
id uint32
|
|
|
|
cmd string
|
|
|
|
cwd string
|
|
|
|
hostname string
|
|
|
|
user string
|
2021-05-16 18:01:48 +02:00
|
|
|
retval int
|
2021-04-05 14:00:51 +02:00
|
|
|
timestamp time.Time
|
2021-04-05 12:49:13 +02:00
|
|
|
}
|
|
|
|
|
2021-05-16 18:38:33 +02:00
|
|
|
var GitTag string
|
|
|
|
var GitCommit string
|
|
|
|
|
2021-03-20 20:18:27 +01:00
|
|
|
func databaseLocation() string {
|
2021-03-21 10:38:12 +01:00
|
|
|
envOverride := os.Getenv("HS9001_DB_PATH")
|
|
|
|
if envOverride != "" {
|
|
|
|
return envOverride
|
|
|
|
}
|
2021-03-20 20:18:27 +01:00
|
|
|
return filepath.Join(xdgOrFallback("XDG_DATA_HOME", filepath.Join(os.Getenv("HOME"), ".local/share")), "hs9001/db.sqlite")
|
|
|
|
}
|
|
|
|
|
|
|
|
func createConnection() *sql.DB {
|
2021-03-20 19:39:18 +01:00
|
|
|
|
2021-03-20 20:18:27 +01:00
|
|
|
db, err := sql.Open("sqlite", databaseLocation())
|
2021-03-20 19:39:18 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
2021-04-05 11:44:07 +02:00
|
|
|
|
2021-03-20 19:39:18 +01:00
|
|
|
return db
|
|
|
|
}
|
|
|
|
|
2021-03-20 20:47:05 +01:00
|
|
|
func initDatabase(conn *sql.DB) {
|
2021-03-20 20:18:27 +01:00
|
|
|
queryStmt := "CREATE TABLE history(id INTEGER PRIMARY KEY, command varchar(512), timestamp datetime DEFAULT current_timestamp, user varchar(25), hostname varchar(32));\n" +
|
|
|
|
"CREATE VIEW count_by_date AS SELECT COUNT(id), STRFTIME('%Y-%m-%d', timestamp) FROM history GROUP BY strftime('%Y-%m-%d', timestamp)"
|
|
|
|
|
|
|
|
_, err := conn.Exec(queryStmt)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-05 11:44:07 +02:00
|
|
|
func migrateDatabase(conn *sql.DB, currentVersion int) {
|
|
|
|
|
2021-04-05 11:54:09 +02:00
|
|
|
migrations := []string{
|
2021-05-16 18:01:48 +02:00
|
|
|
"ALTER TABLE history ADD COLUMN workdir varchar(4096) DEFAULT ''",
|
|
|
|
"ALTER TABLE history ADD COLUMN retval integer DEFAULT -9001",
|
2021-09-28 18:10:53 +02:00
|
|
|
"ALTER TABLE history ADD COLUMN unix_tmp integer",
|
|
|
|
"UPDATE history SET unix_tmp = strftime('%s', timestamp)",
|
|
|
|
"DROP VIEW count_by_date",
|
|
|
|
"ALTER TABLE history DROP COLUMN timestamp",
|
|
|
|
"ALTER TABLE history RENAME COLUMN unix_tmp TO timestamp",
|
2021-04-05 11:54:09 +02:00
|
|
|
}
|
2021-04-05 11:44:07 +02:00
|
|
|
|
|
|
|
if !(len(migrations) > currentVersion) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := conn.Exec("BEGIN;")
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
for _, m := range migrations[currentVersion:] {
|
|
|
|
_, err := conn.Exec(m)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
setDBVersion(conn, len(migrations))
|
|
|
|
|
|
|
|
_, err = conn.Exec("END;")
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func fetchDBVersion(conn *sql.DB) int {
|
|
|
|
rows, err := conn.Query("PRAGMA user_version;")
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
rows.Next()
|
|
|
|
var res int
|
|
|
|
rows.Scan(&res)
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
func setDBVersion(conn *sql.DB, ver int) {
|
|
|
|
_, err := conn.Exec(fmt.Sprintf("PRAGMA user_version=%d", ver))
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-16 18:01:48 +02:00
|
|
|
func NewHistoryEntry(cmd string, retval int) HistoryEntry {
|
2021-04-05 13:00:27 +02:00
|
|
|
wd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
hostname, err := os.Hostname()
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
return HistoryEntry{
|
2021-04-05 14:00:51 +02:00
|
|
|
user: os.Getenv("USER"),
|
|
|
|
hostname: hostname,
|
|
|
|
cmd: cmd,
|
|
|
|
cwd: wd,
|
|
|
|
timestamp: time.Now(),
|
2021-05-16 18:01:48 +02:00
|
|
|
retval: retval,
|
2021-04-05 13:00:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-20 20:47:05 +01:00
|
|
|
func importFromStdin(conn *sql.DB) {
|
|
|
|
scanner := bufio.NewScanner(os.Stdin)
|
|
|
|
|
|
|
|
_, err := conn.Exec("BEGIN;")
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for scanner.Scan() {
|
2021-05-16 18:01:48 +02:00
|
|
|
entry := NewHistoryEntry(scanner.Text(), -9001)
|
2021-04-05 13:00:27 +02:00
|
|
|
entry.cwd = ""
|
2021-04-05 14:00:51 +02:00
|
|
|
entry.timestamp = time.Unix(0, 0)
|
2021-04-05 13:00:27 +02:00
|
|
|
add(conn, entry)
|
2021-03-20 20:47:05 +01:00
|
|
|
}
|
2021-03-20 19:39:18 +01:00
|
|
|
|
2021-03-20 20:47:05 +01:00
|
|
|
_, err = conn.Exec("END;")
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-05 19:59:47 +02:00
|
|
|
type searchopts struct {
|
|
|
|
command *string
|
|
|
|
workdir *string
|
|
|
|
after *time.Time
|
|
|
|
before *time.Time
|
|
|
|
retval *int
|
2021-08-19 21:30:04 +02:00
|
|
|
order *string
|
|
|
|
limit *int
|
2021-06-05 19:59:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func search(conn *sql.DB, opts searchopts) list.List {
|
|
|
|
args := make([]interface{}, 0)
|
2021-05-16 18:01:48 +02:00
|
|
|
var sb strings.Builder
|
2021-09-28 18:10:53 +02:00
|
|
|
sb.WriteString("SELECT id, command, workdir, user, hostname, retval, timestamp ")
|
2021-05-16 18:01:48 +02:00
|
|
|
sb.WriteString("FROM history ")
|
2021-06-05 19:59:47 +02:00
|
|
|
sb.WriteString("WHERE 1=1 ") //1=1 so we can append as many AND foo as we want, or none
|
|
|
|
|
|
|
|
if opts.command != nil {
|
|
|
|
sb.WriteString("AND command LIKE ? ")
|
|
|
|
args = append(args, opts.command)
|
|
|
|
}
|
|
|
|
if opts.workdir != nil {
|
|
|
|
sb.WriteString("AND workdir LIKE ? ")
|
|
|
|
args = append(args, opts.workdir)
|
|
|
|
}
|
|
|
|
if opts.after != nil {
|
2021-09-28 18:10:53 +02:00
|
|
|
sb.WriteString("AND timestamp > ? ")
|
2021-06-05 19:59:47 +02:00
|
|
|
args = append(args, opts.after.Unix())
|
|
|
|
}
|
|
|
|
if opts.before != nil {
|
2021-09-28 18:10:53 +02:00
|
|
|
sb.WriteString("AND timestamp < ? ")
|
2021-06-05 19:59:47 +02:00
|
|
|
args = append(args, opts.before.Unix())
|
|
|
|
}
|
|
|
|
if opts.retval != nil {
|
2021-05-16 18:01:48 +02:00
|
|
|
sb.WriteString("AND retval = ? ")
|
2021-06-05 19:59:47 +02:00
|
|
|
args = append(args, opts.retval)
|
2021-05-16 18:01:48 +02:00
|
|
|
}
|
2021-06-05 19:59:47 +02:00
|
|
|
sb.WriteString("ORDER BY timestamp ")
|
2021-08-19 21:30:04 +02:00
|
|
|
if opts.order != nil {
|
|
|
|
sb.WriteString(*opts.order)
|
|
|
|
sb.WriteRune(' ')
|
|
|
|
} else {
|
|
|
|
sb.WriteString("ASC ")
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.limit != nil {
|
|
|
|
sb.WriteString("LIMIT ")
|
|
|
|
sb.WriteString(strconv.Itoa(*opts.limit))
|
|
|
|
sb.WriteRune(' ')
|
|
|
|
}
|
2021-05-16 18:01:48 +02:00
|
|
|
|
|
|
|
queryStmt := sb.String()
|
2021-06-05 19:59:47 +02:00
|
|
|
|
2021-05-16 18:01:48 +02:00
|
|
|
rows, err := conn.Query(queryStmt, args...)
|
2021-03-20 19:39:18 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
|
2021-04-05 12:49:13 +02:00
|
|
|
var result list.List
|
2021-04-05 11:44:07 +02:00
|
|
|
defer rows.Close()
|
2021-03-20 19:39:18 +01:00
|
|
|
for rows.Next() {
|
2021-04-05 12:49:13 +02:00
|
|
|
var entry HistoryEntry
|
2021-09-25 12:49:50 +02:00
|
|
|
var timestamp int64
|
|
|
|
err = rows.Scan(&entry.id, &entry.cmd, &entry.cwd, &entry.user, &entry.hostname, &entry.retval, ×tamp)
|
2021-03-20 19:39:18 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
2021-09-25 12:49:50 +02:00
|
|
|
entry.timestamp = time.Unix(timestamp, 0)
|
2021-04-05 12:49:13 +02:00
|
|
|
result.PushBack(&entry)
|
2021-03-20 19:39:18 +01:00
|
|
|
}
|
2021-04-05 12:49:13 +02:00
|
|
|
return result
|
2021-03-20 19:39:18 +01:00
|
|
|
}
|
|
|
|
|
2021-04-05 12:49:13 +02:00
|
|
|
func delete(conn *sql.DB, entryId uint32) {
|
|
|
|
queryStmt := "DELETE FROM history WHERE id = ?"
|
2021-04-05 10:29:50 +02:00
|
|
|
|
2021-04-05 12:49:13 +02:00
|
|
|
_, err := conn.Exec(queryStmt, entryId)
|
2021-04-05 10:29:50 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-05 13:15:20 +02:00
|
|
|
func add(conn *sql.DB, entry HistoryEntry) {
|
2021-09-28 18:10:53 +02:00
|
|
|
stmt, err := conn.Prepare("INSERT INTO history (user, command, hostname, workdir, timestamp, retval) VALUES (?, ?, ?, ?, ?,?)")
|
2021-03-20 19:39:18 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
|
2021-05-16 18:01:48 +02:00
|
|
|
_, err = stmt.Exec(entry.user, entry.cmd, entry.hostname, entry.cwd, entry.timestamp.Unix(), entry.retval)
|
2021-03-20 19:39:18 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-03-20 20:18:27 +01:00
|
|
|
func xdgOrFallback(xdg string, fallback string) string {
|
|
|
|
dir := os.Getenv(xdg)
|
|
|
|
if dir != "" {
|
|
|
|
if ok, err := exists(dir); ok && err == nil {
|
|
|
|
return dir
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return fallback
|
|
|
|
}
|
|
|
|
|
|
|
|
func exists(path string) (bool, error) {
|
|
|
|
_, err := os.Stat(path)
|
|
|
|
if err == nil {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return false, err
|
|
|
|
}
|
2021-03-20 19:39:18 +01:00
|
|
|
|
2021-03-22 19:50:27 +01:00
|
|
|
func printUsage() {
|
2021-06-05 15:42:33 +02:00
|
|
|
fmt.Fprintf(os.Stderr, "Usage: ./hs9001 <add/search/import/nolog/bash-enable>\n")
|
2021-03-22 19:50:27 +01:00
|
|
|
}
|
|
|
|
|
2021-03-20 19:39:18 +01:00
|
|
|
func main() {
|
2021-04-05 10:16:40 +02:00
|
|
|
addCmd := flag.NewFlagSet("add", flag.ExitOnError)
|
|
|
|
searchCmd := flag.NewFlagSet("search", flag.ExitOnError)
|
2021-03-20 20:22:38 +01:00
|
|
|
|
2021-04-05 10:43:54 +02:00
|
|
|
if len(os.Args) < 2 {
|
2021-03-22 19:50:27 +01:00
|
|
|
printUsage()
|
2021-03-20 20:22:38 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-05 10:16:40 +02:00
|
|
|
cmd := os.Args[1]
|
|
|
|
globalargs := os.Args[2:]
|
2021-03-20 19:39:18 +01:00
|
|
|
|
2021-04-05 11:44:07 +02:00
|
|
|
var conn *sql.DB
|
|
|
|
ok, _ := exists(databaseLocation())
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
err := os.MkdirAll(filepath.Dir(databaseLocation()), 0755)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
conn = createConnection()
|
|
|
|
initDatabase(conn)
|
|
|
|
} else {
|
|
|
|
conn = createConnection()
|
|
|
|
}
|
|
|
|
|
|
|
|
migrateDatabase(conn, fetchDBVersion(conn))
|
2021-03-20 20:47:05 +01:00
|
|
|
|
2021-04-05 10:16:40 +02:00
|
|
|
switch cmd {
|
2021-06-05 17:41:58 +02:00
|
|
|
case "bash-ctrlr":
|
|
|
|
line := liner.NewLiner()
|
|
|
|
defer line.Close()
|
|
|
|
line.SetCtrlCAborts(true)
|
2021-06-05 19:59:47 +02:00
|
|
|
line.SetHistoryProvider(&history{conn: conn})
|
|
|
|
line.SetMultiLineMode(true)
|
|
|
|
|
|
|
|
rdlineline := os.Getenv("READLINE_LINE")
|
|
|
|
rdlinepos := os.Getenv("READLINE_POS")
|
|
|
|
rdlineposint, _ := strconv.Atoi(rdlinepos)
|
2021-06-05 17:41:58 +02:00
|
|
|
|
2021-06-05 19:59:47 +02:00
|
|
|
if name, err := line.PromptWithSuggestionReverse("", rdlineline, rdlineposint); err == nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "%s\n", name)
|
2021-06-05 17:41:58 +02:00
|
|
|
}
|
|
|
|
|
2021-06-05 15:42:33 +02:00
|
|
|
case "bash-enable":
|
|
|
|
fmt.Printf(`
|
|
|
|
if [ -n "$PS1" ] ; then
|
|
|
|
PROMPT_COMMAND='hs9001 add -ret $? "$(history 1)"'
|
2021-06-05 19:59:47 +02:00
|
|
|
bind -x '"\C-r": " READLINE_LINE=$(hs9001 bash-ctrlr 3>&1 1>&2 2>&3) READLINE_POINT=0"'
|
2021-06-05 15:42:33 +02:00
|
|
|
fi
|
2021-08-08 13:22:08 +02:00
|
|
|
alias hs='hs9001 search'
|
2021-06-05 15:42:33 +02:00
|
|
|
`)
|
|
|
|
case "bash-disable":
|
|
|
|
fmt.Printf("unset PROMPT_COMMAND\n")
|
2021-04-05 10:16:40 +02:00
|
|
|
case "add":
|
|
|
|
var ret int
|
|
|
|
addCmd.IntVar(&ret, "ret", 0, "Return value of the command to add")
|
|
|
|
addCmd.Parse(globalargs)
|
|
|
|
args := addCmd.Args()
|
|
|
|
|
2021-03-21 12:45:26 +01:00
|
|
|
if ret == 23 { // 23 is our secret do not log status code
|
|
|
|
return
|
|
|
|
}
|
2021-04-05 10:16:40 +02:00
|
|
|
if len(args) < 1 {
|
2021-03-20 19:39:18 +01:00
|
|
|
fmt.Fprint(os.Stderr, "Error: You need to provide the command to be added")
|
|
|
|
|
|
|
|
}
|
2021-04-05 10:16:40 +02:00
|
|
|
historycmd := args[0]
|
2021-06-05 15:05:18 +02:00
|
|
|
var rgx = regexp.MustCompile(`\s+\d+\s+(.*)`)
|
2021-03-20 19:39:18 +01:00
|
|
|
rs := rgx.FindStringSubmatch(historycmd)
|
2021-03-21 10:43:06 +01:00
|
|
|
if len(rs) == 2 {
|
2021-05-16 18:01:48 +02:00
|
|
|
add(conn, NewHistoryEntry(rs[1], ret))
|
2021-03-21 10:43:06 +01:00
|
|
|
}
|
2021-04-05 13:15:20 +02:00
|
|
|
case "search":
|
|
|
|
fallthrough
|
2021-04-05 12:49:13 +02:00
|
|
|
case "delete":
|
2021-04-05 13:15:20 +02:00
|
|
|
var workDir string
|
2021-06-05 19:59:47 +02:00
|
|
|
var afterTime string
|
|
|
|
var beforeTime string
|
2021-05-16 17:13:29 +02:00
|
|
|
var distinct bool = true
|
2021-09-25 12:49:50 +02:00
|
|
|
var today bool = false
|
2021-05-16 18:01:48 +02:00
|
|
|
var retVal int
|
2021-06-05 19:59:47 +02:00
|
|
|
searchCmd.StringVar(&workDir, "cwd", "", "Search only within this workdir")
|
|
|
|
searchCmd.StringVar(&afterTime, "after", "", "Start searching from this timeframe")
|
|
|
|
searchCmd.StringVar(&beforeTime, "before", "", "End searching from this timeframe")
|
2021-05-16 17:13:29 +02:00
|
|
|
searchCmd.BoolVar(&distinct, "distinct", true, "Remove consecutive duplicate commands from output")
|
2021-09-25 12:49:50 +02:00
|
|
|
searchCmd.BoolVar(&today, "today", false, "Search only today's entries. Overrides --after")
|
2021-05-16 18:01:48 +02:00
|
|
|
searchCmd.IntVar(&retVal, "ret", -9001, "Only query commands that returned with this exit code. -9001=all (default)")
|
2021-04-05 10:16:40 +02:00
|
|
|
searchCmd.Parse(globalargs)
|
|
|
|
|
2021-04-05 13:15:20 +02:00
|
|
|
args := searchCmd.Args()
|
2021-04-05 10:16:40 +02:00
|
|
|
|
2021-06-05 19:59:47 +02:00
|
|
|
q := strings.Join(args, " ")
|
2021-04-05 14:21:33 +02:00
|
|
|
|
2021-06-05 19:59:47 +02:00
|
|
|
opts := searchopts{}
|
2021-08-19 21:30:04 +02:00
|
|
|
o := "ASC"
|
|
|
|
opts.order = &o
|
2021-06-05 19:59:47 +02:00
|
|
|
if q != "" {
|
|
|
|
cmd := "%" + q + "%"
|
|
|
|
opts.command = &cmd
|
2021-04-05 14:21:33 +02:00
|
|
|
}
|
2021-06-05 19:59:47 +02:00
|
|
|
if workDir != "" {
|
|
|
|
wd, err := filepath.Abs(workDir)
|
2021-06-05 15:05:18 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "Failed parse working directory path: %s\n", err.Error())
|
|
|
|
}
|
2021-06-05 19:59:47 +02:00
|
|
|
opts.workdir = &wd
|
2021-06-05 14:28:02 +02:00
|
|
|
}
|
2021-09-25 12:49:50 +02:00
|
|
|
|
|
|
|
if today {
|
|
|
|
afterTime = "today"
|
|
|
|
}
|
|
|
|
|
2021-06-05 19:59:47 +02:00
|
|
|
if afterTime != "" {
|
|
|
|
afterTimestamp, err := naturaldate.Parse(afterTime, time.Now())
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "Failed to convert time string: %s\n", err.Error())
|
|
|
|
}
|
|
|
|
opts.after = &afterTimestamp
|
|
|
|
}
|
|
|
|
if beforeTime != "" {
|
|
|
|
beforeTimestamp, err := naturaldate.Parse(beforeTime, time.Now())
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "Failed to convert time string: %s\n", err.Error())
|
|
|
|
}
|
|
|
|
opts.before = &beforeTimestamp
|
|
|
|
}
|
|
|
|
if retVal != -9001 {
|
|
|
|
opts.retval = &retVal
|
|
|
|
}
|
|
|
|
results := search(conn, opts)
|
2021-04-05 12:49:13 +02:00
|
|
|
|
2021-05-16 17:13:29 +02:00
|
|
|
previousCmd := ""
|
2022-09-27 17:01:20 +02:00
|
|
|
previousReturn := -1
|
2021-09-25 12:14:07 +02:00
|
|
|
|
|
|
|
fi, err := os.Stdout.Stat()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
//Don't print colors if output is piped
|
|
|
|
printColors := true
|
|
|
|
if (fi.Mode() & os.ModeCharDevice) == 0 {
|
|
|
|
printColors = false
|
|
|
|
}
|
|
|
|
|
2021-04-05 12:49:13 +02:00
|
|
|
for e := results.Front(); e != nil; e = e.Next() {
|
|
|
|
entry, ok := e.Value.(*HistoryEntry)
|
|
|
|
if !ok {
|
|
|
|
log.Panic("Failed to retrieve entries")
|
|
|
|
}
|
2022-09-27 17:01:20 +02:00
|
|
|
if !distinct || !(previousCmd == entry.cmd && previousReturn == entry.retval) {
|
2021-09-25 12:14:07 +02:00
|
|
|
prefix := ""
|
|
|
|
postfix := ""
|
|
|
|
if printColors && entry.retval != 0 {
|
|
|
|
prefix = "\033[38;5;88m"
|
|
|
|
postfix = "\033[0m"
|
|
|
|
}
|
|
|
|
fmt.Printf("%s%s%s\n", prefix, entry.cmd, postfix)
|
2021-05-16 17:13:29 +02:00
|
|
|
}
|
|
|
|
previousCmd = entry.cmd
|
2022-09-27 17:01:20 +02:00
|
|
|
previousReturn = entry.retval
|
2021-04-05 10:29:50 +02:00
|
|
|
}
|
|
|
|
|
2021-04-05 12:49:13 +02:00
|
|
|
if cmd == "delete" {
|
2021-04-05 11:44:07 +02:00
|
|
|
|
2021-04-05 12:49:13 +02:00
|
|
|
_, err := conn.Exec("BEGIN;")
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for e := results.Front(); e != nil; e = e.Next() {
|
|
|
|
entry, ok := e.Value.(*HistoryEntry)
|
|
|
|
if !ok {
|
|
|
|
log.Panic("Failed to retrieve entries")
|
|
|
|
}
|
|
|
|
delete(conn, entry.id)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = conn.Exec("END;")
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = conn.Exec("VACUUM")
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
os.Exit(23)
|
2021-04-05 10:16:40 +02:00
|
|
|
case "import":
|
2021-03-20 20:47:05 +01:00
|
|
|
importFromStdin(conn)
|
2021-05-16 18:38:33 +02:00
|
|
|
case "version":
|
|
|
|
fmt.Fprintf(os.Stdout, "Git Tag: %s\nGit Commit: %s\n", GitTag, GitCommit)
|
2021-04-05 10:16:40 +02:00
|
|
|
default:
|
|
|
|
fmt.Fprintf(os.Stderr, "Error: Unknown subcommand '%s' supplied\n\n", cmd)
|
2021-03-22 19:50:27 +01:00
|
|
|
printUsage()
|
|
|
|
return
|
2021-03-20 19:39:18 +01:00
|
|
|
}
|
2021-04-05 10:16:40 +02:00
|
|
|
|
2021-03-20 19:39:18 +01:00
|
|
|
}
|