Compare commits

...

No commits in common. "master" and "take-2" have entirely different histories.

5 changed files with 125 additions and 260 deletions

View File

@ -1,36 +0,0 @@
package main
import (
"strings"
"time"
)
type Command struct {
rowid int64
Args string
Times []int64
}
func (c Command) String() string {
return c.Args
}
/*
* Transitional method to import from the ruby implementation
*/
func CommandFromLineImport(line string) (*Command, error) {
line_chunks := strings.SplitN(strings.TrimSpace(line), " ", 3)
t, err := time.Parse(ShellTime, strings.Join(line_chunks[0:2], " "))
if err != nil {
return nil, err
}
if len(line_chunks) < 3 {
return &Command{Time: t}, nil
}
return &Command{
Args: strings.TrimSpace(line_chunks[2], " "),
Time: t,
}, nil
}

181
db.go
View File

@ -1,181 +0,0 @@
package main
import (
"database/sql"
"fmt"
"os"
_ "code.google.com/p/gosqlite/sqlite3" // registers sqlite
)
type Database struct {
conn *sql.DB
}
func (db *Database) Close() error {
return db.conn.Close()
}
func (db Database) GetCommand(commandStr string) (*Command, error) {
rows, err := db.conn.Query("SELECT rowid, command FROM commands WHERE command = ?;", commandStr)
if err != nil {
return nil, err
}
var (
rowid int64
command string
)
for rows.Next() {
err = rows.Scan(&rowid, &command)
if err != nil {
rows.Close()
return nil, err
}
}
rows.Close()
if rowid == 0 {
return nil, nil
}
cmd := Command{rowid: rowid, Args: command}
if rows, err = db.conn.Query("SELECT time FROM history WHERE command_id = ?;", rowid); err != nil {
return nil, err
}
for rows.Next() {
var t int64
if err = rows.Scan(&t); err != nil {
rows.Close()
return nil, err
}
cmd.Times = append(cmd.Times, t)
}
rows.Close()
return &cmd, nil
}
func (db *Database) InsertCommand(cmdTime int64, command string) error {
var (
cmd *Command
err error
)
// will be
if cmd, err = db.GetCommand(command); err != nil {
return err
}
fmt.Printf("%#v\n", cmd)
insertCommand, err := db.conn.Prepare("INSERT INTO commands (command) VALUES (?);")
if err != nil {
return err
}
insertTime, err := db.conn.Prepare("INSERT INTO history (command_id, time) VALUES (?,?);")
if err != nil {
return err
}
tx, err := db.conn.Begin()
if err != nil {
return err
}
var (
commandId int64
hasTime bool
)
if cmd == nil {
res, err := tx.Stmt(insertCommand).Exec(command)
if err != nil {
tx.Rollback()
return err
}
commandId, err = res.LastInsertId()
if err != nil {
tx.Rollback()
return err
}
cmd = &Command{rowid: commandId, Args: command}
} else {
commandId = cmd.rowid
}
for i := range cmd.Times {
if cmd.Times[i] == cmdTime {
hasTime = true
}
}
if !hasTime {
if _, err = tx.Stmt(insertTime).Exec(commandId, cmdTime); err != nil {
tx.Rollback()
return err
}
}
if err := tx.Commit(); err != nil {
return err
}
return nil
}
func NewDatabase(path string) (*Database, error) {
initDb := false
stat, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
initDb = true
} else {
return nil, err
}
}
if stat != nil && stat.Size() == 0 {
initDb = true
}
conn, err := sql.Open("sqlite3", path)
if err != nil {
return nil, err
}
db := &Database{conn: conn}
if initDb {
if err := initializeDatabase(db); err != nil {
return nil, err
}
}
return db, nil
}
func initializeDatabase(db *Database) error {
if _, err := db.conn.Exec(createCommandsTable); err != nil {
return err
}
if _, err := db.conn.Exec(createHistoryTable); err != nil {
return err
}
if _, err := db.conn.Exec(createIndexes); err != nil {
return err
}
return nil
}
const (
//id int NOT NULL PRIMARY KEY,
createCommandsTable = `
CREATE TABLE IF NOT EXISTS commands (
command text NOT NULL
); `
//id text NOT NULL PRIMARY KEY,
createHistoryTable = `
CREATE TABLE IF NOT EXISTS history (
"command_id" int NOT NULL,
"time" bigint NOT NULL,
CONSTRAINT "command_fk" FOREIGN KEY ("command_id") REFERENCES "commands" ("id")
); `
createIndexes = `
CREATE UNIQUE INDEX IF NOT EXISTS "commands_ix" ON "commands" (command);
`
)

