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