github.com/cobalt77/jfrog-client-go@v0.14.5/artifactory/services/go/publishzipandmod.go (about)

     1  package _go
     2  
     3  import (
     4  	"net/http"
     5  	"net/url"
     6  	"strings"
     7  
     8  	rthttpclient "github.com/cobalt77/jfrog-client-go/artifactory/httpclient"
     9  	"github.com/cobalt77/jfrog-client-go/artifactory/services/utils"
    10  	"github.com/cobalt77/jfrog-client-go/auth"
    11  	"github.com/cobalt77/jfrog-client-go/utils/errorutils"
    12  	"github.com/cobalt77/jfrog-client-go/utils/io/fileutils"
    13  	"github.com/cobalt77/jfrog-client-go/utils/io/httputils"
    14  	"github.com/cobalt77/jfrog-client-go/utils/version"
    15  )
    16  
    17  func init() {
    18  	register(&publishZipAndModApi{})
    19  }
    20  
    21  const ArtifactoryMinSupportedVersionForInfoFile = "6.10.0"
    22  
    23  // Support for Artifactory 6.6.1 and above API
    24  type publishZipAndModApi struct {
    25  	artifactoryVersion string
    26  	clientDetails      httputils.HttpClientDetails
    27  	client             *rthttpclient.ArtifactoryHttpClient
    28  }
    29  
    30  func (pwa *publishZipAndModApi) isCompatible(artifactoryVersion string) bool {
    31  	propertiesApi := "6.6.1"
    32  	version := version.NewVersion(artifactoryVersion)
    33  	pwa.artifactoryVersion = artifactoryVersion
    34  	return version.AtLeast(propertiesApi)
    35  }
    36  
    37  func (pwa *publishZipAndModApi) PublishPackage(params GoParams, client *rthttpclient.ArtifactoryHttpClient, ArtDetails auth.ServiceDetails) error {
    38  	url, err := utils.BuildArtifactoryUrl(ArtDetails.GetUrl(), "api/go/"+params.GetTargetRepo(), make(map[string]string))
    39  	if err != nil {
    40  		return err
    41  	}
    42  	pwa.clientDetails = ArtDetails.CreateHttpClientDetails()
    43  	pwa.client = client
    44  	moduleId := strings.Split(params.GetModuleId(), ":")
    45  	// Upload zip file
    46  	err = pwa.upload(params.GetZipPath(), moduleId[0], params.GetVersion(), params.GetProps(), ".zip", url)
    47  	if err != nil {
    48  		return err
    49  	}
    50  	// Upload mod file
    51  	err = pwa.upload(params.GetModPath(), moduleId[0], params.GetVersion(), params.GetProps(), ".mod", url)
    52  	if err != nil {
    53  		return err
    54  	}
    55  	if version.NewVersion(pwa.artifactoryVersion).AtLeast(ArtifactoryMinSupportedVersionForInfoFile) && params.GetInfoPath() != "" {
    56  		// Upload info file. This supported from Artifactory version 6.10.0 and above
    57  		return pwa.upload(params.GetInfoPath(), moduleId[0], params.GetVersion(), params.GetProps(), ".info", url)
    58  	}
    59  	return nil
    60  }
    61  
    62  func addGoVersion(version string, urlPath *string) {
    63  	*urlPath += ";go.version=" + url.QueryEscape(version)
    64  }
    65  
    66  // localPath - The location of the file on the file system.
    67  // moduleId - The name of the module for example github.com/cobalt77/jfrog-client-go.
    68  // version - The version of the project that being uploaded.
    69  // props - The properties to be assigned for each artifact
    70  // ext - The extension of the file: zip, mod, info. This extension will be joined with the version for the path. For example v1.2.3.info or v1.2.3.zip
    71  // urlPath - The url including the repository. For example: http://127.0.0.1/artifactory/api/go/go-local
    72  func (pwa *publishZipAndModApi) upload(localPath, moduleId, version, props, ext, urlPath string) error {
    73  	err := CreateUrlPath(moduleId, version, props, ext, &urlPath)
    74  	if err != nil {
    75  		return err
    76  	}
    77  	addGoVersion(version, &urlPath)
    78  	details, err := fileutils.GetFileDetails(localPath)
    79  	if err != nil {
    80  		return err
    81  	}
    82  	utils.AddChecksumHeaders(pwa.clientDetails.Headers, details)
    83  	resp, _, err := pwa.client.UploadFile(localPath, urlPath, "", &pwa.clientDetails, GoUploadRetries, nil)
    84  	if err != nil {
    85  		return err
    86  	}
    87  	return errorutils.CheckResponseStatus(resp, http.StatusCreated)
    88  }