-
Notifications
You must be signed in to change notification settings - Fork 58
/
gitlfsclean.go
108 lines (93 loc) · 3.12 KB
/
gitlfsclean.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 generic
import (
"fmt"
"github.com/jfrog/jfrog-cli-core/artifactory/utils"
"github.com/jfrog/jfrog-cli-core/utils/coreutils"
"github.com/jfrog/jfrog-client-go/artifactory/services"
clientutils "github.com/jfrog/jfrog-client-go/artifactory/services/utils"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
"github.com/jfrog/jfrog-client-go/utils/io/content"
"github.com/jfrog/jfrog-client-go/utils/log"
)
type GitLfsCommand struct {
GenericCommand
configuration *GitLfsCleanConfiguration
}
func NewGitLfsCommand() *GitLfsCommand {
return &GitLfsCommand{GenericCommand: *NewGenericCommand()}
}
func (glc *GitLfsCommand) Configuration() *GitLfsCleanConfiguration {
return glc.configuration
}
func (glc *GitLfsCommand) SetConfiguration(configuration *GitLfsCleanConfiguration) *GitLfsCommand {
glc.configuration = configuration
return glc
}
func (glc *GitLfsCommand) Run() error {
serverDetails, err := glc.ServerDetails()
if errorutils.CheckError(err) != nil {
return err
}
servicesManager, err := utils.CreateServiceManager(serverDetails, glc.retries, glc.DryRun())
if err != nil {
return err
}
gitLfsCleanParams := getGitLfsCleanParams(glc.configuration)
filesToDeleteReader, err := servicesManager.GetUnreferencedGitLfsFiles(gitLfsCleanParams)
if err != nil {
return err
}
defer filesToDeleteReader.Close()
length, err := filesToDeleteReader.Length()
if err != nil || length < 1 {
return err
}
if glc.configuration.Quiet {
return glc.deleteLfsFilesFromArtifactory(filesToDeleteReader)
}
return glc.interactiveDeleteLfsFiles(filesToDeleteReader)
}
func (glc *GitLfsCommand) CommandName() string {
return "rt_git_lfs_clean"
}
func (glc *GitLfsCommand) deleteLfsFilesFromArtifactory(deleteItems *content.ContentReader) error {
log.Info("Deleting", deleteItems.Length, "files from", glc.configuration.Repo, "...")
servicesManager, err := utils.CreateServiceManager(glc.serverDetails, glc.retries, glc.DryRun())
if err != nil {
return err
}
_, err = servicesManager.DeleteFiles(deleteItems)
if err != nil {
return errorutils.CheckError(err)
}
return nil
}
type GitLfsCleanConfiguration struct {
Quiet bool
Refs string
Repo string
GitPath string
}
func getGitLfsCleanParams(configuration *GitLfsCleanConfiguration) (gitLfsCleanParams services.GitLfsCleanParams) {
gitLfsCleanParams = services.NewGitLfsCleanParams()
gitLfsCleanParams.GitPath = configuration.GitPath
gitLfsCleanParams.Refs = configuration.Refs
gitLfsCleanParams.Repo = configuration.Repo
return
}
func (glc *GitLfsCommand) interactiveDeleteLfsFiles(filesToDelete *content.ContentReader) error {
for resultItem := new(clientutils.ResultItem); filesToDelete.NextRecord(resultItem) == nil; resultItem = new(clientutils.ResultItem) {
fmt.Println(" " + resultItem.Name)
}
if err := filesToDelete.GetError(); err != nil {
return err
}
filesToDelete.Reset()
confirmed := coreutils.AskYesNo("Are you sure you want to delete the above files?\n"+
"You can avoid this confirmation message by adding --quiet to the command.", false)
if confirmed {
err := glc.deleteLfsFilesFromArtifactory(filesToDelete)
return err
}
return nil
}