Skip to content

Commit

Permalink
Update confort: add UpOption (WithWaiter)
Browse files Browse the repository at this point in the history
  • Loading branch information
daichitakahashi committed Jan 17, 2023
1 parent f7ca9e2 commit aaaa14c
Showing 1 changed file with 41 additions and 2 deletions.
43 changes: 41 additions & 2 deletions compose.go
Expand Up @@ -13,6 +13,7 @@ import (

composetypes "github.com/compose-spec/compose-go/types"
"github.com/daichitakahashi/confort/internal/exclusion"
"github.com/daichitakahashi/confort/wait"
"github.com/docker/cli/cli/command"
composecmd "github.com/docker/compose/v2/cmd/compose"
"github.com/docker/compose/v2/pkg/api"
Expand All @@ -23,7 +24,7 @@ import (
)

type ComposeProject struct {
cli client.APIClient
cli *client.Client
svc api.Service
proj *composetypes.Project
defaultTimeout time.Duration
Expand Down Expand Up @@ -243,14 +244,33 @@ func resolveConfigFilePath(base string, configFiles []string) (r []string, err e
return r, nil
}

type (
upIdent interface{ up() }
UpOption interface {
option.Interface
upIdent
}
identOptionWaiter struct{}
upOption struct {
option.Interface
upIdent
}
)

func WithWaiter(w *wait.Waiter) UpOption {
return upOption{
Interface: option.New(identOptionWaiter{}, w),
}
}

type Service struct {
c *ComposeProject
s composetypes.ServiceConfig
name string
ports Ports
}

func (c *ComposeProject) Up(ctx context.Context, service string) (*Service, error) {
func (c *ComposeProject) Up(ctx context.Context, service string, opts ...UpOption) (*Service, error) {
// Check service name.
serviceConfig, err := c.proj.GetService(service)
if err != nil {
Expand All @@ -260,6 +280,14 @@ func (c *ComposeProject) Up(ctx context.Context, service string) (*Service, erro
ctx, cancel := applyTimeout(ctx, c.defaultTimeout)
defer cancel()

var w *wait.Waiter
for _, opt := range opts {
switch opt.Ident() {
case identOptionWaiter{}:
w = opt.Value().(*wait.Waiter)
}
}

name := fmt.Sprintf("%s-%s", c.proj.Name, service)
unlock, err := c.ex.LockForContainerSetup(ctx, name)
if err != nil {
Expand Down Expand Up @@ -324,6 +352,17 @@ func (c *ComposeProject) Up(ctx context.Context, service string) (*Service, erro
return nil, fmt.Errorf("failed to get service container info: %w", err)
}

if w != nil {
err = w.Wait(ctx, &fetcher{
cli: c.cli,
containerID: containerID,
ports: info.NetworkSettings.Ports,
})
if err != nil {
return nil, fmt.Errorf("error on waiting service: %w", err)
}
}

return &Service{
c: c,
s: serviceConfig,
Expand Down

0 comments on commit aaaa14c

Please sign in to comment.