From fe86b50ee34663bbbe21a0b4dc6f4a1a06e1669e Mon Sep 17 00:00:00 2001 From: Roxie Gibson Date: Thu, 22 Jul 2021 09:51:27 +0100 Subject: [PATCH] refactor: actual context getting --- cli/app.go | 6 ++++-- client/client.go | 28 +++++++++++++++++++++++++--- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/cli/app.go b/cli/app.go index 8baffad..8d4d198 100644 --- a/cli/app.go +++ b/cli/app.go @@ -97,8 +97,10 @@ var appPsCommand = &cli.Command{ Name: "ps", Action: func(c *cli.Context) error { ctx := context.Background() - fmt.Println(Context) - cl := client.NewClientWithContext(Context) + cl, err := client.NewClientWithContext(Context) + if err != nil { + logrus.Fatal(err) + } tasks, err := cl.TaskList(ctx, types.TaskListOptions{}) if err != nil { logrus.Fatal(err) diff --git a/client/client.go b/client/client.go index 05e5b11..84570f5 100644 --- a/client/client.go +++ b/client/client.go @@ -14,8 +14,16 @@ import ( "github.com/sirupsen/logrus" ) -func NewClientWithContext(contextURL string) *dClient.Client { - helper := newConnectionHelper(contextURL) +func NewClientWithContext(contextName string) (*dClient.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 @@ -46,7 +54,21 @@ func NewClientWithContext(contextURL string) *dClient.Client { if err != nil { logrus.Fatalf("unable to create Docker client: %s", err) } - return cl + return cl, nil +} + +func GetContext(contextName string) (dCliContextStore.Metadata, error) { + ctxs, err := NewDefaultDockerContextStore().Store.List() + if err != nil { + return dCliContextStore.Metadata{}, err + } + var context dCliContextStore.Metadata + for _, ctx := range ctxs { + if ctx.Name == contextName { + context = ctx + } + } + return context, nil } func GetContextEndpoint(ctx dCliContextStore.Metadata) (string, error) {