gosh/h.go

75 lines
1.5 KiB
Go

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