Skip to content

Commit

Permalink
chore(compose): return error in options (#2520)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdelapenya committed Apr 30, 2024
1 parent 1b35392 commit e1c5bfa
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
7 changes: 4 additions & 3 deletions modules/compose/compose.go
Expand Up @@ -35,7 +35,7 @@ type composeStackOptions struct {
}

type ComposeStackOption interface {
applyToComposeStack(o *composeStackOptions)
applyToComposeStack(o *composeStackOptions) error
}

type stackUpOptions struct {
Expand Down Expand Up @@ -112,7 +112,6 @@ func WithStackFiles(filePaths ...string) ComposeStackOption {
}

// WithStackReaders supports reading the compose file/s from a reader.
// This function will panic if it's no possible to read the content from the reader.
func WithStackReaders(readers ...io.Reader) ComposeStackOption {
return ComposeStackReaders(readers)
}
Expand All @@ -129,7 +128,9 @@ func NewDockerComposeWith(opts ...ComposeStackOption) (*dockerCompose, error) {
}

for i := range opts {
opts[i].applyToComposeStack(&composeOptions)
if err := opts[i].applyToComposeStack(&composeOptions); err != nil {
return nil, err
}
}

if len(composeOptions.Paths) < 1 {
Expand Down
16 changes: 10 additions & 6 deletions modules/compose/compose_api.go
Expand Up @@ -120,27 +120,27 @@ func (ri RemoveImages) applyToStackDown(o *stackDownOptions) {

type ComposeStackReaders []io.Reader

func (r ComposeStackReaders) applyToComposeStack(o *composeStackOptions) {
func (r ComposeStackReaders) applyToComposeStack(o *composeStackOptions) error {
f := make([]string, len(r))
baseName := "docker-compose-%d.yml"
for i, reader := range r {
tmp := os.TempDir()
tmp = filepath.Join(tmp, strconv.FormatInt(time.Now().UnixNano(), 10))
err := os.MkdirAll(tmp, 0755)
if err != nil {
panic(err)
return fmt.Errorf("failed to create temporary directory: %w", err)
}

name := fmt.Sprintf(baseName, i)

bs, err := io.ReadAll(reader)
if err != nil {
panic(err)
fmt.Errorf("failed to read from reader: %w", err)
}

err = os.WriteFile(filepath.Join(tmp, name), bs, 0644)
if err != nil {
panic(err)
fmt.Errorf("failed to write to temporary file: %w", err)
}

f[i] = filepath.Join(tmp, name)
Expand All @@ -150,18 +150,22 @@ func (r ComposeStackReaders) applyToComposeStack(o *composeStackOptions) {
}

o.Paths = f

return nil
}

type ComposeStackFiles []string

func (f ComposeStackFiles) applyToComposeStack(o *composeStackOptions) {
func (f ComposeStackFiles) applyToComposeStack(o *composeStackOptions) error {
o.Paths = f
return nil
}

type StackIdentifier string

func (f StackIdentifier) applyToComposeStack(o *composeStackOptions) {
func (f StackIdentifier) applyToComposeStack(o *composeStackOptions) error {
o.Identifier = string(f)
return nil
}

func (f StackIdentifier) String() string {
Expand Down
3 changes: 2 additions & 1 deletion modules/compose/compose_local.go
Expand Up @@ -96,8 +96,9 @@ func (o ComposeLoggerOption) ApplyToLocalCompose(opts *LocalDockerComposeOptions
opts.Logger = o.logger
}

func (o ComposeLoggerOption) applyToComposeStack(opts *composeStackOptions) {
func (o ComposeLoggerOption) applyToComposeStack(opts *composeStackOptions) error {
opts.Logger = o.logger
return nil
}

// Deprecated: it will be removed in the next major release
Expand Down

0 comments on commit e1c5bfa

Please sign in to comment.