-
Notifications
You must be signed in to change notification settings - Fork 58
/
delete.go
81 lines (68 loc) · 2.04 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
package repository
import (
"path/filepath"
"strings"
rtUtils "github.com/jfrog/jfrog-cli-core/artifactory/utils"
"github.com/jfrog/jfrog-cli-core/utils/config"
"github.com/jfrog/jfrog-cli-core/utils/coreutils"
"github.com/jfrog/jfrog-client-go/artifactory"
)
type RepoDeleteCommand struct {
serverDetails *config.ServerDetails
repoPattern string
quiet bool
}
func NewRepoDeleteCommand() *RepoDeleteCommand {
return &RepoDeleteCommand{}
}
func (rdc *RepoDeleteCommand) SetRepoPattern(repoPattern string) *RepoDeleteCommand {
rdc.repoPattern = repoPattern
return rdc
}
func (rdc *RepoDeleteCommand) SetQuiet(quiet bool) *RepoDeleteCommand {
rdc.quiet = quiet
return rdc
}
func (rdc *RepoDeleteCommand) SetServerDetails(serverDetails *config.ServerDetails) *RepoDeleteCommand {
rdc.serverDetails = serverDetails
return rdc
}
func (rdc *RepoDeleteCommand) ServerDetails() (*config.ServerDetails, error) {
return rdc.serverDetails, nil
}
func (rdc *RepoDeleteCommand) CommandName() string {
return "rt_repo_delete"
}
func (rdc *RepoDeleteCommand) Run() (err error) {
servicesManager, err := rtUtils.CreateServiceManager(rdc.serverDetails, false)
if err != nil {
return err
}
// A single repo to be deleted
if !strings.Contains(rdc.repoPattern, "*") {
return rdc.deleteRepo(&servicesManager, rdc.repoPattern)
}
// A pattern for the repo name was received
repos, err := servicesManager.GetAllRepositories()
if err != nil {
return err
}
for _, repo := range *repos {
matched, err := filepath.Match(rdc.repoPattern, repo.Key)
if err != nil {
return err
}
if matched {
if err := rdc.deleteRepo(&servicesManager, repo.Key); err != nil {
return err
}
}
}
return nil
}
func (rdc *RepoDeleteCommand) deleteRepo(servicesManager *artifactory.ArtifactoryServicesManager, repoKey string) error {
if !rdc.quiet && !coreutils.AskYesNo("Are you sure you want to permanently delete the repository "+repoKey+" including all of its content?", false) {
return nil
}
return (*servicesManager).DeleteRepository(repoKey)
}