Compare commits

..

No commits in common. "216e59747cd1a99b173cf7fbff5b0dc0d0acfc1a" and "3b01d7b898346050003159a902e112797128755b" have entirely different histories.

2 changed files with 12 additions and 46 deletions

View File

@ -1,6 +0,0 @@
GIT_COMMIT=$(shell git rev-list -1 HEAD)
GIT_TAG=$(shell git tag --sort="-version:refname" | head -n 1)
all:
go build -ldflags "-X main.GitCommit=${GIT_COMMIT} -X main.GitTag=${GIT_TAG}"

52
main.go
View File

@ -23,13 +23,9 @@ type HistoryEntry struct {
cwd string cwd string
hostname string hostname string
user string user string
retval int
timestamp time.Time timestamp time.Time
} }
var GitTag string
var GitCommit string
func databaseLocation() string { func databaseLocation() string {
envOverride := os.Getenv("HS9001_DB_PATH") envOverride := os.Getenv("HS9001_DB_PATH")
if envOverride != "" { if envOverride != "" {
@ -61,8 +57,7 @@ func initDatabase(conn *sql.DB) {
func migrateDatabase(conn *sql.DB, currentVersion int) { func migrateDatabase(conn *sql.DB, currentVersion int) {
migrations := []string{ migrations := []string{
"ALTER TABLE history ADD COLUMN workdir varchar(4096) DEFAULT ''", "ALTER TABLE history add column workdir varchar(4096) DEFAULT ''",
"ALTER TABLE history ADD COLUMN retval integer DEFAULT -9001",
} }
if !(len(migrations) > currentVersion) { if !(len(migrations) > currentVersion) {
@ -108,7 +103,7 @@ func setDBVersion(conn *sql.DB, ver int) {
} }
} }
func NewHistoryEntry(cmd string, retval int) HistoryEntry { func NewHistoryEntry(cmd string) HistoryEntry {
wd, err := os.Getwd() wd, err := os.Getwd()
if err != nil { if err != nil {
log.Panic(err) log.Panic(err)
@ -123,7 +118,6 @@ func NewHistoryEntry(cmd string, retval int) HistoryEntry {
cmd: cmd, cmd: cmd,
cwd: wd, cwd: wd,
timestamp: time.Now(), timestamp: time.Now(),
retval: retval,
} }
} }
@ -136,7 +130,7 @@ func importFromStdin(conn *sql.DB) {
} }
for scanner.Scan() { for scanner.Scan() {
entry := NewHistoryEntry(scanner.Text(), -9001) entry := NewHistoryEntry(scanner.Text())
entry.cwd = "" entry.cwd = ""
entry.timestamp = time.Unix(0, 0) entry.timestamp = time.Unix(0, 0)
add(conn, entry) add(conn, entry)
@ -148,29 +142,10 @@ func importFromStdin(conn *sql.DB) {
} }
} }
func search(conn *sql.DB, q string, workdir string, beginTime time.Time, endTime time.Time, retval int) list.List { func search(conn *sql.DB, q string, workdir string, beginTime time.Time, endTime time.Time) list.List {
var sb strings.Builder queryStmt := "SELECT id, command, workdir, user, hostname FROM history WHERE timestamp BETWEEN datetime(?, 'unixepoch') AND datetime(?, 'unixepoch') AND command LIKE ? AND workdir LIKE ? ORDER BY timestamp ASC"
sb.WriteString("SELECT id, command, workdir, user, hostname, retval ")
sb.WriteString("FROM history ")
sb.WriteString("WHERE timestamp BETWEEN datetime(?, 'unixepoch') ")
sb.WriteString("AND datetime(?, 'unixepoch') ")
sb.WriteString("AND command LIKE ? ")
sb.WriteString("AND workdir LIKE ? ")
if retval != -9001 {
sb.WriteString("AND retval = ? ")
}
sb.WriteString("ORDER BY timestamp ASC ")
queryStmt := sb.String() rows, err := conn.Query(queryStmt, beginTime.Unix(), endTime.Unix(), q, workdir)
args := make([]interface{}, 0)
args = append(args, beginTime.Unix())
args = append(args, endTime.Unix())
args = append(args, q)
args = append(args, workdir)
if retval != -9001 {
args = append(args, retval)
}
rows, err := conn.Query(queryStmt, args...)
if err != nil { if err != nil {
log.Panic(err) log.Panic(err)
} }
@ -179,7 +154,7 @@ func search(conn *sql.DB, q string, workdir string, beginTime time.Time, endTime
defer rows.Close() defer rows.Close()
for rows.Next() { for rows.Next() {
var entry HistoryEntry var entry HistoryEntry
err = rows.Scan(&entry.id, &entry.cmd, &entry.cwd, &entry.user, &entry.hostname, &entry.retval) err = rows.Scan(&entry.id, &entry.cmd, &entry.cwd, &entry.user, &entry.hostname)
if err != nil { if err != nil {
log.Panic(err) log.Panic(err)
} }
@ -198,12 +173,12 @@ func delete(conn *sql.DB, entryId uint32) {
} }
func add(conn *sql.DB, entry HistoryEntry) { func add(conn *sql.DB, entry HistoryEntry) {
stmt, err := conn.Prepare("INSERT INTO history (user, command, hostname, workdir, timestamp, retval) VALUES (?, ?, ?, ?, datetime(?, 'unixepoch'),?)") stmt, err := conn.Prepare("INSERT INTO history (user, command, hostname, workdir, timestamp) VALUES (?, ?, ?, ?, datetime(?, 'unixepoch'))")
if err != nil { if err != nil {
log.Panic(err) log.Panic(err)
} }
_, err = stmt.Exec(entry.user, entry.cmd, entry.hostname, entry.cwd, entry.timestamp.Unix(), entry.retval) _, err = stmt.Exec(entry.user, entry.cmd, entry.hostname, entry.cwd, entry.timestamp.Unix())
if err != nil { if err != nil {
log.Panic(err) log.Panic(err)
} }
@ -283,7 +258,7 @@ func main() {
var rgx = regexp.MustCompile("\\s+\\d+\\s+(.*)") var rgx = regexp.MustCompile("\\s+\\d+\\s+(.*)")
rs := rgx.FindStringSubmatch(historycmd) rs := rgx.FindStringSubmatch(historycmd)
if len(rs) == 2 { if len(rs) == 2 {
add(conn, NewHistoryEntry(rs[1], ret)) add(conn, NewHistoryEntry(rs[1]))
} }
case "search": case "search":
fallthrough fallthrough
@ -292,12 +267,11 @@ func main() {
var beginTime string var beginTime string
var endTime string var endTime string
var distinct bool = true var distinct bool = true
var retVal int
searchCmd.StringVar(&workDir, "workdir", "%", "Search only within this workdir") searchCmd.StringVar(&workDir, "workdir", "%", "Search only within this workdir")
searchCmd.StringVar(&beginTime, "begin", "50 years ago", "Start searching from this timeframe") searchCmd.StringVar(&beginTime, "begin", "50 years ago", "Start searching from this timeframe")
searchCmd.StringVar(&endTime, "end", "now", "End searching from this timeframe") searchCmd.StringVar(&endTime, "end", "now", "End searching from this timeframe")
searchCmd.BoolVar(&distinct, "distinct", true, "Remove consecutive duplicate commands from output") searchCmd.BoolVar(&distinct, "distinct", true, "Remove consecutive duplicate commands from output")
searchCmd.IntVar(&retVal, "ret", -9001, "Only query commands that returned with this exit code. -9001=all (default)")
searchCmd.Parse(globalargs) searchCmd.Parse(globalargs)
@ -314,7 +288,7 @@ func main() {
} }
q := strings.Join(args, " ") q := strings.Join(args, " ")
results := search(conn, "%"+q+"%", workDir, beginTimestamp, endTimeStamp, retVal) results := search(conn, "%"+q+"%", workDir, beginTimestamp, endTimeStamp)
previousCmd := "" previousCmd := ""
for e := results.Front(); e != nil; e = e.Next() { for e := results.Front(); e != nil; e = e.Next() {
@ -357,8 +331,6 @@ func main() {
os.Exit(23) os.Exit(23)
case "import": case "import":
importFromStdin(conn) importFromStdin(conn)
case "version":
fmt.Fprintf(os.Stdout, "Git Tag: %s\nGit Commit: %s\n", GitTag, GitCommit)
default: default:
fmt.Fprintf(os.Stderr, "Error: Unknown subcommand '%s' supplied\n\n", cmd) fmt.Fprintf(os.Stderr, "Error: Unknown subcommand '%s' supplied\n\n", cmd)
printUsage() printUsage()