forked from toolshed/abra
feat: abra server ls
WE DID IT! The first actual command to be ported. Code is still a mess in terms of UX but its a milestone!
This commit is contained in:
@ -5,10 +5,15 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/docker/docker/client"
|
||||
dCliCommand "github.com/docker/cli/cli/command"
|
||||
dCliConfig "github.com/docker/cli/cli/config"
|
||||
dContext "github.com/docker/cli/cli/context"
|
||||
dCliContextStore "github.com/docker/cli/cli/context/store"
|
||||
dClient "github.com/docker/docker/client"
|
||||
"github.com/moby/term"
|
||||
)
|
||||
|
||||
func NewClientWithContext(contextURL string) *client.Client {
|
||||
func NewClientWithContext(contextURL string) *dClient.Client {
|
||||
helper := newConnectionHelper(contextURL)
|
||||
httpClient := &http.Client{
|
||||
// No tls
|
||||
@ -18,24 +23,24 @@ func NewClientWithContext(contextURL string) *client.Client {
|
||||
},
|
||||
}
|
||||
|
||||
var clientOpts []client.Opt
|
||||
var clientOpts []dClient.Opt
|
||||
|
||||
clientOpts = append(clientOpts,
|
||||
client.WithHTTPClient(httpClient),
|
||||
client.WithHost(helper.Host),
|
||||
client.WithDialContext(helper.Dialer),
|
||||
dClient.WithHTTPClient(httpClient),
|
||||
dClient.WithHost(helper.Host),
|
||||
dClient.WithDialContext(helper.Dialer),
|
||||
)
|
||||
|
||||
// FIXME: Maybe don't have this variable here and load it beforehand
|
||||
version := os.Getenv("DOCKER_API_VERSION")
|
||||
|
||||
if version != "" {
|
||||
clientOpts = append(clientOpts, client.WithVersion(version))
|
||||
clientOpts = append(clientOpts, dClient.WithVersion(version))
|
||||
} else {
|
||||
clientOpts = append(clientOpts, client.WithAPIVersionNegotiation())
|
||||
clientOpts = append(clientOpts, dClient.WithAPIVersionNegotiation())
|
||||
}
|
||||
|
||||
cl, err := client.NewClientWithOpts(clientOpts...)
|
||||
cl, err := dClient.NewClientWithOpts(clientOpts...)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("Unable to create docker client")
|
||||
@ -43,3 +48,33 @@ func NewClientWithContext(contextURL string) *client.Client {
|
||||
}
|
||||
return cl
|
||||
}
|
||||
|
||||
func GetContextEndpoint(ctx dCliContextStore.Metadata) string {
|
||||
// safe to use docker key hardcoded since abra doesn't use k8s... yet...
|
||||
endpointmeta := ctx.Endpoints["docker"].(dContext.EndpointMetaBase)
|
||||
return endpointmeta.Host
|
||||
}
|
||||
|
||||
func NewDefaultDockerContextStore() *dCliCommand.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 := dCliConfig.LoadDefaultConfigFile(stderr)
|
||||
contextDir := dCliConfig.ContextStoreDir()
|
||||
storeConfig := dCliCommand.DefaultContextStoreConfig()
|
||||
store := newContextStore(contextDir, storeConfig)
|
||||
|
||||
dockerContextStore := &dCliCommand.ContextStoreWithDefault{
|
||||
Store: store,
|
||||
Resolver: func() (*dCliCommand.DefaultContext, error) {
|
||||
// nil for the Opts because it works without it and its a cli thing
|
||||
return dCliCommand.ResolveDefaultContext(nil, dockerConfig, storeConfig, stderr)
|
||||
},
|
||||
}
|
||||
return dockerContextStore
|
||||
}
|
||||
|
||||
func newContextStore(dir string, config dCliContextStore.Config) dCliContextStore.Store {
|
||||
return dCliContextStore.New(dir, config)
|
||||
}
|
||||
|
Reference in New Issue
Block a user