96 lines
2.0 KiB
Go
96 lines
2.0 KiB
Go
package client
|
|
|
|
import (
|
|
"net/url"
|
|
"strings"
|
|
)
|
|
|
|
type Repository struct {
|
|
urlobj *url.URL
|
|
user, pass string
|
|
base string
|
|
}
|
|
|
|
func NewRepository(rawrepo string) (*Repository, error) {
|
|
repo := &Repository{}
|
|
if !strings.Contains(rawrepo, "://") {
|
|
rawrepo = "https://" + rawrepo
|
|
}
|
|
urlobj, err := url.Parse(rawrepo)
|
|
if err != nil {
|
|
return repo, err
|
|
}
|
|
if urlobj.User != nil {
|
|
repo.user = urlobj.User.Username()
|
|
repo.pass, _ = urlobj.User.Password()
|
|
urlobj.User = nil
|
|
}
|
|
repo.urlobj = urlobj
|
|
repo.base = repo.urlobj.Path
|
|
repo.urlobj.Path = "/"
|
|
repo.urlobj = urlobj
|
|
|
|
return repo, err
|
|
}
|
|
|
|
func (repo *Repository) Manifest(tag string) string {
|
|
curl := repo.urlobj.JoinPath("/v2", repo.base, "/manifests", tag)
|
|
return curl.String()
|
|
}
|
|
|
|
func (repo *Repository) Blob(digest string) string {
|
|
curl := repo.urlobj.JoinPath("/v2", repo.base, "/blobs", digest)
|
|
return curl.String()
|
|
}
|
|
|
|
func (repo *Repository) Upload() string {
|
|
curl := repo.urlobj.JoinPath("/v2", repo.base, "/blobs/uploads/")
|
|
return curl.String()
|
|
}
|
|
|
|
func (repo *Repository) Patch(loc string) (string, error) {
|
|
var curl *url.URL
|
|
var out string
|
|
var err error
|
|
if isUUID(loc) {
|
|
curl = repo.urlobj.JoinPath("/v2/", repo.base, "/blobs/uploads/", loc)
|
|
return curl.String(), nil
|
|
}
|
|
if strings.Contains(loc, "://") {
|
|
curl, err = url.Parse(loc)
|
|
if err != nil {
|
|
return out, err
|
|
}
|
|
} else {
|
|
curl = repo.urlobj.JoinPath(loc)
|
|
}
|
|
out = curl.String()
|
|
return out, err
|
|
}
|
|
|
|
func (repo *Repository) Put(loc, digest string) (string, error) {
|
|
var curl *url.URL
|
|
var out string
|
|
var err error
|
|
|
|
if isUUID(loc) {
|
|
curl = repo.urlobj.JoinPath("/v2/", repo.base, "/blobs/uploads/", loc)
|
|
} else if strings.Contains(loc, "://") {
|
|
curl, err = url.Parse(loc)
|
|
if err != nil {
|
|
return out, err
|
|
}
|
|
} else {
|
|
curl = repo.urlobj.JoinPath(loc)
|
|
}
|
|
query := curl.Query()
|
|
query.Set("digest", digest)
|
|
curl.RawQuery = query.Encode()
|
|
out = curl.String()
|
|
return out, err
|
|
}
|
|
|
|
func (repo *Repository) Userinfo() (string, string) {
|
|
return repo.user, repo.pass
|
|
}
|