forked from toolshed/abra
		
	fix: allow for client creation on default context
See coop-cloud/organising#206.
This commit is contained in:
		@ -11,30 +11,33 @@ import (
 | 
			
		||||
 | 
			
		||||
// New initiates a new Docker client.
 | 
			
		||||
func New(contextName string) (*client.Client, error) {
 | 
			
		||||
	context, err := GetContext(contextName)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	ctxEndpoint, err := GetContextEndpoint(context)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	helper := newConnectionHelper(ctxEndpoint)
 | 
			
		||||
	httpClient := &http.Client{
 | 
			
		||||
		// No tls, no proxy
 | 
			
		||||
		Transport: &http.Transport{
 | 
			
		||||
			DialContext: helper.Dialer,
 | 
			
		||||
		},
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var clientOpts []client.Opt
 | 
			
		||||
	clientOpts = append(clientOpts,
 | 
			
		||||
		client.WithHTTPClient(httpClient),
 | 
			
		||||
		client.WithHost(helper.Host),
 | 
			
		||||
		client.WithDialContext(helper.Dialer),
 | 
			
		||||
	)
 | 
			
		||||
 | 
			
		||||
	if contextName != "default" {
 | 
			
		||||
		context, err := GetContext(contextName)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		ctxEndpoint, err := GetContextEndpoint(context)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		helper := newConnectionHelper(ctxEndpoint)
 | 
			
		||||
		httpClient := &http.Client{
 | 
			
		||||
			// No tls, no proxy
 | 
			
		||||
			Transport: &http.Transport{
 | 
			
		||||
				DialContext: helper.Dialer,
 | 
			
		||||
			},
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		clientOpts = append(clientOpts,
 | 
			
		||||
			client.WithHTTPClient(httpClient),
 | 
			
		||||
			client.WithHost(helper.Host),
 | 
			
		||||
			client.WithDialContext(helper.Dialer),
 | 
			
		||||
		)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	version := os.Getenv("DOCKER_API_VERSION")
 | 
			
		||||
	if version != "" {
 | 
			
		||||
 | 
			
		||||
@ -10,10 +10,10 @@ import (
 | 
			
		||||
 | 
			
		||||
func newConnectionHelper(daemonURL string) *connhelper.ConnectionHelper {
 | 
			
		||||
	helper, err := connhelper.GetConnectionHelper(daemonURL)
 | 
			
		||||
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		logrus.Fatal(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return helper
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -9,6 +9,7 @@ import (
 | 
			
		||||
	context "github.com/docker/cli/cli/context"
 | 
			
		||||
	"github.com/docker/cli/cli/context/docker"
 | 
			
		||||
	contextStore "github.com/docker/cli/cli/context/store"
 | 
			
		||||
	cliflags "github.com/docker/cli/cli/flags"
 | 
			
		||||
	"github.com/moby/term"
 | 
			
		||||
	"github.com/sirupsen/logrus"
 | 
			
		||||
)
 | 
			
		||||
@ -94,7 +95,6 @@ func GetContext(contextName string) (contextStore.Metadata, error) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func GetContextEndpoint(ctx contextStore.Metadata) (string, error) {
 | 
			
		||||
	// safe to use docker key hardcoded since abra doesn't use k8s... yet...
 | 
			
		||||
	endpointmeta, ok := ctx.Endpoints["docker"].(context.EndpointMetaBase)
 | 
			
		||||
	if !ok {
 | 
			
		||||
		err := errors.New("context lacks Docker endpoint")
 | 
			
		||||
@ -108,21 +108,20 @@ func newContextStore(dir string, config contextStore.Config) contextStore.Store
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewDefaultDockerContextStore() *command.ContextStoreWithDefault {
 | 
			
		||||
	// Grabbing the stderr from Docker commands
 | 
			
		||||
	// Much easier to fit this into the code we are using to replicate docker cli commands
 | 
			
		||||
	_, _, stderr := term.StdStreams()
 | 
			
		||||
	// TODO: Look into custom docker configs in case users want that
 | 
			
		||||
	dockerConfig := dConfig.LoadDefaultConfigFile(stderr)
 | 
			
		||||
	contextDir := dConfig.ContextStoreDir()
 | 
			
		||||
	storeConfig := command.DefaultContextStoreConfig()
 | 
			
		||||
	store := newContextStore(contextDir, storeConfig)
 | 
			
		||||
 | 
			
		||||
	opts := &cliflags.CommonOptions{Context: "default"}
 | 
			
		||||
 | 
			
		||||
	dockerContextStore := &command.ContextStoreWithDefault{
 | 
			
		||||
		Store: store,
 | 
			
		||||
		Resolver: func() (*command.DefaultContext, error) {
 | 
			
		||||
			// nil for the Opts because it works without it and its a cli thing
 | 
			
		||||
			return command.ResolveDefaultContext(nil, dockerConfig, storeConfig, stderr)
 | 
			
		||||
			return command.ResolveDefaultContext(opts, dockerConfig, storeConfig, stderr)
 | 
			
		||||
		},
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return dockerContextStore
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user