github.com/jfrog/jfrog-cli-go@v1.22.1-0.20200318093948-4826ef344ffd/artifactory/commands/golang/go.go (about)

     1  package golang
     2  
     3  import (
     4  	"github.com/jfrog/gocmd"
     5  	"github.com/jfrog/gocmd/params"
     6  	"github.com/jfrog/jfrog-cli-go/artifactory/utils"
     7  	"github.com/jfrog/jfrog-cli-go/artifactory/utils/golang"
     8  	"github.com/jfrog/jfrog-cli-go/artifactory/utils/golang/project"
     9  	"github.com/jfrog/jfrog-cli-go/utils/config"
    10  	"github.com/jfrog/jfrog-client-go/artifactory"
    11  	_go "github.com/jfrog/jfrog-client-go/artifactory/services/go"
    12  	"github.com/jfrog/jfrog-client-go/utils/version"
    13  )
    14  
    15  const GoCommandName = "rt_go"
    16  
    17  type GoCommand struct {
    18  	noRegistry         bool
    19  	publishDeps        bool
    20  	goArg              []string
    21  	buildConfiguration *utils.BuildConfiguration
    22  	deployerParams     *utils.RepositoryConfig
    23  	resolverParams     *utils.RepositoryConfig
    24  }
    25  
    26  func NewGoCommand() *GoCommand {
    27  	return &GoCommand{}
    28  }
    29  
    30  func (gc *GoCommand) SetResolverParams(resolverParams *utils.RepositoryConfig) *GoCommand {
    31  	gc.resolverParams = resolverParams
    32  	return gc
    33  }
    34  
    35  func (gc *GoCommand) SetDeployerParams(deployerParams *utils.RepositoryConfig) *GoCommand {
    36  	gc.deployerParams = deployerParams
    37  	return gc
    38  }
    39  
    40  func (gc *GoCommand) SetBuildConfiguration(buildConfiguration *utils.BuildConfiguration) *GoCommand {
    41  	gc.buildConfiguration = buildConfiguration
    42  	return gc
    43  }
    44  
    45  func (gc *GoCommand) SetNoRegistry(noRegistry bool) *GoCommand {
    46  	gc.noRegistry = noRegistry
    47  	return gc
    48  }
    49  
    50  func (gc *GoCommand) SetPublishDeps(publishDeps bool) *GoCommand {
    51  	gc.publishDeps = publishDeps
    52  	return gc
    53  }
    54  
    55  func (gc *GoCommand) SetGoArg(goArg []string) *GoCommand {
    56  	gc.goArg = goArg
    57  	return gc
    58  }
    59  
    60  func (gc *GoCommand) RtDetails() (*config.ArtifactoryDetails, error) {
    61  	if gc.deployerParams != nil && !gc.deployerParams.IsRtDetailsEmpty() {
    62  		return gc.deployerParams.RtDetails()
    63  	}
    64  	return gc.resolverParams.RtDetails()
    65  }
    66  
    67  func (gc *GoCommand) CommandName() string {
    68  	return GoCommandName
    69  }
    70  
    71  func (gc *GoCommand) Run() error {
    72  	err := golang.LogGoVersion()
    73  	if err != nil {
    74  		return err
    75  	}
    76  	buildName := gc.buildConfiguration.BuildName
    77  	buildNumber := gc.buildConfiguration.BuildNumber
    78  	isCollectBuildInfo := len(buildName) > 0 && len(buildNumber) > 0
    79  	if isCollectBuildInfo {
    80  		err = utils.SaveBuildGeneralDetails(buildName, buildNumber)
    81  		if err != nil {
    82  			return err
    83  		}
    84  	}
    85  	// The version is not necessary because we are collecting the dependencies only.
    86  	goProject, err := project.Load("-")
    87  	if err != nil {
    88  		return err
    89  	}
    90  
    91  	resolverDetails, err := gc.resolverParams.RtDetails()
    92  	if err != nil {
    93  		return err
    94  	}
    95  	resolverServiceManager, err := utils.CreateServiceManager(resolverDetails, false)
    96  	if err != nil {
    97  		return err
    98  	}
    99  	resolverParams := &params.Params{}
   100  	resolverParams.SetRepo(gc.resolverParams.TargetRepo()).SetServiceManager(resolverServiceManager)
   101  	goInfo := &params.ResolverDeployer{}
   102  	goInfo.SetResolver(resolverParams)
   103  	var deployerServiceManager *artifactory.ArtifactoryServicesManager
   104  	if gc.publishDeps {
   105  		deployerDetails, err := gc.deployerParams.RtDetails()
   106  		if err != nil {
   107  			return err
   108  		}
   109  		deployerServiceManager, err = utils.CreateServiceManager(deployerDetails, false)
   110  		if err != nil {
   111  			return err
   112  		}
   113  		deployerParams := &params.Params{}
   114  		deployerParams.SetRepo(gc.deployerParams.TargetRepo()).SetServiceManager(deployerServiceManager)
   115  		goInfo.SetDeployer(deployerParams)
   116  	}
   117  
   118  	err = gocmd.RunWithFallbacksAndPublish(gc.goArg, gc.noRegistry, gc.publishDeps, goInfo)
   119  	if err != nil {
   120  		return err
   121  	}
   122  	if isCollectBuildInfo {
   123  		includeInfoFiles, err := shouldIncludeInfoFiles(deployerServiceManager, resolverServiceManager)
   124  		if err != nil {
   125  			return err
   126  		}
   127  		err = goProject.LoadDependencies()
   128  		if err != nil {
   129  			return err
   130  		}
   131  		err = goProject.CreateBuildInfoDependencies(includeInfoFiles)
   132  		if err != nil {
   133  			return err
   134  		}
   135  		err = utils.SaveBuildInfo(buildName, buildNumber, goProject.BuildInfo(false, gc.buildConfiguration.Module))
   136  	}
   137  
   138  	return err
   139  }
   140  
   141  // Returns true/false if info files should be included in the build info.
   142  func shouldIncludeInfoFiles(deployerServiceManager *artifactory.ArtifactoryServicesManager, resolverServiceManager *artifactory.ArtifactoryServicesManager) (bool, error) {
   143  	var artifactoryVersion string
   144  	var err error
   145  	if deployerServiceManager != nil {
   146  		artifactoryVersion, err = deployerServiceManager.GetConfig().GetCommonDetails().GetVersion()
   147  	} else {
   148  		artifactoryVersion, err = resolverServiceManager.GetConfig().GetCommonDetails().GetVersion()
   149  	}
   150  	if err != nil {
   151  		return false, err
   152  	}
   153  	version := version.NewVersion(artifactoryVersion)
   154  	includeInfoFiles := version.AtLeast(_go.ArtifactoryMinSupportedVersionForInfoFile)
   155  	return includeInfoFiles, nil
   156  }