74
h.go Normal file
View File

@ -0,0 +1,74 @@
package main
import (
"bufio"
"errors"
"fmt"
"go-history"
"os"
"strings"
"time"
)
var (
emptyEntryError = errors.New("Empty Entry")
parseError = errors.New("Parse Error")
)
// 1969-12-31 19:00:00 ruby -rubygems -I./lib -r warbler/web_server -r warbler/local_web_server -S warble --trace
/*
Wed Dec 31 19:00:00 -0500 EST 1969
Wed Dec 31 19:00:00 -0500 EST 1969 rake doc
*/
func parseLine(line string) (history.Entry, error) {
chunks := strings.SplitN(strings.Trim(line, " \n"), " ", 8)
if len(chunks) == 7 {
return history.Entry{}, emptyEntryError
} else if len(chunks) == 8 {
t, err := time.Parse("1969-01-02 19:00:00", strings.Join(chunks[0:1], " "))
if err != nil {
return history.Entry{}, err
}
return history.Entry{Time: t, Command: chunks[7]}, nil
}
return history.Entry{}, parseError
}
func parseCustomFile(filename string) (entries []history.Entry, err error) {
file, err := os.Open(filename)
if err != nil {
return
}
buf_r := bufio.NewReader(file)
for {
line, err := buf_r.ReadString('\n')
if err != nil {
return entries, err
}
entry, err := parseLine(line)
if err == emptyEntryError {
continue
} else if err != nil {
return entries, err
}
entries = append(entries, entry)
}
return
}
func main() {
fmt.Println(parseCustomFile(os.Args[1]))
return
for _, arg := range os.Args[1:] {
entries, err := history.ParseBashHistory(arg)
if err != nil {
fmt.Println(err)
}
for i := range entries {
fmt.Printf("%#v\n", entries[i])
}
}
}

43
main.go
View File

@ -1,43 +0,0 @@
package main
import (
"flag"
"fmt"
//"github.com/vbatts/go-gdbm"
"os"
"path"
)
var (
loadNew = true
flBashHistoryFile = flag.String("hist", path.Join(os.Getenv("HOME"), ".bash_history"), "path to the bash history file")
flDb = flag.String("db", path.Join(os.Getenv("HOME"), ".shell_history.db"), "path to database")
flList = flag.Bool("l", false, "list all commands stored")
flQuiet = flag.Bool("q", false, "less output")
)
func main() {
flag.Parse()
db, err := NewDatabase(*flDb)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
defer db.Close()
if *flList {
loadNew = false
}
fmt.Println(db)
if loadNew {
// TODO parse the bash history and load each
if err := db.InsertCommand(0, "ls -lh"); err != nil {
fmt.Fprintln(os.Stderr, err)
}
if err := db.InsertCommand(2, "ls -lh"); err != nil {
fmt.Fprintln(os.Stderr, err)
}
}
}

51
persistentdb.go Normal file
View File

@ -0,0 +1,51 @@
package main
import (
"encoding/json"
"fmt"
"github.com/vbatts/go-gdbm"
"os"
)
func main() {
db, err := gdbm.Open("foo.db", "c")
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
defer db.Close()
k, err := db.FirstKey()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
for {
v, err := db.Fetch(k)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
c := Command{}
err = json.Unmarshal([]byte(v), &c)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
fmt.Printf("#v\n", c)
k, err = db.NextKey(k)
if err == gdbm.NoError {
break
} else if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
}
type Command struct {
Cmd string `json:"cmd"`
Times []int64 `json:"time"`
}