本文整理匯總了Golang中github.com/wallyworld/core/charm.URL類的典型用法代碼示例。如果您正苦於以下問題:Golang URL類的具體用法?Golang URL怎麽用?Golang URL使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了URL類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: DeleteCharm
// DeleteCharm deletes the charms matching url. If no revision is specified,
// all revisions of the charm are deleted.
func (s *Store) DeleteCharm(url *charm.URL) ([]*CharmInfo, error) {
logger.Debugf("deleting charm %s", url)
infos, err := s.getRevisions(url, 0)
if err != nil {
return nil, err
}
if len(infos) == 0 {
return nil, ErrNotFound
}
session := s.session.Copy()
defer session.Close()
var deleted []*CharmInfo
for _, info := range infos {
err := session.Charms().Remove(
bson.D{{"urls", url.WithRevision(-1)}, {"revision", info.Revision()}})
if err != nil {
logger.Errorf("failed to delete metadata for charm %s: %v", url, err)
return deleted, err
}
err = session.CharmFS().RemoveId(info.fileId)
if err != nil {
logger.Errorf("failed to delete GridFS file for charm %s: %v", url, err)
return deleted, err
}
deleted = append(deleted, info)
}
return deleted, err
}
示例2: AddBundle
func (br *bundleReader) AddBundle(c *gc.C, url *corecharm.URL, bundle charm.Bundle) charm.BundleInfo {
if br.bundles == nil {
br.bundles = map[string]charm.Bundle{}
}
br.bundles[url.String()] = bundle
return &bundleInfo{nil, url}
}
示例3: addCharmViaAPI
// addCharmViaAPI calls the appropriate client API calls to add the
// given charm URL to state. Also displays the charm URL of the added
// charm on stdout.
func addCharmViaAPI(client *api.Client, ctx *cmd.Context, curl *charm.URL, repo charm.Repository) (*charm.URL, error) {
if curl.Revision < 0 {
latest, err := charm.Latest(repo, curl)
if err != nil {
return nil, err
}
curl = curl.WithRevision(latest)
}
switch curl.Schema {
case "local":
ch, err := repo.Get(curl)
if err != nil {
return nil, err
}
stateCurl, err := client.AddLocalCharm(curl, ch)
if err != nil {
return nil, err
}
curl = stateCurl
case "cs":
err := client.AddCharm(curl)
if err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("unsupported charm URL schema: %q", curl.Schema)
}
ctx.Infof("Added charm %q to the environment.", curl)
return curl, nil
}
示例4: PutCharm
// PutCharm uploads the given charm to provider storage, and adds a
// state.Charm to the state. The charm is not uploaded if a charm with
// the same URL already exists in the state.
// If bumpRevision is true, the charm must be a local directory,
// and the revision number will be incremented before pushing.
func (conn *Conn) PutCharm(curl *charm.URL, repo charm.Repository, bumpRevision bool) (*state.Charm, error) {
if curl.Revision == -1 {
rev, err := charm.Latest(repo, curl)
if err != nil {
return nil, fmt.Errorf("cannot get latest charm revision: %v", err)
}
curl = curl.WithRevision(rev)
}
ch, err := repo.Get(curl)
if err != nil {
return nil, fmt.Errorf("cannot get charm: %v", err)
}
if bumpRevision {
chd, ok := ch.(*charm.Dir)
if !ok {
return nil, fmt.Errorf("cannot increment revision of charm %q: not a directory", curl)
}
if err = chd.SetDiskRevision(chd.Revision() + 1); err != nil {
return nil, fmt.Errorf("cannot increment revision of charm %q: %v", curl, err)
}
curl = curl.WithRevision(chd.Revision())
}
if sch, err := conn.State.Charm(curl); err == nil {
return sch, nil
}
return conn.addCharm(curl, ch)
}
示例5: storeManifest
// storeManifest stores, into dataPath, the supplied manifest for the supplied charm.
func (d *manifestDeployer) storeManifest(url *charm.URL, manifest set.Strings) error {
if err := os.MkdirAll(d.DataPath(manifestsDataPath), 0755); err != nil {
return err
}
name := charm.Quote(url.String())
path := filepath.Join(d.DataPath(manifestsDataPath), name)
return utils.WriteYaml(path, manifest.SortedValues())
}
示例6: PrepareLocalCharmUpload
// PrepareLocalCharmUpload must be called before a local charm is
// uploaded to the provider storage in order to create a charm
// document in state. It returns the chosen unique charm URL reserved
// in state for the charm.
//
// The url's schema must be "local" and it must include a revision.
func (st *State) PrepareLocalCharmUpload(curl *charm.URL) (chosenUrl *charm.URL, err error) {
// Perform a few sanity checks first.
if curl.Schema != "local" {
return nil, fmt.Errorf("expected charm URL with local schema, got %q", curl)
}
if curl.Revision < 0 {
return nil, fmt.Errorf("expected charm URL with revision, got %q", curl)
}
// Get a regex with the charm URL and no revision.
noRevURL := curl.WithRevision(-1)
curlRegex := "^" + regexp.QuoteMeta(noRevURL.String())
for attempt := 0; attempt < 3; attempt++ {
// Find the highest revision of that charm in state.
var docs []charmDoc
err = st.charms.Find(bson.D{{"_id", bson.D{{"$regex", curlRegex}}}}).Select(bson.D{{"_id", 1}}).All(&docs)
if err != nil {
return nil, err
}
// Find the highest revision.
maxRevision := -1
for _, doc := range docs {
if doc.URL.Revision > maxRevision {
maxRevision = doc.URL.Revision
}
}
// Respect the local charm's revision first.
chosenRevision := curl.Revision
if maxRevision >= chosenRevision {
// More recent revision exists in state, pick the next.
chosenRevision = maxRevision + 1
}
chosenUrl = curl.WithRevision(chosenRevision)
uploadedCharm := &charmDoc{
URL: chosenUrl,
PendingUpload: true,
}
ops := []txn.Op{{
C: st.charms.Name,
Id: uploadedCharm.URL,
Assert: txn.DocMissing,
Insert: uploadedCharm,
}}
// Run the transaction, and retry on abort.
if err = st.runTransaction(ops); err == txn.ErrAborted {
continue
} else if err != nil {
return nil, err
}
break
}
if err != nil {
return nil, ErrExcessiveContention
}
return chosenUrl, nil
}
示例7: Charm
// Charm returns the charm with the given URL.
func (st *State) Charm(curl *charm.URL) (*Charm, error) {
if curl == nil {
return nil, fmt.Errorf("charm url cannot be nil")
}
return &Charm{
st: st,
url: curl.String(),
}, nil
}
示例8: interpret
// interpret extracts from charmURL information relevant to both Latest and
// Get. The returned "base" is always the string representation of the
// unrevisioned part of charmURL; the "rev" wil be taken from the charmURL if
// available, and will otherwise be the revision of the latest charm in the
// store with the same "base".
func (s *MockCharmStore) interpret(charmURL *charm.URL) (base string, rev int) {
base, rev = charmURL.WithRevision(-1).String(), charmURL.Revision
if rev == -1 {
for candidate := range s.charms[base] {
if candidate > rev {
rev = candidate
}
}
}
return
}
示例9: addCharm
func (conn *Conn) addCharm(curl *charm.URL, ch charm.Charm) (*state.Charm, error) {
var f *os.File
name := charm.Quote(curl.String())
switch ch := ch.(type) {
case *charm.Dir:
var err error
if f, err = ioutil.TempFile("", name); err != nil {
return nil, err
}
defer os.Remove(f.Name())
defer f.Close()
err = ch.BundleTo(f)
if err != nil {
return nil, fmt.Errorf("cannot bundle charm: %v", err)
}
if _, err := f.Seek(0, 0); err != nil {
return nil, err
}
case *charm.Bundle:
var err error
if f, err = os.Open(ch.Path); err != nil {
return nil, fmt.Errorf("cannot read charm bundle: %v", err)
}
defer f.Close()
default:
return nil, fmt.Errorf("unknown charm type %T", ch)
}
digest, size, err := utils.ReadSHA256(f)
if err != nil {
return nil, err
}
if _, err := f.Seek(0, 0); err != nil {
return nil, err
}
stor := conn.Environ.Storage()
logger.Infof("writing charm to storage [%d bytes]", size)
if err := stor.Put(name, f, size); err != nil {
return nil, fmt.Errorf("cannot put charm: %v", err)
}
ustr, err := stor.URL(name)
if err != nil {
return nil, fmt.Errorf("cannot get storage URL for charm: %v", err)
}
u, err := url.Parse(ustr)
if err != nil {
return nil, fmt.Errorf("cannot parse storage URL: %v", err)
}
logger.Infof("adding charm to state")
sch, err := conn.State.AddCharm(ch, curl, u, digest)
if err != nil {
return nil, fmt.Errorf("cannot add charm: %v", err)
}
return sch, nil
}
示例10: handleEvent
func handleEvent(ctx *cmd.Context, curl *charm.URL, event *charm.EventResponse) error {
switch event.Kind {
case "published":
curlRev := curl.WithRevision(event.Revision)
logger.Infof("charm published at %s as %s", event.Time, curlRev)
fmt.Fprintln(ctx.Stdout, curlRev)
case "publish-error":
return fmt.Errorf("charm could not be published: %s", strings.Join(event.Errors, "; "))
default:
return fmt.Errorf("unknown event kind %q for charm %s", event.Kind, curl)
}
return nil
}
示例11: AddStoreCharmPlaceholder
// AddStoreCharmPlaceholder creates a charm document in state for the given charm URL which
// must reference a charm from the store. The charm document is marked as a placeholder which
// means that if the charm is to be deployed, it will need to first be uploaded to env storage.
func (st *State) AddStoreCharmPlaceholder(curl *charm.URL) (err error) {
// Perform sanity checks first.
if curl.Schema != "cs" {
return fmt.Errorf("expected charm URL with cs schema, got %q", curl)
}
if curl.Revision < 0 {
return fmt.Errorf("expected charm URL with revision, got %q", curl)
}
for attempt := 0; attempt < 3; attempt++ {
// See if the charm already exists in state and exit early if that's the case.
var doc charmDoc
err = st.charms.Find(bson.D{{"_id", curl.String()}}).Select(bson.D{{"_id", 1}}).One(&doc)
if err != nil && err != mgo.ErrNotFound {
return err
}
if err == nil {
return nil
}
// Delete all previous placeholders so we don't fill up the database with unused data.
var ops []txn.Op
ops, err = st.deleteOldPlaceholderCharmsOps(curl)
if err != nil {
return nil
}
// Add the new charm doc.
placeholderCharm := &charmDoc{
URL: curl,
Placeholder: true,
}
ops = append(ops, txn.Op{
C: st.charms.Name,
Id: placeholderCharm.URL.String(),
Assert: txn.DocMissing,
Insert: placeholderCharm,
})
// Run the transaction, and retry on abort.
if err = st.runTransaction(ops); err == txn.ErrAborted {
continue
} else if err != nil {
return err
}
break
}
if err != nil {
return ErrExcessiveContention
}
return nil
}
示例12: SetCharmURL
// SetCharmURL marks the unit as currently using the supplied charm URL.
// An error will be returned if the unit is dead, or the charm URL not known.
func (u *Unit) SetCharmURL(curl *charm.URL) error {
if curl == nil {
return fmt.Errorf("charm URL cannot be nil")
}
var result params.ErrorResults
args := params.EntitiesCharmURL{
Entities: []params.EntityCharmURL{
{Tag: u.tag, CharmURL: curl.String()},
},
}
err := u.st.call("SetCharmURL", args, &result)
if err != nil {
return err
}
return result.OneError()
}
示例13: serviceSetCharm1dot16
// serviceSetCharm1dot16 sets the charm for the given service in 1.16
// compatibility mode. Remove this when support for 1.16 is dropped.
func (c *Client) serviceSetCharm1dot16(service *state.Service, curl *charm.URL, force bool) error {
if curl.Schema != "cs" {
return fmt.Errorf(`charm url has unsupported schema %q`, curl.Schema)
}
if curl.Revision < 0 {
return fmt.Errorf("charm url must include revision")
}
err := c.AddCharm(params.CharmURL{curl.String()})
if err != nil {
return err
}
ch, err := c.api.state.Charm(curl)
if err != nil {
return err
}
return service.SetCharm(ch, force)
}
示例14: LatestPlaceholderCharm
// LatestPlaceholderCharm returns the latest charm described by the
// given URL but which is not yet deployed.
func (st *State) LatestPlaceholderCharm(curl *charm.URL) (*Charm, error) {
noRevURL := curl.WithRevision(-1)
curlRegex := "^" + regexp.QuoteMeta(noRevURL.String())
var docs []charmDoc
err := st.charms.Find(bson.D{{"_id", bson.D{{"$regex", curlRegex}}}, {"placeholder", true}}).All(&docs)
if err != nil {
return nil, fmt.Errorf("cannot get charm %q: %v", curl, err)
}
// Find the highest revision.
var latest charmDoc
for _, doc := range docs {
if latest.URL == nil || doc.URL.Revision > latest.URL.Revision {
latest = doc
}
}
if latest.URL == nil {
return nil, errors.NotFoundf("placeholder charm %q", noRevURL)
}
return newCharm(st, &latest)
}
示例15: SetCharm
// SetCharm adds and removes charms in s. The affected charm is identified by
// charmURL, which must be revisioned. If bundle is nil, the charm will be
// removed; otherwise, it will be stored. It is an error to store a bundle
// under a charmURL that does not share its name and revision.
func (s *MockCharmStore) SetCharm(charmURL *charm.URL, bundle *charm.Bundle) error {
base := charmURL.WithRevision(-1).String()
if charmURL.Revision < 0 {
return fmt.Errorf("bad charm url revision")
}
if bundle == nil {
delete(s.charms[base], charmURL.Revision)
return nil
}
bundleRev := bundle.Revision()
bundleName := bundle.Meta().Name
if bundleName != charmURL.Name || bundleRev != charmURL.Revision {
return fmt.Errorf("charm url %s mismatch with bundle %s-%d", charmURL, bundleName, bundleRev)
}
if _, found := s.charms[base]; !found {
s.charms[base] = map[int]*charm.Bundle{}
}
s.charms[base][charmURL.Revision] = bundle
return nil
}