-
Notifications
You must be signed in to change notification settings - Fork 50
/
delete.go
151 lines (123 loc) · 4.27 KB
/
delete.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package deletecmd
import (
"github.com/jenkins-x-plugins/jx-gitops/pkg/helmfiles"
"github.com/jenkins-x/jx-helpers/v3/pkg/cmdrunner"
"github.com/jenkins-x/jx-helpers/v3/pkg/cobras/templates"
"github.com/jenkins-x/jx-helpers/v3/pkg/gitclient"
"github.com/jenkins-x/jx-helpers/v3/pkg/gitclient/cli"
"github.com/jenkins-x/jx-helpers/v3/pkg/options"
"github.com/jenkins-x/jx-logging/v3/pkg/log"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
// Options the flags for updating webhooks
type Options struct {
options.BaseOptions
Details helmfiles.ChartDetails
Dir string
Helmfile string
GitCommitMessage string
DoGitCommit bool
Gitter gitclient.Interface
CommandRunner cmdrunner.CommandRunner
}
var (
cmdLong = templates.LongDesc(`
Deletes a chart from the helmfiles in one or all namespaces
`)
cmdExample = templates.Examples(`
# deletes the chart from all namespaces
jx gitops helmfile delete --chart my-chart
# deletes the chart from a specific namespace
jx gitops helmfile delete --chart my-chart --namespace jx-staging
# deletes the chart with the repo prefix from a specific namespace
jx gitops helmfile delete --chart myrepo/my-chart --namespace jx-staging
`)
)
func NewCmdHelmfileDelete() (*cobra.Command, *Options) {
o := &Options{}
cmd := &cobra.Command{
Use: "delete",
Aliases: []string{"remove", "rm", "del"},
Short: "Deletes a chart from the helmfiles in one or all namespaces",
Long: cmdLong,
Example: cmdExample,
RunE: func(cmd *cobra.Command, args []string) error {
return o.Run()
},
}
o.BaseOptions.AddBaseFlags(cmd)
cmd.Flags().StringVarP(&o.Helmfile, "helmfile", "", "", "the helmfile to resolve. If not specified defaults to 'helmfile.yaml' in the dir")
cmd.Flags().StringVarP(&o.GitCommitMessage, "commit-message", "", "chore: generated kubernetes resources from helm chart", "the git commit message used")
cmd.Flags().StringVarP(&o.Dir, "dir", "d", ".", "the directory that contains the helmfile.yaml and helmfiles directory")
// chart flags
cmd.Flags().StringVarP(&o.Details.Chart, "chart", "c", "", "the name of the helm chart to remove")
cmd.Flags().StringVarP(&o.Details.Namespace, "namespace", "n", "", "the namespace to remove the chart from. If blank then remove from all namespaces")
// git commit stuff....
cmd.Flags().BoolVarP(&o.DoGitCommit, "git-commit", "", false, "if set then the template command will git commit the modified helmfile.yaml files")
return cmd, o
}
// Validate validates the options and populates any missing values
func (o *Options) Validate() error {
err := o.BaseOptions.Validate()
if err != nil {
return errors.Wrapf(err, "failed to validate")
}
if o.Details.Chart == "" {
return options.MissingOption("chart")
}
if o.Helmfile == "" {
o.Helmfile = "helmfile.yaml"
}
if o.GitCommitMessage == "" {
o.GitCommitMessage = "chore: remove chart " + o.Details.Chart
}
return nil
}
// Run runs the command
func (o *Options) Run() error {
err := o.Validate()
if err != nil {
return errors.Wrapf(err, "failed to validate options")
}
hfNames, err := helmfiles.GatherHelmfiles(o.Helmfile, o.Dir)
if err != nil {
return errors.Wrapf(err, "failed to gather target helmfiles from %s", o.Dir)
}
editor, err := helmfiles.NewEditor(o.Dir, hfNames)
if err != nil {
return errors.Wrapf(err, "failed to create helmfile editor")
}
err = editor.DeleteChart(&o.Details)
if err != nil {
return errors.Wrapf(err, "failed to add chart %s", o.Details.Chart)
}
err = editor.Save()
if err != nil {
return errors.Wrapf(err, "failed to save modified files")
}
if !o.DoGitCommit {
return nil
}
log.Logger().Infof("committing changes: %s", o.GitCommitMessage)
err = o.GitCommit(o.Dir, o.GitCommitMessage)
if err != nil {
return errors.Wrapf(err, "failed to commit changes")
}
return nil
}
// Git returns the gitter - lazily creating one if required
func (o *Options) Git() gitclient.Interface {
if o.Gitter == nil {
o.Gitter = cli.NewCLIClient("", o.CommandRunner)
}
return o.Gitter
}
func (o *Options) GitCommit(outDir, commitMessage string) error {
gitter := o.Git()
err := gitclient.CommitIfChanges(gitter, outDir, commitMessage)
if err != nil {
return errors.Wrapf(err, "failed to commit changes to git in dir %s", outDir)
}
return nil
}