src.dualinventive.com/go/devsim/repository/git/repository.go

78 lines
1.9 KiB
Go

package git
import (
"encoding/json"
"gopkg.in/src-d/go-git.v4/plumbing/transport"
"src.dualinventive.com/go/devsim/repository"
)
// Repository is a GIT repository
type Repository struct {
manager *manager
}
// Static check
var _ repository.Repository = &Repository{}
// newRepository creates a new repository. The uri is the location of the remote repository. The path is the location
// to clone the repository in. AuthMethod is the authorization that is used for GIT.
func newRepository(uri string, path string, authMethod transport.AuthMethod) (*Repository, error) {
m, err := newManager(uri, path, authMethod)
if err != nil {
return nil, err
}
r := &Repository{
manager: m,
}
return r, nil
}
// Versions return the known versions
func (g *Repository) Versions() ([]string, error) {
return g.manager.versions()
}
// Poll updates the internal cache
func (g *Repository) Poll() error {
return g.manager.fetch()
}
// Name returns the name of the repository
func (g *Repository) Name() string {
return g.manager.uri
}
// URI returns the URI of the repository
func (g *Repository) URI() string {
return g.manager.uri
}
// Key returns the key based on the given version. When the version is not found ErrNotFound is returned.
func (g *Repository) Key(version string) (string, error) {
return g.manager.key(version)
}
// Template returns the template based on the given key.
// To get a key use Key. When the key is not found ErrNotFound is returned.
func (g *Repository) Template(key string) (repository.Template, error) {
return g.manager.template(key)
}
// MarshalJSON marshals the repository data into a JSON string
func (g *Repository) MarshalJSON() ([]byte, error) {
type structure struct {
URI string `json:"uri"`
Versions []string `json:"versions"`
}
var s structure
s.URI = g.URI()
versions, err := g.Versions()
if err != nil {
return nil, err
}
s.Versions = versions
return json.Marshal(&s)
}