github.com/chenbh/concourse/v6@v6.4.2/fly/commands/archive_pipeline.go (about) 1 package commands 2 3 import ( 4 "fmt" 5 "os" 6 7 "github.com/chenbh/concourse/v6/fly/commands/internal/displayhelpers" 8 "github.com/chenbh/concourse/v6/fly/commands/internal/flaghelpers" 9 "github.com/chenbh/concourse/v6/fly/rc" 10 "github.com/chenbh/concourse/v6/fly/ui" 11 "github.com/fatih/color" 12 "github.com/vito/go-interact/interact" 13 ) 14 15 type ArchivePipelineCommand struct { 16 Pipeline flaghelpers.PipelineFlag `short:"p" long:"pipeline" description:"Pipeline to archive"` 17 All bool `short:"a" long:"all" description:"Archive all pipelines"` 18 SkipInteractive bool `short:"n" long:"non-interactive" description:"Skips interactions, uses default values"` 19 } 20 21 func (command *ArchivePipelineCommand) Validate() error { 22 _, err := command.Pipeline.Validate() 23 return err 24 } 25 26 func (command *ArchivePipelineCommand) Execute(args []string) error { 27 if string(command.Pipeline) == "" && !command.All { 28 displayhelpers.Failf("one of the flags '-p, --pipeline' or '-a, --all' is required") 29 } 30 31 if string(command.Pipeline) != "" && command.All { 32 displayhelpers.Failf("only one of the flags '-p, --pipeline' or '-a, --all' is allowed") 33 } 34 35 err := command.Validate() 36 if err != nil { 37 return err 38 } 39 40 target, err := rc.LoadTarget(Fly.Target, Fly.Verbose) 41 if err != nil { 42 return err 43 } 44 45 err = target.Validate() 46 if err != nil { 47 return err 48 } 49 50 var pipelineNames []string 51 if string(command.Pipeline) != "" { 52 pipelineNames = []string{string(command.Pipeline)} 53 } 54 55 if command.All { 56 pipelines, err := target.Team().ListPipelines() 57 if err != nil { 58 return err 59 } 60 61 for _, pipeline := range pipelines { 62 if !pipeline.Archived { 63 pipelineNames = append(pipelineNames, pipeline.Name) 64 } 65 } 66 } 67 68 if len(pipelineNames) == 0 { 69 fmt.Println("there are no unarchived pipelines") 70 fmt.Println("bailing out") 71 return nil 72 } 73 74 if !command.confirmArchive(pipelineNames) { 75 fmt.Println("bailing out") 76 return nil 77 } 78 79 for _, pipelineName := range pipelineNames { 80 found, err := target.Team().ArchivePipeline(pipelineName) 81 if err != nil { 82 return err 83 } 84 85 if found { 86 fmt.Printf("archived '%s'\n", pipelineName) 87 } else { 88 displayhelpers.Failf("pipeline '%s' not found\n", pipelineName) 89 } 90 } 91 92 return nil 93 } 94 95 func (command ArchivePipelineCommand) confirmArchive(pipelines []string) bool { 96 if command.SkipInteractive { 97 return true 98 } 99 100 if len(pipelines) > 1 { 101 command.printPipelinesTable(pipelines) 102 } 103 104 fmt.Printf("!!! archiving the pipeline will remove its configuration. Build history will be retained.\n\n") 105 106 var confirm bool 107 err := interact.NewInteraction(command.archivePrompt(pipelines)).Resolve(&confirm) 108 if err != nil { 109 return false 110 } 111 112 return confirm 113 } 114 115 func (ArchivePipelineCommand) printPipelinesTable(pipelines []string) { 116 table := ui.Table{Headers: ui.TableRow{{Contents: "pipelines", Color: color.New(color.Bold)}}} 117 for _, pipeline := range pipelines { 118 table.Data = append(table.Data, ui.TableRow{{Contents: pipeline}}) 119 } 120 table.Render(os.Stdout, true) 121 fmt.Println() 122 } 123 124 func (ArchivePipelineCommand) archivePrompt(pipelines []string) string { 125 if len(pipelines) == 1 { 126 return fmt.Sprintf("archive pipeline '%s'?", pipelines[0]) 127 } 128 return fmt.Sprintf("archive %d pipelines?", len(pipelines)) 129 }