github.com/jfrog/jfrog-cli-core/v2@v2.51.0/artifactory/commands/container/containermanagerbase.go (about) 1 package container 2 3 import ( 4 "github.com/jfrog/jfrog-cli-core/v2/artifactory/utils" 5 "github.com/jfrog/jfrog-cli-core/v2/artifactory/utils/container" 6 "github.com/jfrog/jfrog-cli-core/v2/common/build" 7 "github.com/jfrog/jfrog-cli-core/v2/utils/config" 8 clientutils "github.com/jfrog/jfrog-client-go/utils" 9 "github.com/jfrog/jfrog-client-go/utils/errorutils" 10 ) 11 12 const ( 13 // Artifactory 'MinRtVersionForRepoFetching' version and above, returns the image's repository in Artifactory. 14 MinRtVersionForRepoFetching = "7.33.3" 15 ) 16 17 type ContainerCommandBase struct { 18 image *container.Image 19 repo string 20 buildConfiguration *build.BuildConfiguration 21 serverDetails *config.ServerDetails 22 } 23 24 func (ccb *ContainerCommandBase) ImageTag() string { 25 return ccb.image.Name() 26 } 27 28 func (ccb *ContainerCommandBase) SetImageTag(imageTag string) *ContainerCommandBase { 29 ccb.image = container.NewImage(imageTag) 30 return ccb 31 } 32 33 // Returns the repository name that contains this image. 34 func (ccb *ContainerCommandBase) GetRepo() (string, error) { 35 // The repository name is saved after first calling this function. 36 if ccb.repo != "" { 37 return ccb.repo, nil 38 } 39 40 serviceManager, err := utils.CreateServiceManager(ccb.serverDetails, -1, 0, false) 41 if err != nil { 42 return "", err 43 } 44 ccb.repo, err = ccb.image.GetRemoteRepo(serviceManager) 45 return ccb.repo, err 46 } 47 48 func (ccb *ContainerCommandBase) SetRepo(repo string) *ContainerCommandBase { 49 ccb.repo = repo 50 return ccb 51 } 52 53 // Since 'RtMinVersion' version of Artifactory we can fetch the docker repository without the user input (witch is deprecated). 54 func (ccb *ContainerCommandBase) IsGetRepoSupported() (bool, error) { 55 serviceManager, err := utils.CreateServiceManager(ccb.serverDetails, -1, 0, false) 56 if err != nil { 57 return false, err 58 } 59 currentVersion, err := serviceManager.GetVersion() 60 if err != nil { 61 return false, err 62 } 63 err = clientutils.ValidateMinimumVersion(clientutils.Artifactory, currentVersion, MinRtVersionForRepoFetching) 64 return err == nil, nil 65 } 66 67 func (ccb *ContainerCommandBase) BuildConfiguration() *build.BuildConfiguration { 68 return ccb.buildConfiguration 69 } 70 71 func (ccb *ContainerCommandBase) SetBuildConfiguration(buildConfiguration *build.BuildConfiguration) *ContainerCommandBase { 72 ccb.buildConfiguration = buildConfiguration 73 return ccb 74 } 75 76 func (ccb *ContainerCommandBase) ServerDetails() *config.ServerDetails { 77 return ccb.serverDetails 78 } 79 80 func (ccb *ContainerCommandBase) SetServerDetails(serverDetails *config.ServerDetails) *ContainerCommandBase { 81 ccb.serverDetails = serverDetails 82 return ccb 83 } 84 85 func (ccb *ContainerCommandBase) init() error { 86 toCollect, err := ccb.buildConfiguration.IsCollectBuildInfo() 87 if err != nil || !toCollect { 88 return err 89 } 90 if ccb.repo != "" { 91 return nil 92 } 93 // Check we have all we need to collect build-info. 94 ok, err := ccb.IsGetRepoSupported() 95 if err != nil { 96 return err 97 } 98 if !ok { 99 return errorutils.CheckErrorf("Collecting docker build-info with this command requires Artifactory version %s or higher", MinRtVersionForRepoFetching) 100 } 101 return nil 102 }