Skip to content

Commit dc3c9cb

Browse files
committed
publish: return ErrPublishAborted when user declines interactive prompts
Signed-off-by: Ishwar <ishwarcm@iitbhilai.ac.in>
1 parent ef86a6e commit dc3c9cb

2 files changed

Lines changed: 62 additions & 1 deletion

File tree

pkg/compose/publish.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ import (
4242
"github.com/docker/compose/v5/pkg/compose/transform"
4343
)
4444

45+
46+
var ErrPublishAborted = errors.New("publish aborted by user")
47+
4548
func (s *composeService) Publish(ctx context.Context, project *types.Project, repository string, options api.PublishOptions) error {
4649
return Run(ctx, func(ctx context.Context) error {
4750
return s.publish(ctx, project, repository, options)
@@ -59,7 +62,7 @@ func (s *composeService) publish(ctx context.Context, project *types.Project, re
5962
return err
6063
}
6164
if !accept {
62-
return nil
65+
return ErrPublishAborted
6366
}
6467
err = s.Push(ctx, project, api.PushOptions{IgnoreFailures: true, ImageMandatory: true})
6568
if err != nil {

pkg/compose/publish_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package compose
1818

1919
import (
20+
"errors"
2021
"slices"
2122
"testing"
2223

@@ -100,3 +101,60 @@ services:
100101
return !slices.Contains([]string{".Data", ".Digest", ".Size"}, path.String())
101102
}, cmp.Ignore()))
102103
}
104+
105+
func Test_preChecks_decline_returns_ErrPublishAborted(t *testing.T) {
106+
project := &types.Project{
107+
Services: types.Services{
108+
"web": {
109+
Name: "web",
110+
Image: "nginx",
111+
Volumes: []types.ServiceVolumeConfig{
112+
{
113+
Type: types.VolumeTypeBind,
114+
Source: "/host/path",
115+
Target: "/container/path",
116+
},
117+
},
118+
},
119+
},
120+
}
121+
122+
declined := func(message string, defaultValue bool) (bool, error) {
123+
return false, nil
124+
}
125+
svc := &composeService{
126+
prompt: declined,
127+
}
128+
129+
_, err := svc.preChecks(project, api.PublishOptions{})
130+
assert.NilError(t, err)
131+
}
132+
func Test_publish_decline_returns_ErrPublishAborted(t *testing.T) {
133+
project := &types.Project{
134+
Services: types.Services{
135+
"web": {
136+
Name: "web",
137+
Image: "nginx",
138+
Volumes: []types.ServiceVolumeConfig{
139+
{
140+
Type: types.VolumeTypeBind,
141+
Source: "/host/path",
142+
Target: "/container/path",
143+
},
144+
},
145+
},
146+
},
147+
}
148+
149+
declined := func(message string, defaultValue bool) (bool, error) {
150+
return false, nil
151+
}
152+
svc := &composeService{
153+
prompt: declined,
154+
events: &ignore{},
155+
}
156+
157+
err := svc.publish(t.Context(), project, "docker.io/myorg/myapp:latest", api.PublishOptions{})
158+
assert.Assert(t, errors.Is(err, ErrPublishAborted),
159+
"expected ErrPublishAborted when user declines, got: %v", err)
160+
}

0 commit comments

Comments
 (0)