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