Skip to content

Commit e1c5bfa

Browse files
authoredApr 30, 2024··
chore(compose): return error in options (#2520)
1 parent 1b35392 commit e1c5bfa

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed
 

‎modules/compose/compose.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ type composeStackOptions struct {
3535
}
3636

3737
type ComposeStackOption interface {
38-
applyToComposeStack(o *composeStackOptions)
38+
applyToComposeStack(o *composeStackOptions) error
3939
}
4040

4141
type stackUpOptions struct {
@@ -112,7 +112,6 @@ func WithStackFiles(filePaths ...string) ComposeStackOption {
112112
}
113113

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

131130
for i := range opts {
132-
opts[i].applyToComposeStack(&composeOptions)
131+
if err := opts[i].applyToComposeStack(&composeOptions); err != nil {
132+
return nil, err
133+
}
133134
}
134135

135136
if len(composeOptions.Paths) < 1 {

‎modules/compose/compose_api.go

+10-6
Original file line numberDiff line numberDiff line change
@@ -120,27 +120,27 @@ func (ri RemoveImages) applyToStackDown(o *stackDownOptions) {
120120

121121
type ComposeStackReaders []io.Reader
122122

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

134134
name := fmt.Sprintf(baseName, i)
135135

136136
bs, err := io.ReadAll(reader)
137137
if err != nil {
138-
panic(err)
138+
fmt.Errorf("failed to read from reader: %w", err)
139139
}
140140

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

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

152152
o.Paths = f
153+
154+
return nil
153155
}
154156

155157
type ComposeStackFiles []string
156158

157-
func (f ComposeStackFiles) applyToComposeStack(o *composeStackOptions) {
159+
func (f ComposeStackFiles) applyToComposeStack(o *composeStackOptions) error {
158160
o.Paths = f
161+
return nil
159162
}
160163

161164
type StackIdentifier string
162165

163-
func (f StackIdentifier) applyToComposeStack(o *composeStackOptions) {
166+
func (f StackIdentifier) applyToComposeStack(o *composeStackOptions) error {
164167
o.Identifier = string(f)
168+
return nil
165169
}
166170

167171
func (f StackIdentifier) String() string {

‎modules/compose/compose_local.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,9 @@ func (o ComposeLoggerOption) ApplyToLocalCompose(opts *LocalDockerComposeOptions
9696
opts.Logger = o.logger
9797
}
9898

99-
func (o ComposeLoggerOption) applyToComposeStack(opts *composeStackOptions) {
99+
func (o ComposeLoggerOption) applyToComposeStack(opts *composeStackOptions) error {
100100
opts.Logger = o.logger
101+
return nil
101102
}
102103

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

0 commit comments

Comments
 (0)
Please sign in to comment.