github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/cmd/juju/romulus/resolve.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package cmd 5 6 import ( 7 "net/http" 8 "net/url" 9 10 "github.com/juju/errors" 11 "gopkg.in/juju/charm.v6-unstable" 12 "gopkg.in/juju/charmrepo.v2-unstable" 13 "gopkg.in/juju/charmrepo.v2-unstable/csclient" 14 ) 15 16 // CharmResolver interface defines the functionality to resolve a charm URL. 17 type CharmResolver interface { 18 // Resolve resolves the charm URL. 19 Resolve(visitWebPage func(*url.URL) error, client *http.Client, charmURL string) (string, error) 20 } 21 22 // CharmStoreResolver implements the CharmResolver interface. 23 type CharmStoreResolver struct { 24 csURL string 25 } 26 27 // NewCharmStoreResolver creates a new charm store resolver. 28 func NewCharmStoreResolver() *CharmStoreResolver { 29 return &CharmStoreResolver{ 30 csURL: csclient.ServerURL, 31 } 32 } 33 34 // Resolve implements the CharmResolver interface. 35 func (r *CharmStoreResolver) Resolve(visitWebPage func(*url.URL) error, client *http.Client, charmURL string) (string, error) { 36 repo := charmrepo.NewCharmStore(charmrepo.NewCharmStoreParams{ 37 URL: r.csURL, 38 HTTPClient: client, 39 VisitWebPage: visitWebPage, 40 }) 41 42 curl, err := charm.ParseURL(charmURL) 43 if err != nil { 44 return "", errors.Annotate(err, "could not parse charm url") 45 } 46 // ignore local charm urls 47 if curl.Schema == "local" { 48 return charmURL, nil 49 } 50 resolvedURL, _, err := repo.Resolve(curl) 51 if err != nil { 52 return "", errors.Trace(err) 53 } 54 return resolvedURL.String(), nil 55 }