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
t.Save(&buf)
id, err := repo.PutRaw(khepri.TypeRef, buf.Bytes())
id, err := repo.PutRaw(khepri.TYPE_BLOB, buf.Bytes())
if err != nil {
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 {
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 {
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 {
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 {
return err
}
@ -111,7 +111,7 @@ func commandRestore(repo *khepri.DirRepository, args []string) error {
id, err := khepri.ParseID(args[0])
if err != nil {
errmsg(1, "invalid id %q: %v", args[0], err)
errx(1, "invalid id %q: %v", args[0], err)
}
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"`
}
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' {
format += "\n"
}
@ -58,17 +58,17 @@ func main() {
f, ok := commands[cmd]
if !ok {
errmsg(1, "unknown command: %q\n", cmd)
errx(1, "unknown command: %q\n", cmd)
}
repo, err := khepri.NewDirRepository(Opts.Repo)
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:])
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
const (
TypeUnknown = iota
TypeBlob
TypeRef
TYPE_BLOB = iota
TYPE_REF
)
func NewTypeFromString(s string) Type {
switch s {
case "blob":
return TypeBlob
return TYPE_BLOB
case "ref":
return TypeRef
return TYPE_REF
}
panic(fmt.Sprintf("unknown type %q", s))
@ -57,9 +56,9 @@ func NewTypeFromString(s string) Type {
func (t Type) String() string {
switch t {
case TypeBlob:
case TYPE_BLOB:
return "blob"
case TypeRef:
case TYPE_REF:
return "ref"
}
@ -153,9 +152,9 @@ func (r *DirRepository) Put(t Type, reader io.Reader) (ID, error) {
// Construct directory for given Type.
func (r *DirRepository) dir(t Type) string {
switch t {
case TypeBlob:
case TYPE_BLOB:
return path.Join(r.path, blobPath)
case TypeRef:
case TYPE_REF:
return path.Join(r.path, refPath)
}
@ -175,7 +174,7 @@ func (r *DirRepository) PutFile(path string) (ID, error) {
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.

View File

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