Skip to content

Commit

Permalink
fly: behaviour: add confirm to archive-pipelines
Browse files Browse the repository at this point in the history
#5320

Since archiving pipelines can be destructive, the fly archive-pipelines
command should have a confirm dialog before making any changes. This is
overridable by a --non-interactive flag.

In the case of --all, print a table of all the pipelines that will be
archived if the user confirms.

Signed-off-by: Aidan Oldershaw <aoldershaw@pivotal.io>
Co-authored-by: James Thomson <jthomson@pivotal.io>
  • Loading branch information
aoldershaw and James Thomson committed Mar 30, 2020
1 parent c292309 commit e42d175
Show file tree
Hide file tree
Showing 2 changed files with 256 additions and 64 deletions.
55 changes: 52 additions & 3 deletions fly/commands/archive_pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ package commands

import (
"fmt"

"github.com/concourse/concourse/fly/commands/internal/displayhelpers"
"github.com/concourse/concourse/fly/commands/internal/flaghelpers"
"github.com/concourse/concourse/fly/rc"
"github.com/concourse/concourse/fly/ui"
"github.com/fatih/color"
"github.com/vito/go-interact/interact"
"os"
)

type ArchivePipelineCommand struct {
Pipeline flaghelpers.PipelineFlag `short:"p" long:"pipeline" description:"Pipeline to archive"`
All bool `short:"a" long:"all" description:"Archive all pipelines"`
Pipeline flaghelpers.PipelineFlag `short:"p" long:"pipeline" description:"Pipeline to archive"`
All bool `short:"a" long:"all" description:"Archive all pipelines"`
SkipInteractive bool `short:"n" long:"non-interactive" description:"Skips interactions, uses default values"`
}

func (command *ArchivePipelineCommand) Validate() error {
Expand Down Expand Up @@ -59,6 +63,17 @@ func (command *ArchivePipelineCommand) Execute(args []string) error {
}
}

if len(pipelineNames) == 0 {
fmt.Println("there are no unarchived pipelines")
fmt.Println("bailing out")
return nil
}

if !command.confirmArchive(pipelineNames) {
fmt.Println("bailing out")
return nil
}

for _, pipelineName := range pipelineNames {
found, err := target.Team().ArchivePipeline(pipelineName)
if err != nil {
Expand All @@ -74,3 +89,37 @@ func (command *ArchivePipelineCommand) Execute(args []string) error {

return nil
}

func (command ArchivePipelineCommand) confirmArchive(pipelines []string) bool {
if command.SkipInteractive {
return true
}

if len(pipelines) > 1 {
command.printPipelinesTable(pipelines)
}

var confirm bool
err := interact.NewInteraction(command.archivePrompt(pipelines)).Resolve(&confirm)
if err != nil {
return false
}

return confirm
}

func (ArchivePipelineCommand) printPipelinesTable(pipelines []string) {
table := ui.Table{Headers: ui.TableRow{{Contents: "pipelines", Color: color.New(color.Bold)}}}
for _, pipeline := range pipelines {
table.Data = append(table.Data, ui.TableRow{{Contents: pipeline}})
}
table.Render(os.Stdout, true)
fmt.Println()
}

func (ArchivePipelineCommand) archivePrompt(pipelines []string) string {
if len(pipelines) == 1 {
return fmt.Sprintf("archive pipeline '%s'?", pipelines[0])
}
return fmt.Sprintf("archive %d pipelines?", len(pipelines))
}

0 comments on commit e42d175

Please sign in to comment.