mirror of
https://github.com/docker/compose.git
synced 2026-02-09 01:59:22 +08:00
add AlwaysOkPrompt to replace 'AlwaysYes' current implementation'
Signed-off-by: Guillaume Lours <705411+glours@users.noreply.github.com>
This commit is contained in:
committed by
Nicolas De loof
parent
74a4ccdd85
commit
3658a063bb
@@ -110,6 +110,10 @@ func runCreate(ctx context.Context, dockerCli command.Cli, backendOptions *Backe
|
|||||||
build = &bo
|
build = &bo
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if createOpts.AssumeYes {
|
||||||
|
backendOptions.Options = append(backendOptions.Options, compose.WithPrompt(compose.AlwaysOkPrompt()))
|
||||||
|
}
|
||||||
|
|
||||||
backend, err := compose.NewComposeService(dockerCli, backendOptions.Options...)
|
backend, err := compose.NewComposeService(dockerCli, backendOptions.Options...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -124,7 +128,6 @@ func runCreate(ctx context.Context, dockerCli command.Cli, backendOptions *Backe
|
|||||||
Inherit: !createOpts.noInherit,
|
Inherit: !createOpts.noInherit,
|
||||||
Timeout: createOpts.GetTimeout(),
|
Timeout: createOpts.GetTimeout(),
|
||||||
QuietPull: createOpts.quietPull,
|
QuietPull: createOpts.quietPull,
|
||||||
AssumeYes: createOpts.AssumeYes,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -78,6 +78,9 @@ func runPublish(ctx context.Context, dockerCli command.Cli, backendOptions *Back
|
|||||||
return errors.New("cannot publish compose file with local includes")
|
return errors.New("cannot publish compose file with local includes")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if opts.assumeYes {
|
||||||
|
backendOptions.Options = append(backendOptions.Options, compose.WithPrompt(compose.AlwaysOkPrompt()))
|
||||||
|
}
|
||||||
backend, err := compose.NewComposeService(dockerCli, backendOptions.Options...)
|
backend, err := compose.NewComposeService(dockerCli, backendOptions.Options...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -87,6 +90,5 @@ func runPublish(ctx context.Context, dockerCli command.Cli, backendOptions *Back
|
|||||||
Application: opts.app,
|
Application: opts.app,
|
||||||
OCIVersion: api.OCIVersion(opts.ociVersion),
|
OCIVersion: api.OCIVersion(opts.ociVersion),
|
||||||
WithEnvironment: opts.withEnvironment,
|
WithEnvironment: opts.withEnvironment,
|
||||||
AssumeYes: opts.assumeYes,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -278,7 +278,10 @@ func runUp(
|
|||||||
Inherit: !createOptions.noInherit,
|
Inherit: !createOptions.noInherit,
|
||||||
Timeout: createOptions.GetTimeout(),
|
Timeout: createOptions.GetTimeout(),
|
||||||
QuietPull: createOptions.quietPull,
|
QuietPull: createOptions.quietPull,
|
||||||
AssumeYes: createOptions.AssumeYes,
|
}
|
||||||
|
|
||||||
|
if createOptions.AssumeYes {
|
||||||
|
backendOptions.Options = append(backendOptions.Options, compose.WithPrompt(compose.AlwaysOkPrompt()))
|
||||||
}
|
}
|
||||||
|
|
||||||
backend, err := compose.NewComposeService(dockerCli, backendOptions.Options...)
|
backend, err := compose.NewComposeService(dockerCli, backendOptions.Options...)
|
||||||
|
|||||||
@@ -231,8 +231,6 @@ type CreateOptions struct {
|
|||||||
Timeout *time.Duration
|
Timeout *time.Duration
|
||||||
// QuietPull makes the pulling process quiet
|
// QuietPull makes the pulling process quiet
|
||||||
QuietPull bool
|
QuietPull bool
|
||||||
// AssumeYes assume "yes" as answer to all prompts and run non-interactively
|
|
||||||
AssumeYes bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// StartOptions group options of the Start API
|
// StartOptions group options of the Start API
|
||||||
@@ -447,7 +445,6 @@ type PublishOptions struct {
|
|||||||
Application bool
|
Application bool
|
||||||
WithEnvironment bool
|
WithEnvironment bool
|
||||||
|
|
||||||
AssumeYes bool
|
|
||||||
OCIVersion OCIVersion
|
OCIVersion OCIVersion
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -195,6 +195,13 @@ func WithDryRun(s *composeService) error {
|
|||||||
|
|
||||||
type Prompt func(message string, defaultValue bool) (bool, error)
|
type Prompt func(message string, defaultValue bool) (bool, error)
|
||||||
|
|
||||||
|
// AlwaysOkPrompt returns a Prompt implementation that always returns true without user interaction.
|
||||||
|
func AlwaysOkPrompt() Prompt {
|
||||||
|
return func(message string, defaultValue bool) (bool, error) {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// WithEventProcessor configure component to get notified on Compose operation and progress events.
|
// WithEventProcessor configure component to get notified on Compose operation and progress events.
|
||||||
// Typically used to configure a progress UI
|
// Typically used to configure a progress UI
|
||||||
func WithEventProcessor(bus progress.EventProcessor) Option {
|
func WithEventProcessor(bus progress.EventProcessor) Option {
|
||||||
|
|||||||
@@ -93,7 +93,7 @@ func (s *composeService) create(ctx context.Context, project *types.Project, opt
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
volumes, err := s.ensureProjectVolumes(ctx, project, options.AssumeYes)
|
volumes, err := s.ensureProjectVolumes(ctx, project)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -150,13 +150,13 @@ func (s *composeService) ensureNetworks(ctx context.Context, project *types.Proj
|
|||||||
return networks, nil
|
return networks, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *composeService) ensureProjectVolumes(ctx context.Context, project *types.Project, assumeYes bool) (map[string]string, error) {
|
func (s *composeService) ensureProjectVolumes(ctx context.Context, project *types.Project) (map[string]string, error) {
|
||||||
ids := map[string]string{}
|
ids := map[string]string{}
|
||||||
for k, volume := range project.Volumes {
|
for k, volume := range project.Volumes {
|
||||||
volume.CustomLabels = volume.CustomLabels.Add(api.VolumeLabel, k)
|
volume.CustomLabels = volume.CustomLabels.Add(api.VolumeLabel, k)
|
||||||
volume.CustomLabels = volume.CustomLabels.Add(api.ProjectLabel, project.Name)
|
volume.CustomLabels = volume.CustomLabels.Add(api.ProjectLabel, project.Name)
|
||||||
volume.CustomLabels = volume.CustomLabels.Add(api.VersionLabel, api.ComposeVersion)
|
volume.CustomLabels = volume.CustomLabels.Add(api.VersionLabel, api.ComposeVersion)
|
||||||
id, err := s.ensureVolume(ctx, k, volume, project, assumeYes)
|
id, err := s.ensureVolume(ctx, k, volume, project)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -1529,7 +1529,7 @@ func (s *composeService) resolveExternalNetwork(ctx context.Context, n *types.Ne
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *composeService) ensureVolume(ctx context.Context, name string, volume types.VolumeConfig, project *types.Project, assumeYes bool) (string, error) {
|
func (s *composeService) ensureVolume(ctx context.Context, name string, volume types.VolumeConfig, project *types.Project) (string, error) {
|
||||||
inspected, err := s.apiClient().VolumeInspect(ctx, volume.Name)
|
inspected, err := s.apiClient().VolumeInspect(ctx, volume.Name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !errdefs.IsNotFound(err) {
|
if !errdefs.IsNotFound(err) {
|
||||||
@@ -1561,13 +1561,10 @@ func (s *composeService) ensureVolume(ctx context.Context, name string, volume t
|
|||||||
}
|
}
|
||||||
actual, ok := inspected.Labels[api.ConfigHashLabel]
|
actual, ok := inspected.Labels[api.ConfigHashLabel]
|
||||||
if ok && actual != expected {
|
if ok && actual != expected {
|
||||||
confirm := assumeYes
|
msg := fmt.Sprintf("Volume %q exists but doesn't match configuration in compose file. Recreate (data will be lost)?", volume.Name)
|
||||||
if !assumeYes {
|
confirm, err := s.prompt(msg, false)
|
||||||
msg := fmt.Sprintf("Volume %q exists but doesn't match configuration in compose file. Recreate (data will be lost)?", volume.Name)
|
if err != nil {
|
||||||
confirm, err = s.prompt(msg, false)
|
return "", err
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if confirm {
|
if confirm {
|
||||||
err = s.removeDivergedVolume(ctx, name, volume, project)
|
err = s.removeDivergedVolume(ctx, name, volume, project)
|
||||||
|
|||||||
@@ -297,9 +297,6 @@ func (s *composeService) preChecks(project *types.Project, options api.PublishOp
|
|||||||
if ok, err := s.checkOnlyBuildSection(project); !ok || err != nil {
|
if ok, err := s.checkOnlyBuildSection(project); !ok || err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
if options.AssumeYes {
|
|
||||||
return true, nil
|
|
||||||
}
|
|
||||||
bindMounts := s.checkForBindMount(project)
|
bindMounts := s.checkForBindMount(project)
|
||||||
if len(bindMounts) > 0 {
|
if len(bindMounts) > 0 {
|
||||||
b := strings.Builder{}
|
b := strings.Builder{}
|
||||||
|
|||||||
Reference in New Issue
Block a user