This commit is contained in:
Vincent Batts 2014-10-16 14:01:07 -04:00
commit 2104c43aff
2 changed files with 125 additions and 0 deletions

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])
}
}
}

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"`
}