Rename types and errmsg function

This commit is contained in:
Alexander Neumann 2014-08-03 15:16:56 +02:00
parent 5cbd1d0090
commit fbd33636f0
5 changed files with 22 additions and 23 deletions

View file

@ -70,7 +70,7 @@ func archive_dir(repo *khepri.DirRepository, path string) (khepri.ID, error) {
var buf bytes.Buffer var buf bytes.Buffer
t.Save(&buf) t.Save(&buf)
id, err := repo.PutRaw(khepri.TypeRef, buf.Bytes()) id, err := repo.PutRaw(khepri.TYPE_BLOB, buf.Bytes())
if err != nil { if err != nil {
log.Printf("error saving tree to repo: %v", err) log.Printf("error saving tree to repo: %v", err)

View file

@ -13,7 +13,7 @@ import (
func restore_file(repo *khepri.DirRepository, node khepri.Node, target string) error { func restore_file(repo *khepri.DirRepository, node khepri.Node, target string) error {
fmt.Printf(" restore file %q\n", target) fmt.Printf(" restore file %q\n", target)
rd, err := repo.Get(khepri.TypeBlob, node.Content) rd, err := repo.Get(khepri.TYPE_BLOB, node.Content)
if err != nil { if err != nil {
return err return err
} }
@ -49,7 +49,7 @@ func restore_file(repo *khepri.DirRepository, node khepri.Node, target string) e
func restore_dir(repo *khepri.DirRepository, id khepri.ID, target string) error { func restore_dir(repo *khepri.DirRepository, id khepri.ID, target string) error {
fmt.Printf(" restore dir %q\n", target) fmt.Printf(" restore dir %q\n", target)
rd, err := repo.Get(khepri.TypeRef, id) rd, err := repo.Get(khepri.TYPE_REF, id)
if err != nil { if err != nil {
return err return err
} }
@ -111,7 +111,7 @@ func commandRestore(repo *khepri.DirRepository, args []string) error {
id, err := khepri.ParseID(args[0]) id, err := khepri.ParseID(args[0])
if err != nil { if err != nil {
errmsg(1, "invalid id %q: %v", args[0], err) errx(1, "invalid id %q: %v", args[0], err)
} }
target := args[1] target := args[1]

View file

@ -14,7 +14,7 @@ var Opts struct {
Repo string `short:"r" long:"repo" description:"Repository directory to backup to/restor from"` Repo string `short:"r" long:"repo" description:"Repository directory to backup to/restor from"`
} }
func errmsg(code int, format string, data ...interface{}) { func errx(code int, format string, data ...interface{}) {
if len(format) > 0 && format[len(format)-1] != '\n' { if len(format) > 0 && format[len(format)-1] != '\n' {
format += "\n" format += "\n"
} }
@ -58,17 +58,17 @@ func main() {
f, ok := commands[cmd] f, ok := commands[cmd]
if !ok { if !ok {
errmsg(1, "unknown command: %q\n", cmd) errx(1, "unknown command: %q\n", cmd)
} }
repo, err := khepri.NewDirRepository(Opts.Repo) repo, err := khepri.NewDirRepository(Opts.Repo)
if err != nil { if err != nil {
errmsg(1, "unable to create/open repo: %v", err) errx(1, "unable to create/open repo: %v", err)
} }
err = f(repo, args[1:]) err = f(repo, args[1:])
if err != nil { if err != nil {
errmsg(1, "error executing command %q: %v", cmd, err) errx(1, "error executing command %q: %v", cmd, err)
} }
} }

View file

@ -39,17 +39,16 @@ type DirRepository struct {
type Type int type Type int
const ( const (
TypeUnknown = iota TYPE_BLOB = iota
TypeBlob TYPE_REF
TypeRef
) )
func NewTypeFromString(s string) Type { func NewTypeFromString(s string) Type {
switch s { switch s {
case "blob": case "blob":
return TypeBlob return TYPE_BLOB
case "ref": case "ref":
return TypeRef return TYPE_REF
} }
panic(fmt.Sprintf("unknown type %q", s)) panic(fmt.Sprintf("unknown type %q", s))
@ -57,9 +56,9 @@ func NewTypeFromString(s string) Type {
func (t Type) String() string { func (t Type) String() string {
switch t { switch t {
case TypeBlob: case TYPE_BLOB:
return "blob" return "blob"
case TypeRef: case TYPE_REF:
return "ref" return "ref"
} }
@ -153,9 +152,9 @@ func (r *DirRepository) Put(t Type, reader io.Reader) (ID, error) {
// Construct directory for given Type. // Construct directory for given Type.
func (r *DirRepository) dir(t Type) string { func (r *DirRepository) dir(t Type) string {
switch t { switch t {
case TypeBlob: case TYPE_BLOB:
return path.Join(r.path, blobPath) return path.Join(r.path, blobPath)
case TypeRef: case TYPE_REF:
return path.Join(r.path, refPath) return path.Join(r.path, refPath)
} }
@ -175,7 +174,7 @@ func (r *DirRepository) PutFile(path string) (ID, error) {
return nil, err return nil, err
} }
return r.Put(TypeBlob, f) return r.Put(TYPE_BLOB, f)
} }
// PutRaw saves a []byte's content to the repository and returns the ID. // PutRaw saves a []byte's content to the repository and returns the ID.

View file

@ -19,10 +19,10 @@ var TestStrings = []struct {
t khepri.Type t khepri.Type
data string data string
}{ }{
{"c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2", khepri.TypeBlob, "foobar"}, {"c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2", khepri.TYPE_BLOB, "foobar"},
{"248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1", khepri.TypeBlob, "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"}, {"248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1", khepri.TYPE_BLOB, "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"},
{"cc5d46bdb4991c6eae3eb739c9c8a7a46fe9654fab79c47b4fe48383b5b25e1c", khepri.TypeRef, "foo/bar"}, {"cc5d46bdb4991c6eae3eb739c9c8a7a46fe9654fab79c47b4fe48383b5b25e1c", khepri.TYPE_REF, "foo/bar"},
{"4e54d2c721cbdb730f01b10b62dec622962b36966ec685880effa63d71c808f2", khepri.TypeBlob, "foo/../../baz"}, {"4e54d2c721cbdb730f01b10b62dec622962b36966ec685880effa63d71c808f2", khepri.TYPE_BLOB, "foo/../../baz"},
} }
func setupRepo() (*khepri.DirRepository, error) { func setupRepo() (*khepri.DirRepository, error) {
@ -99,7 +99,7 @@ func TestRepository(t *testing.T) {
} }
// list ids // list ids
for _, tpe := range []khepri.Type{khepri.TypeBlob, khepri.TypeRef} { for _, tpe := range []khepri.Type{khepri.TYPE_BLOB, khepri.TYPE_REF} {
IDs := khepri.IDs{} IDs := khepri.IDs{}
for _, test := range TestStrings { for _, test := range TestStrings {
if test.t == tpe { if test.t == tpe {