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",
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)

View File

@ -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) {