github.com/jfrog/jfrog-cli-core@v1.12.1/artifactory/commands/golang/golegacypublish.go (about)

     1  package golang
     2  
     3  import (
     4  	"errors"
     5  	"strings"
     6  
     7  	commandutils "github.com/jfrog/jfrog-cli-core/artifactory/commands/utils"
     8  	"github.com/jfrog/jfrog-cli-core/artifactory/utils"
     9  	"github.com/jfrog/jfrog-cli-core/artifactory/utils/golang"
    10  	"github.com/jfrog/jfrog-cli-core/artifactory/utils/golang/project"
    11  	_go "github.com/jfrog/jfrog-client-go/artifactory/services/go"
    12  	"github.com/jfrog/jfrog-client-go/utils/errorutils"
    13  	"github.com/jfrog/jfrog-client-go/utils/version"
    14  )
    15  
    16  type GoLegacyPublishCommand struct {
    17  	internalCommandName string
    18  	publishPackage      bool
    19  	*GoPublishCommandArgs
    20  }
    21  
    22  func NewGoLegacyPublishCommand() *GoLegacyPublishCommand {
    23  	return &GoLegacyPublishCommand{GoPublishCommandArgs: &GoPublishCommandArgs{result: new(commandutils.Result)}, internalCommandName: "rt_go_legacy_publish"}
    24  }
    25  
    26  func (glpc *GoLegacyPublishCommand) CommandName() string {
    27  	return glpc.internalCommandName
    28  }
    29  
    30  func (glpc *GoLegacyPublishCommand) SetPublishPackage(publishPackage bool) *GoLegacyPublishCommand {
    31  	glpc.publishPackage = publishPackage
    32  	return glpc
    33  }
    34  
    35  func (glpc *GoLegacyPublishCommand) Run() error {
    36  	err := validatePrerequisites()
    37  	if err != nil {
    38  		return err
    39  	}
    40  
    41  	err = golang.LogGoVersion()
    42  	if err != nil {
    43  		return err
    44  	}
    45  
    46  	serverDetails, err := glpc.ServerDetails()
    47  	if errorutils.CheckError(err) != nil {
    48  		return err
    49  	}
    50  	serviceManager, err := utils.CreateServiceManager(serverDetails, -1, false)
    51  	if err != nil {
    52  		return err
    53  	}
    54  	artifactoryVersion, err := serviceManager.GetConfig().GetServiceDetails().GetVersion()
    55  	if err != nil {
    56  		return err
    57  	}
    58  
    59  	version := version.NewVersion(artifactoryVersion)
    60  	if !version.AtLeast(minSupportedArtifactoryVersion) {
    61  		return errorutils.CheckError(errors.New("This operation requires Artifactory version 6.2.0 or higher."))
    62  	}
    63  
    64  	buildName := glpc.buildConfiguration.BuildName
    65  	buildNumber := glpc.buildConfiguration.BuildNumber
    66  	projectKey := glpc.buildConfiguration.Project
    67  	isCollectBuildInfo := len(buildName) > 0 && len(buildNumber) > 0
    68  	if isCollectBuildInfo {
    69  		err = utils.SaveBuildGeneralDetails(buildName, buildNumber, projectKey)
    70  		if err != nil {
    71  			return err
    72  		}
    73  	}
    74  
    75  	goProject, err := project.Load(glpc.version, "")
    76  	if err != nil {
    77  		return err
    78  	}
    79  
    80  	// Publish the package to Artifactory
    81  	if glpc.publishPackage {
    82  		_, err = goProject.PublishPackage(glpc.TargetRepo(), buildName, buildNumber, projectKey, serviceManager)
    83  		if err != nil {
    84  			return err
    85  		}
    86  	}
    87  
    88  	result := glpc.Result()
    89  	if glpc.dependencies != "" {
    90  		// Publish the package dependencies to Artifactory
    91  		depsList := strings.Split(glpc.dependencies, ",")
    92  		err = goProject.LoadDependencies()
    93  		if err != nil {
    94  			return err
    95  		}
    96  		succeeded, failed, err := goProject.PublishDependencies(glpc.TargetRepo(), serviceManager, depsList)
    97  		result.SetSuccessCount(succeeded)
    98  		result.SetFailCount(failed)
    99  		if err != nil {
   100  			return err
   101  		}
   102  	}
   103  	if glpc.publishPackage {
   104  		result.SetSuccessCount(result.SuccessCount() + 1)
   105  	}
   106  
   107  	// Publish the build-info to Artifactory
   108  	if isCollectBuildInfo {
   109  		if len(goProject.Dependencies()) == 0 {
   110  			// No dependencies were published but those dependencies need to be loaded for the build info.
   111  			goProject.LoadDependencies()
   112  		}
   113  		err = goProject.CreateBuildInfoDependencies(version.AtLeast(_go.ArtifactoryMinSupportedVersionForInfoFile))
   114  		if err != nil {
   115  			return err
   116  		}
   117  		err = utils.SaveBuildInfo(buildName, buildNumber, projectKey, goProject.BuildInfo(true, glpc.buildConfiguration.Module, glpc.RepositoryConfig.TargetRepo()))
   118  	}
   119  
   120  	return err
   121  }