Some checks failed
continuous-integration/drone/push Build is failing
See #483
87 lines
2.7 KiB
Go
87 lines
2.7 KiB
Go
package container
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"coopcloud.tech/abra/pkg/formatter"
|
|
"coopcloud.tech/abra/pkg/log"
|
|
"github.com/AlecAivazis/survey/v2"
|
|
"github.com/docker/docker/api/types"
|
|
containerTypes "github.com/docker/docker/api/types/container"
|
|
"github.com/docker/docker/api/types/filters"
|
|
"github.com/docker/docker/client"
|
|
"coopcloud.tech/abra/pkg/i18n"
|
|
)
|
|
|
|
// GetContainer retrieves a container. If noInput is false and the retrievd
|
|
// count of containers does not match 1, then a prompt is presented to let the
|
|
// user choose. A count of 0 is handled gracefully.
|
|
func GetContainer(c context.Context, cl *client.Client, filters filters.Args, noInput bool) (types.Container, error) {
|
|
containerOpts := containerTypes.ListOptions{Filters: filters}
|
|
containers, err := cl.ContainerList(c, containerOpts)
|
|
if err != nil {
|
|
return types.Container{}, err
|
|
}
|
|
|
|
if len(containers) == 0 {
|
|
filter := filters.Get("name")[0]
|
|
return types.Container{}, errors.New(i18n.G("no containers matching the %v filter found?", filter))
|
|
}
|
|
|
|
if len(containers) > 1 {
|
|
var containersRaw []string
|
|
for _, container := range containers {
|
|
containerName := strings.Join(container.Names, " ")
|
|
trimmed := strings.TrimPrefix(containerName, "/")
|
|
created := formatter.HumanDuration(container.Created)
|
|
containersRaw = append(containersRaw, i18n.G("%s (created %v)", trimmed, created))
|
|
}
|
|
|
|
if noInput {
|
|
err := errors.New(i18n.G("expected 1 container but found %v: %s", len(containers), strings.Join(containersRaw, " ")))
|
|
return types.Container{}, err
|
|
}
|
|
|
|
log.Warnf(i18n.G("ambiguous container list received, prompting for input"))
|
|
|
|
var response string
|
|
prompt := &survey.Select{
|
|
Message: i18n.G("which container are you looking for?"),
|
|
Options: containersRaw,
|
|
}
|
|
|
|
if err := survey.AskOne(prompt, &response); err != nil {
|
|
return types.Container{}, err
|
|
}
|
|
|
|
chosenContainer := strings.TrimSpace(strings.Split(response, " ")[0])
|
|
for _, container := range containers {
|
|
containerName := strings.TrimSpace(strings.Join(container.Names, " "))
|
|
trimmed := strings.TrimPrefix(containerName, "/")
|
|
if trimmed == chosenContainer {
|
|
return container, nil
|
|
}
|
|
}
|
|
|
|
log.Fatal(i18n.G("failed to match chosen container"))
|
|
}
|
|
|
|
return containers[0], nil
|
|
}
|
|
|
|
// GetContainerFromStackAndService retrieves the container for the given stack and service.
|
|
func GetContainerFromStackAndService(cl *client.Client, stack, service string) (types.Container, error) {
|
|
filters := filters.NewArgs()
|
|
filters.Add("name", fmt.Sprintf("^%s_%s", stack, service))
|
|
|
|
container, err := GetContainer(context.Background(), cl, filters, true)
|
|
if err != nil {
|
|
return types.Container{}, err
|
|
}
|
|
|
|
return container, nil
|
|
}
|