-
Notifications
You must be signed in to change notification settings - Fork 786
/
delete_repo.go
177 lines (158 loc) · 5.1 KB
/
delete_repo.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package cmd
import (
"fmt"
"io"
"strings"
"github.com/jenkins-x/jx/pkg/auth"
"github.com/jenkins-x/jx/pkg/gits"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1"
"gopkg.in/AlecAivazis/survey.v1/terminal"
)
var (
deleteRepoLong = templates.LongDesc(`
Deletes one or more repositories.
This command will require the delete repo role on your Persona Access Token.
Note that command will ask for confirmation before doing anything!
`)
deleteRepoExample = templates.Examples(`
# Selects the repositories to delete from the given GitHub organisation
jx delete repo --github --org myname
# Selects all the repositories in organisation myname that contain 'foo'
# you get a chance to select which ones not to delete
jx delete repo --github --org myname --all --filter foo
`)
)
// DeleteRepoOptions the options for the create spring command
type DeleteRepoOptions struct {
CreateOptions
Organisation string
Repositories []string
GitHost string
GitHub bool
SelectAll bool
SelectFilter string
}
// NewCmdDeleteRepo creates a command object for the "delete repo" command
func NewCmdDeleteRepo(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
options := &DeleteRepoOptions{
CreateOptions: CreateOptions{
CommonOptions: CommonOptions{
Factory: f,
In: in,
Out: out,
Err: errOut,
},
},
}
cmd := &cobra.Command{
Use: "repo",
Short: "Deletes one or more Git repositories",
Aliases: []string{"repository"},
Long: deleteRepoLong,
Example: deleteRepoExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
//addDeleteFlags(cmd, &options.CreateOptions)
cmd.Flags().StringVarP(&options.Organisation, "org", "o", "", "Specify the Git provider organisation that includes the repository to delete")
cmd.Flags().StringArrayVarP(&options.Repositories, "name", "n", []string{}, "Specify the Git repository names to delete")
cmd.Flags().StringVarP(&options.GitHost, "git-host", "g", "", "The Git server host if not using GitHub")
cmd.Flags().BoolVarP(&options.GitHub, "github", "", false, "If you wish to pick the repositories from GitHub to import")
cmd.Flags().BoolVarP(&options.SelectAll, "all", "a", false, "If selecting projects to delete from a Git provider this defaults to selecting them all")
cmd.Flags().StringVarP(&options.SelectFilter, "filter", "f", "", "If selecting projects to delete from a Git provider this filters the list of repositories")
cmd.Flags().BoolVarP(&options.BatchMode, "batch-mode", "b", false, "Run without being prompted. WARNING! You will not be asked to confirm deletions if you use this flag.")
return cmd
}
// Run implements the command
func (o *DeleteRepoOptions) Run() error {
surveyOpts := survey.WithStdio(o.In, o.Out, o.Err)
authConfigSvc, err := o.CreateGitAuthConfigService()
if err != nil {
return err
}
var server *auth.AuthServer
config := authConfigSvc.Config()
if o.GitHub {
server = config.GetOrCreateServer(gits.GitHubURL)
} else {
if o.GitHost != "" {
server = config.GetOrCreateServer(o.GitHost)
} else {
server, err = config.PickServer("Pick the Git server to search for repositories", o.BatchMode, o.In, o.Out, o.Err)
if err != nil {
return err
}
}
}
if server == nil {
return fmt.Errorf("No Git server provided")
}
userAuth, err := config.PickServerUserAuth(server, "Git user name", o.BatchMode, "", o.In, o.Out, o.Err)
if err != nil {
return err
}
provider, err := gits.CreateProvider(server, userAuth, o.Git())
if err != nil {
return err
}
username := userAuth.Username
org := o.Organisation
if org == "" {
org, err = gits.PickOrganisation(provider, username, o.In, o.Out, o.Err)
if err != nil {
return err
}
}
if org == "" {
org = username
}
names := o.Repositories
if len(names) == 0 {
repos, err := gits.PickRepositories(provider, org, "Which repositories do you want to delete:", o.SelectAll, o.SelectFilter, o.In, o.Out, o.Err)
if err != nil {
return err
}
for _, r := range repos {
names = append(names, r.Name)
}
}
if !o.BatchMode {
log.Warnf("You are about to delete these repositories '%s' on the Git provider. This operation CANNOT be undone!",
strings.Join(names, ","))
flag := false
prompt := &survey.Confirm{
Message: "Are you sure you want to delete these all these repositories?",
Default: false,
}
err = survey.AskOne(prompt, &flag, nil, surveyOpts)
if err != nil {
return err
}
if !flag {
return nil
}
}
owner := org
if owner == "" {
owner = username
}
info := util.ColorInfo
for _, name := range names {
err = provider.DeleteRepository(owner, name)
if err != nil {
log.Warnf("Ensure Git Token has delete repo permissions or manually delete, for GitHub check https://github.com/settings/tokens\n")
log.Warnf("%s\n", err)
} else {
log.Infof("Deleted repository %s/%s\n", info(owner), info(name))
}
}
return nil
}