Просмотр исходного кода

add AlwaysOkPrompt to replace 'AlwaysYes' current implementation'

Signed-off-by: Guillaume Lours <[email protected]>
Guillaume Lours 1 месяц назад
Родитель
Сommit
3658a063bb
7 измененных файлов с 26 добавлено и 20 удалено
  1. 4 1
      cmd/compose/create.go
  2. 3 1
      cmd/compose/publish.go
  3. 4 1
      cmd/compose/up.go
  4. 0 3
      pkg/api/api.go
  5. 7 0
      pkg/compose/compose.go
  6. 8 11
      pkg/compose/create.go
  7. 0 3
      pkg/compose/publish.go

+ 4 - 1
cmd/compose/create.go

@@ -110,6 +110,10 @@ func runCreate(ctx context.Context, dockerCli command.Cli, backendOptions *Backe
 		build = &bo
 	}
 
+	if createOpts.AssumeYes {
+		backendOptions.Options = append(backendOptions.Options, compose.WithPrompt(compose.AlwaysOkPrompt()))
+	}
+
 	backend, err := compose.NewComposeService(dockerCli, backendOptions.Options...)
 	if err != nil {
 		return err
@@ -124,7 +128,6 @@ func runCreate(ctx context.Context, dockerCli command.Cli, backendOptions *Backe
 		Inherit:              !createOpts.noInherit,
 		Timeout:              createOpts.GetTimeout(),
 		QuietPull:            createOpts.quietPull,
-		AssumeYes:            createOpts.AssumeYes,
 	})
 }
 

+ 3 - 1
cmd/compose/publish.go

@@ -78,6 +78,9 @@ func runPublish(ctx context.Context, dockerCli command.Cli, backendOptions *Back
 		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...)
 	if err != nil {
 		return err
@@ -87,6 +90,5 @@ func runPublish(ctx context.Context, dockerCli command.Cli, backendOptions *Back
 		Application:         opts.app,
 		OCIVersion:          api.OCIVersion(opts.ociVersion),
 		WithEnvironment:     opts.withEnvironment,
-		AssumeYes:           opts.assumeYes,
 	})
 }

+ 4 - 1
cmd/compose/up.go

@@ -278,7 +278,10 @@ func runUp(
 		Inherit:              !createOptions.noInherit,
 		Timeout:              createOptions.GetTimeout(),
 		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...)

+ 0 - 3
pkg/api/api.go

@@ -231,8 +231,6 @@ type CreateOptions struct {
 	Timeout *time.Duration
 	// QuietPull makes the pulling process quiet
 	QuietPull bool
-	// AssumeYes assume "yes" as answer to all prompts and run non-interactively
-	AssumeYes bool
 }
 
 // StartOptions group options of the Start API
@@ -447,7 +445,6 @@ type PublishOptions struct {
 	Application         bool
 	WithEnvironment     bool
 
-	AssumeYes  bool
 	OCIVersion OCIVersion
 }
 

+ 7 - 0
pkg/compose/compose.go

@@ -195,6 +195,13 @@ func WithDryRun(s *composeService) 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.
 // Typically used to configure a progress UI
 func WithEventProcessor(bus progress.EventProcessor) Option {

+ 8 - 11
pkg/compose/create.go

@@ -93,7 +93,7 @@ func (s *composeService) create(ctx context.Context, project *types.Project, opt
 		return err
 	}
 
-	volumes, err := s.ensureProjectVolumes(ctx, project, options.AssumeYes)
+	volumes, err := s.ensureProjectVolumes(ctx, project)
 	if err != nil {
 		return err
 	}
@@ -150,13 +150,13 @@ func (s *composeService) ensureNetworks(ctx context.Context, project *types.Proj
 	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{}
 	for k, volume := range project.Volumes {
 		volume.CustomLabels = volume.CustomLabels.Add(api.VolumeLabel, k)
 		volume.CustomLabels = volume.CustomLabels.Add(api.ProjectLabel, project.Name)
 		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 {
 			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)
 	if err != nil {
 		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]
 	if ok && actual != expected {
-		confirm := assumeYes
-		if !assumeYes {
-			msg := fmt.Sprintf("Volume %q exists but doesn't match configuration in compose file. Recreate (data will be lost)?", volume.Name)
-			confirm, err = s.prompt(msg, false)
-			if err != nil {
-				return "", err
-			}
+		msg := fmt.Sprintf("Volume %q exists but doesn't match configuration in compose file. Recreate (data will be lost)?", volume.Name)
+		confirm, err := s.prompt(msg, false)
+		if err != nil {
+			return "", err
 		}
 		if confirm {
 			err = s.removeDivergedVolume(ctx, name, volume, project)

+ 0 - 3
pkg/compose/publish.go

@@ -297,9 +297,6 @@ func (s *composeService) preChecks(project *types.Project, options api.PublishOp
 	if ok, err := s.checkOnlyBuildSection(project); !ok || err != nil {
 		return false, err
 	}
-	if options.AssumeYes {
-		return true, nil
-	}
 	bindMounts := s.checkForBindMount(project)
 	if len(bindMounts) > 0 {
 		b := strings.Builder{}