github.com/jfrog/jfrog-cli-go@v1.22.1-0.20200318093948-4826ef344ffd/artifactory/commands/golang/gopublish.go (about) 1 package golang 2 3 import ( 4 "errors" 5 "os/exec" 6 "strings" 7 8 commandutils "github.com/jfrog/jfrog-cli-go/artifactory/commands/utils" 9 "github.com/jfrog/jfrog-cli-go/artifactory/utils" 10 "github.com/jfrog/jfrog-cli-go/artifactory/utils/golang" 11 "github.com/jfrog/jfrog-cli-go/artifactory/utils/golang/project" 12 _go "github.com/jfrog/jfrog-client-go/artifactory/services/go" 13 "github.com/jfrog/jfrog-client-go/utils/errorutils" 14 "github.com/jfrog/jfrog-client-go/utils/version" 15 ) 16 17 const minSupportedArtifactoryVersion = "6.2.0" 18 19 type GoPublishCommand struct { 20 publishPackage bool 21 buildConfiguration *utils.BuildConfiguration 22 dependencies string 23 version string 24 result *commandutils.Result 25 utils.RepositoryConfig 26 } 27 28 func NewGoPublishCommand() *GoPublishCommand { 29 return &GoPublishCommand{result: new(commandutils.Result)} 30 } 31 32 func (gpc *GoPublishCommand) Result() *commandutils.Result { 33 return gpc.result 34 } 35 36 func (gpc *GoPublishCommand) SetVersion(version string) *GoPublishCommand { 37 gpc.version = version 38 return gpc 39 } 40 41 func (gpc *GoPublishCommand) SetDependencies(dependencies string) *GoPublishCommand { 42 gpc.dependencies = dependencies 43 return gpc 44 } 45 46 func (gpc *GoPublishCommand) SetBuildConfiguration(buildConfiguration *utils.BuildConfiguration) *GoPublishCommand { 47 gpc.buildConfiguration = buildConfiguration 48 return gpc 49 } 50 51 func (gpc *GoPublishCommand) SetPublishPackage(publishPackage bool) *GoPublishCommand { 52 gpc.publishPackage = publishPackage 53 return gpc 54 } 55 56 func (gpc *GoPublishCommand) Run() error { 57 err := validatePrerequisites() 58 if err != nil { 59 return err 60 } 61 62 err = golang.LogGoVersion() 63 if err != nil { 64 return err 65 } 66 67 rtDetails, err := gpc.RtDetails() 68 if errorutils.CheckError(err) != nil { 69 return err 70 } 71 serviceManager, err := utils.CreateServiceManager(rtDetails, false) 72 if err != nil { 73 return err 74 } 75 artifactoryVersion, err := serviceManager.GetConfig().GetCommonDetails().GetVersion() 76 if err != nil { 77 return err 78 } 79 80 version := version.NewVersion(artifactoryVersion) 81 if !version.AtLeast(minSupportedArtifactoryVersion) { 82 return errorutils.CheckError(errors.New("This operation requires Artifactory version 6.2.0 or higher.")) 83 } 84 85 buildName := gpc.buildConfiguration.BuildName 86 buildNumber := gpc.buildConfiguration.BuildNumber 87 isCollectBuildInfo := len(buildName) > 0 && len(buildNumber) > 0 88 if isCollectBuildInfo { 89 err = utils.SaveBuildGeneralDetails(buildName, buildNumber) 90 if err != nil { 91 return err 92 } 93 } 94 95 goProject, err := project.Load(gpc.version) 96 if err != nil { 97 return err 98 } 99 100 // Publish the package to Artifactory 101 if gpc.publishPackage { 102 err = goProject.PublishPackage(gpc.TargetRepo(), buildName, buildNumber, serviceManager) 103 if err != nil { 104 return err 105 } 106 } 107 108 result := gpc.Result() 109 if gpc.dependencies != "" { 110 // Publish the package dependencies to Artifactory 111 depsList := strings.Split(gpc.dependencies, ",") 112 err = goProject.LoadDependencies() 113 if err != nil { 114 return err 115 } 116 succeeded, failed, err := goProject.PublishDependencies(gpc.TargetRepo(), serviceManager, depsList) 117 result.SetSuccessCount(succeeded) 118 result.SetFailCount(failed) 119 if err != nil { 120 return err 121 } 122 } 123 if gpc.publishPackage { 124 result.SetSuccessCount(result.SuccessCount() + 1) 125 } 126 127 // Publish the build-info to Artifactory 128 if isCollectBuildInfo { 129 if len(goProject.Dependencies()) == 0 { 130 // No dependencies were published but those dependencies need to be loaded for the build info. 131 goProject.LoadDependencies() 132 } 133 err = goProject.CreateBuildInfoDependencies(version.AtLeast(_go.ArtifactoryMinSupportedVersionForInfoFile)) 134 if err != nil { 135 return err 136 } 137 err = utils.SaveBuildInfo(buildName, buildNumber, goProject.BuildInfo(true, gpc.buildConfiguration.Module)) 138 } 139 140 return err 141 } 142 143 func (gpc *GoPublishCommand) CommandName() string { 144 return "rt_go_publish" 145 } 146 147 func validatePrerequisites() error { 148 _, err := exec.LookPath("go") 149 if err != nil { 150 return errorutils.CheckError(err) 151 } 152 return nil 153 }