github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/charmcmd/store.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package charmcmd 5 6 import ( 7 "io" 8 9 "github.com/juju/cmd" 10 "github.com/juju/errors" 11 12 "github.com/juju/juju/charmstore" 13 "github.com/juju/juju/cmd/modelcmd" 14 ) 15 16 // TODO(ericsnow) Factor out code from cmd/juju/commands/common.go and │ 17 // cmd/envcmd/base.go into cmd/charmstore.go and cmd/apicontext.go. Then │ 18 // use those here instead of copy-and-pasting here. 19 20 /////////////////// 21 // The charmstoreSpec code is based loosely on code in cmd/juju/commands/deploy.go. 22 23 // CharmstoreSpec provides the functionality needed to open a charm 24 // store client. 25 type CharmstoreSpec interface { 26 // Connect connects to the specified charm store. 27 Connect(ctx *cmd.Context) (charmstore.Client, io.Closer, error) 28 } 29 30 type charmstoreSpec struct{} 31 32 // newCharmstoreSpec creates a new charm store spec with default 33 // settings. 34 func newCharmstoreSpec() CharmstoreSpec { 35 return charmstoreSpec{} 36 } 37 38 // Connect implements CharmstoreSpec. 39 func (cs charmstoreSpec) Connect(ctx *cmd.Context) (charmstore.Client, io.Closer, error) { 40 // Note that creating the API context in Connect is technically 41 // wrong, as it means we'll be creating the bakery context 42 // (and reading/writing the cookies) each time it's called. 43 // TODO(ericsnow) Move apiContext to a field on charmstoreSpec. 44 apiContext, err := modelcmd.NewAPIContext(ctx) 45 if err != nil { 46 return charmstore.Client{}, nil, errors.Trace(err) 47 } 48 // We use the default for URL. 49 client, err := charmstore.NewCustomClient(apiContext.BakeryClient, nil) 50 if err != nil { 51 apiContext.Close() 52 return charmstore.Client{}, nil, errors.Trace(err) 53 } 54 return client, apiContext, nil 55 }