dockerutil pkg to manage docker api client and context

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax
2022-11-28 15:47:40 +01:00
parent 47cf72b8ba
commit 63073b65c0
10 changed files with 199 additions and 173 deletions

View File

@ -0,0 +1,40 @@
package dockerutil
import (
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/context/docker"
"github.com/pkg/errors"
)
// GetDockerEndpoint returns docker endpoint meta for given context
func GetDockerEndpoint(dockerCli command.Cli, name string) (*docker.EndpointMeta, error) {
list, err := dockerCli.ContextStore().List()
if err != nil {
return nil, err
}
for _, l := range list {
if l.Name == name {
epm, err := docker.EndpointFromContext(l)
if err != nil {
return nil, err
}
return &epm, nil
}
}
return nil, nil
}
// GetCurrentEndpoint returns the current default endpoint value
func GetCurrentEndpoint(dockerCli command.Cli) (string, error) {
name := dockerCli.CurrentContext()
if name != "default" {
return name, nil
}
dem, err := GetDockerEndpoint(dockerCli, name)
if err != nil {
return "", errors.Errorf("docker endpoint for %q not found", name)
} else if dem != nil {
return dem.Host, nil
}
return "", nil
}