refactor: actual context getting
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Roxie Gibson 2021-07-22 09:51:27 +01:00
parent a4a8997f57
commit fe86b50ee3
Signed by: roxxers
GPG Key ID: 5D0140EDEE123F4D
2 changed files with 29 additions and 5 deletions

View File

@ -97,8 +97,10 @@ var appPsCommand = &cli.Command{
Name: "ps", Name: "ps",
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
ctx := context.Background() ctx := context.Background()
fmt.Println(Context) cl, err := client.NewClientWithContext(Context)
cl := client.NewClientWithContext(Context) if err != nil {
logrus.Fatal(err)
}
tasks, err := cl.TaskList(ctx, types.TaskListOptions{}) tasks, err := cl.TaskList(ctx, types.TaskListOptions{})
if err != nil { if err != nil {
logrus.Fatal(err) logrus.Fatal(err)

View File

@ -14,8 +14,16 @@ import (
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
func NewClientWithContext(contextURL string) *dClient.Client { func NewClientWithContext(contextName string) (*dClient.Client, error) {
helper := newConnectionHelper(contextURL) 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{ httpClient := &http.Client{
// No tls // No tls
// No proxy // No proxy
@ -46,7 +54,21 @@ func NewClientWithContext(contextURL string) *dClient.Client {
if err != nil { if err != nil {
logrus.Fatalf("unable to create Docker client: %s", err) 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) { func GetContextEndpoint(ctx dCliContextStore.Metadata) (string, error) {