1

Improving command line support

This commit is contained in:
Alex Yatskov 2015-06-23 17:43:22 +09:00
parent 76a517d1a3
commit 2a4f274fbc
2 changed files with 54 additions and 29 deletions

View File

@ -23,6 +23,7 @@
package main
import (
"fmt"
"io/ioutil"
"path"
"path/filepath"
@ -40,16 +41,16 @@ type database struct {
vers verList
}
func newDatabase(dir string) (*database, error) {
func newDatabase(dir string, index int, writable bool) (*database, error) {
db := &database{base: dir}
if err := db.load(dir); err != nil {
if err := db.load(dir, index, writable); err != nil {
return nil, err
}
return db, nil
}
func (db *database) load(dir string) error {
func (db *database) load(dir string, index int, writable bool) error {
var err error
db.base, err = filepath.Abs(dir)
@ -57,8 +58,10 @@ func (db *database) load(dir string) error {
return err
}
if err := buildNewVersion(db.base); err != nil {
return err
if writable {
if err := buildNewVersion(db.base); err != nil {
return err
}
}
db.vers, err = db.buildVersions(db.base)
@ -66,6 +69,10 @@ func (db *database) load(dir string) error {
return err
}
if index >= 0 {
db.vers = db.vers[index:]
}
if lastVer := db.lastVersion(); lastVer != nil {
return lastVer.resolve()
}
@ -112,10 +119,10 @@ func (db *database) buildVersions(base string) (verList, error) {
sort.Sort(vers)
var parVer *version
var pv *version
for _, ver := range vers {
ver.parent = parVer
parVer = ver
ver.parent = pv
pv = ver
}
return vers, nil
@ -130,6 +137,12 @@ func (db *database) lastVersion() *version {
return db.vers[count-1]
}
func (db *database) dump() {
for index, ver := range db.vers {
fmt.Printf("version: %d\ttime: %s\n", index, ver.timestamp.String())
}
}
// FS
func (db *database) Root() (fs.Node, error) {
return db.lastVersion().root, nil

54
vfs.go
View File

@ -30,49 +30,61 @@ import (
"bazil.org/fuse"
"bazil.org/fuse/fs"
_ "bazil.org/fuse/fs/fstestutil"
// _ "bazil.org/fuse/fs/fstestutil"
)
func usage() {
fmt.Fprintf(os.Stderr, "Usage: %s [options] database mountpoint\n\n", os.Args[0])
fmt.Fprintf(os.Stderr, "Usage: %s [options] database [mountpoint]\n\n", os.Args[0])
fmt.Fprintf(os.Stderr, "Parameters:\n")
flag.PrintDefaults()
}
func main() {
version := flag.Int("version", -1, "version index (specify -1 for latest)")
flag.Usage = usage
flag.Parse()
if flag.NArg() != 2 {
if flag.NArg() < 1 {
usage()
os.Exit(2)
}
database := flag.Arg(0)
mountpoint := flag.Arg(1)
mount := flag.NArg() > 1
writable := mount && *version < 0
db, err := newDatabase(database)
db, err := newDatabase(flag.Arg(0), *version, writable)
if err != nil {
log.Fatal(err)
}
conn, err := fuse.Mount(mountpoint)
if err != nil {
log.Fatal(err)
}
defer conn.Close()
if mount {
var options []fuse.MountOption
if !writable {
options = append(options, fuse.ReadOnly())
}
err = fs.Serve(conn, db)
if err != nil {
log.Fatal(err)
}
conn, err := fuse.Mount(flag.Arg(1), options...)
if err != nil {
log.Fatal(err)
}
defer conn.Close()
<-conn.Ready
if err := conn.MountError; err != nil {
log.Fatal(err)
}
err = fs.Serve(conn, db)
if err != nil {
log.Fatal(err)
}
if err := db.save(); err != nil {
log.Fatal(err)
<-conn.Ready
if err := conn.MountError; err != nil {
log.Fatal(err)
}
if writable {
if err := db.save(); err != nil {
log.Fatal(err)
}
}
} else {
db.dump()
}
}