diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2016-08-31 19:54:49 +0200 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2016-08-31 19:54:49 +0200 |
commit | a82239a71a2cfc7a55af253d11c28fcec73a3039 (patch) | |
tree | cb8e363f6317d04fc24e8bc5ea610bbf5131592b /examples | |
parent | 587d380585c08e9b5f839d06df0f0c7bd32218cf (diff) | |
download | go-git-a82239a71a2cfc7a55af253d11c28fcec73a3039.tar.gz |
example: improvements on the storage example
Diffstat (limited to 'examples')
-rw-r--r-- | examples/storage/main.go | 91 | ||||
-rw-r--r-- | examples/storage/storage.go | 8 |
2 files changed, 54 insertions, 45 deletions
diff --git a/examples/storage/main.go b/examples/storage/main.go index e6ccc89..7587812 100644 --- a/examples/storage/main.go +++ b/examples/storage/main.go @@ -2,77 +2,84 @@ package main import ( "fmt" - "io" "os" - "time" + "strings" "github.com/aerospike/aerospike-client-go" + "github.com/fatih/color" "gopkg.in/src-d/go-git.v4" ) func main() { + checkArgs() + action := os.Args[1] url := os.Args[2] + + // Aerospike client to be used by the custom storage client, err := aerospike.NewClient("127.0.0.1", 3000) - if err != nil { - panic(err) - } + checkIfError(err) + // New instance of the custom aerospike storage, all the objects, + // references and configuration is saved to aerospike s, err := NewAerospikeStorage(client, "test", url) - if err != nil { - panic(err) - } + checkIfError(err) + // A new repository instance using as storage the custom implementation r, err := git.NewRepository(s) - if err != nil { - panic(err) - } + checkIfError(err) - switch os.Args[1] { + switch action { case "clone": clone(r, url) - case "list": - list(r) + case "log": + log(r) default: panic("unknown option") } } func clone(r *git.Repository, url string) { - fmt.Printf("Cloning %q ...\n", os.Args[2]) - start := time.Now() + // Clone the given repository, all the objects, references and + // configuration sush as remotes, are save into the Aerospike database. + // > git clone <url> + color.Blue("git clone %s", url) + err := r.Clone(&git.CloneOptions{URL: url}) + checkIfError(err) +} - if err := r.Clone(&git.CloneOptions{URL: url}); err != nil { - panic(err) +func log(r *git.Repository) { + // Prints the history of the repository starting in the current HEAD, the + // objects are retrieved from Aerospike database. + // > git log --oneline + color.Blue("git log --oneline") + + ref, err := r.Head() + checkIfError(err) + commit, err := r.Commit(ref.Hash()) + checkIfError(err) + commits, err := commit.History() + checkIfError(err) + + for _, c := range commits { + hash := c.Hash.String() + line := strings.Split(c.Message, "\n") + fmt.Println(hash[:7], line[0]) } - - fmt.Printf("Time elapsed %s\n", time.Since(start)) } -func list(r *git.Repository) { - fmt.Printf("Listing commits from %q ...\n", os.Args[1]) - - iter, err := r.Commits() - if err != nil { - panic(err) +func checkIfError(err error) { + if err == nil { + return } - defer iter.Close() - - var count int - for { - //the commits are not shorted in any special order - commit, err := iter.Next() - if err != nil { - if err == io.EOF { - break - } - panic(err) - } + color.Red("error: %s", err) + os.Exit(1) +} - count++ - fmt.Println(commit) +func checkArgs() { + if len(os.Args) < 3 { + color.Cyan("Usage: %s <clone|log> <url>", os.Args[0]) + os.Exit(1) } - - fmt.Printf("Total number of commits %d\n", count) } diff --git a/examples/storage/storage.go b/examples/storage/storage.go index 2231f95..48b3dcd 100644 --- a/examples/storage/storage.go +++ b/examples/storage/storage.go @@ -2,7 +2,6 @@ package main import ( "encoding/json" - "fmt" "io" "io/ioutil" @@ -108,7 +107,11 @@ func (s *AerospikeObjectStorage) Get(t core.ObjectType, h core.Hash) (core.Objec return nil, err } - return nil, core.ErrObjectNotFound + if rec == nil { + return nil, core.ErrObjectNotFound + } + + return objectFromRecord(rec, t) } func (s *AerospikeObjectStorage) Iter(t core.ObjectType) (core.ObjectIter, error) { @@ -125,7 +128,6 @@ func (s *AerospikeObjectStorage) Iter(t core.ObjectType) (core.ObjectIter, error func (s *AerospikeObjectStorage) keyFromObject(h core.Hash, t core.ObjectType, ) (*aerospike.Key, error) { - fmt.Println(t.String()) return aerospike.NewKey(s.ns, t.String(), h.String()) } |