-
Notifications
You must be signed in to change notification settings - Fork 51
/
delete.go
108 lines (91 loc) · 3.25 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
package deletecmd
import (
"path/filepath"
"github.com/jenkins-x-plugins/jx-gitops/pkg/apis/gitops/v1alpha1"
"github.com/jenkins-x-plugins/jx-gitops/pkg/sourceconfigs"
"github.com/jenkins-x/jx-api/v4/pkg/client/clientset/versioned"
"github.com/jenkins-x/jx-helpers/v3/pkg/cobras/helper"
"github.com/jenkins-x/jx-helpers/v3/pkg/cobras/templates"
"github.com/jenkins-x/jx-helpers/v3/pkg/files"
"github.com/jenkins-x/jx-helpers/v3/pkg/options"
"github.com/jenkins-x/jx-helpers/v3/pkg/termcolor"
"github.com/jenkins-x/jx-helpers/v3/pkg/yamls"
"github.com/jenkins-x/jx-logging/v3/pkg/log"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
var (
info = termcolor.ColorInfo
cmdLong = templates.LongDesc(`
Add one or more repositories to the SourceConfig
`)
cmdExample = templates.Examples(`
# deletes a repository by name from the '.jx/gitops/source-config.yaml' file
jx gitops repository delete --name myrepo
# deletes a repository by name and owner from the '.jx/gitops/source-config.yaml' file
jx gitops repository delete --name myrepo --owner myowner
`)
)
// LabelOptions the options for the command
type Options struct {
Owner string
Name string
Dir string
ConfigFile string
Namespace string
JXClient versioned.Interface
ExplicitMode bool
}
// NewCmdDeleteRepository creates a command object for the command
func NewCmdDeleteRepository() (*cobra.Command, *Options) {
o := &Options{}
cmd := &cobra.Command{
Use: "delete",
Aliases: []string{"remove", "rm", "del"},
Short: "Deletes a repository from the source configuration",
Long: cmdLong,
Example: cmdExample,
Run: func(cmd *cobra.Command, args []string) {
err := o.Run()
helper.CheckErr(err)
},
}
cmd.Flags().StringVarP(&o.Dir, "dir", "d", ".", "the directory look for the 'jx-requirements.yml` file")
cmd.Flags().StringVarP(&o.ConfigFile, "config", "c", "", "the configuration file to load for the repository configurations. If not specified we look in .jx/gitops/source-repositories.yaml")
cmd.Flags().StringVarP(&o.Name, "name", "n", "", "the name of the repository to remove")
cmd.Flags().StringVarP(&o.Owner, "owner", "o", "", "the owner of the repository to remove")
return cmd, o
}
// Run transforms the YAML files
func (o *Options) Run() error {
if o.ConfigFile == "" {
o.ConfigFile = filepath.Join(o.Dir, ".jx", "gitops", v1alpha1.SourceConfigFileName)
}
if o.Name == "" {
return options.MissingOption("name")
}
exists, err := files.FileExists(o.ConfigFile)
if err != nil {
return errors.Wrapf(err, "failed to check if file exists %s", o.ConfigFile)
}
config := &v1alpha1.SourceConfig{}
if !exists {
log.Logger().Infof("file %s does not exist", termcolor.ColorStatus(o.ConfigFile))
return nil
}
err = yamls.LoadFile(o.ConfigFile, config)
if err != nil {
return errors.Wrapf(err, "failed to load file %s", o.ConfigFile)
}
modified := sourceconfigs.RemoveRepository(config, o.Owner, o.Name)
if !modified {
log.Logger().Infof("repository %s not found in file %s", info(o.Name), info(o.ConfigFile))
return nil
}
err = yamls.SaveFile(config, o.ConfigFile)
if err != nil {
return errors.Wrapf(err, "failed to save file %s", o.ConfigFile)
}
log.Logger().Infof("modified file %s", info(o.ConfigFile))
return nil
}