github.com/jfrog/jfrog-cli-go@v1.22.1-0.20200318093948-4826ef344ffd/artifactory/commands/generic/gitlfsclean.go (about) 1 package generic 2 3 import ( 4 "fmt" 5 "github.com/jfrog/jfrog-cli-go/artifactory/utils" 6 "github.com/jfrog/jfrog-cli-go/utils/cliutils" 7 "github.com/jfrog/jfrog-client-go/artifactory/services" 8 clientutils "github.com/jfrog/jfrog-client-go/artifactory/services/utils" 9 "github.com/jfrog/jfrog-client-go/utils/errorutils" 10 "github.com/jfrog/jfrog-client-go/utils/log" 11 ) 12 13 type GitLfsCommand struct { 14 GenericCommand 15 configuration *GitLfsCleanConfiguration 16 } 17 18 func NewGitLfsCommand() *GitLfsCommand { 19 return &GitLfsCommand{GenericCommand: *NewGenericCommand()} 20 } 21 22 func (glc *GitLfsCommand) Configuration() *GitLfsCleanConfiguration { 23 return glc.configuration 24 } 25 26 func (glc *GitLfsCommand) SetConfiguration(configuration *GitLfsCleanConfiguration) *GitLfsCommand { 27 glc.configuration = configuration 28 return glc 29 } 30 31 func (glc *GitLfsCommand) Run() error { 32 rtDetails, err := glc.RtDetails() 33 if errorutils.CheckError(err) != nil { 34 return err 35 } 36 servicesManager, err := utils.CreateServiceManager(rtDetails, glc.DryRun()) 37 if err != nil { 38 return err 39 } 40 41 gitLfsCleanParams := getGitLfsCleanParams(glc.configuration) 42 43 filesToDelete, err := servicesManager.GetUnreferencedGitLfsFiles(gitLfsCleanParams) 44 45 if err != nil || len(filesToDelete) < 1 { 46 return err 47 } 48 49 if glc.configuration.Quiet { 50 err = glc.deleteLfsFilesFromArtifactory(filesToDelete) 51 return err 52 } 53 return glc.interactiveDeleteLfsFiles(filesToDelete) 54 } 55 56 func (glc *GitLfsCommand) CommandName() string { 57 return "rt_git_lfs_clean" 58 } 59 60 func (glc *GitLfsCommand) deleteLfsFilesFromArtifactory(deleteItems []clientutils.ResultItem) error { 61 log.Info("Deleting", len(deleteItems), "files from", glc.configuration.Repo, "...") 62 servicesManager, err := utils.CreateServiceManager(glc.rtDetails, glc.DryRun()) 63 if err != nil { 64 return err 65 } 66 _, err = servicesManager.DeleteFiles(deleteItems) 67 if err != nil { 68 return errorutils.CheckError(err) 69 } 70 return nil 71 } 72 73 type GitLfsCleanConfiguration struct { 74 Quiet bool 75 Refs string 76 Repo string 77 GitPath string 78 } 79 80 func getGitLfsCleanParams(configuration *GitLfsCleanConfiguration) (gitLfsCleanParams services.GitLfsCleanParams) { 81 gitLfsCleanParams = services.NewGitLfsCleanParams() 82 gitLfsCleanParams.GitPath = configuration.GitPath 83 gitLfsCleanParams.Refs = configuration.Refs 84 gitLfsCleanParams.Repo = configuration.Repo 85 return 86 } 87 88 func (glc *GitLfsCommand) interactiveDeleteLfsFiles(filesToDelete []clientutils.ResultItem) error { 89 for _, v := range filesToDelete { 90 fmt.Println(" " + v.Name) 91 } 92 confirmed := cliutils.InteractiveConfirm("Are you sure you want to delete the above files?\n" + 93 "You can avoid this confirmation message by adding --quiet to the command.") 94 if confirmed { 95 err := glc.deleteLfsFilesFromArtifactory(filesToDelete) 96 return err 97 } 98 return nil 99 }