github.com/jfrog/jfrog-cli-core/v2@v2.51.0/artifactory/commands/utils/transfer.go (about)

     1  package utils
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"github.com/gocarina/gocsv"
     7  	ioutils "github.com/jfrog/gofrog/io"
     8  	logutils "github.com/jfrog/jfrog-cli-core/v2/utils/log"
     9  	"github.com/jfrog/jfrog-client-go/http/jfroghttpclient"
    10  	"github.com/jfrog/jfrog-client-go/utils/errorutils"
    11  	"github.com/jfrog/jfrog-client-go/utils/io/httputils"
    12  	"net/http"
    13  	"time"
    14  )
    15  
    16  type ServerType string
    17  
    18  const (
    19  	Source                ServerType = "source"
    20  	Target                ServerType = "target"
    21  	PluginsExecuteRestApi            = "api/plugins/execute/"
    22  )
    23  
    24  func GetTransferPluginVersion(client *jfroghttpclient.JfrogHttpClient, url, pluginName string, serverType ServerType, rtDetails *httputils.HttpClientDetails) (string, error) {
    25  	versionResp, versionBody, _, err := client.SendGet(url, false, rtDetails)
    26  	if err != nil {
    27  		return "", err
    28  	}
    29  	if versionResp.StatusCode == http.StatusOK {
    30  		verRes := &VersionResponse{}
    31  		err = json.Unmarshal(versionBody, verRes)
    32  		if err != nil {
    33  			return "", errorutils.CheckError(err)
    34  		}
    35  		return verRes.Version, nil
    36  	}
    37  
    38  	messageFormat := fmt.Sprintf("Response from Artifactory: %s.\n%s\n", versionResp.Status, versionBody)
    39  	if versionResp.StatusCode == http.StatusNotFound {
    40  		return "", errorutils.CheckErrorf("%sIt looks like the %s plugin is not installed on the %s server.", messageFormat, pluginName, serverType)
    41  	} else {
    42  		// 403 if the user is not admin, 500+ if there is a server error
    43  		return "", errorutils.CheckErrorf(messageFormat)
    44  	}
    45  }
    46  
    47  type VersionResponse struct {
    48  	Version string `json:"version,omitempty"`
    49  }
    50  
    51  func CreateCSVFile(filePrefix string, items interface{}, timeStarted time.Time) (csvPath string, err error) {
    52  	// Create CSV file
    53  	summaryCsv, err := logutils.CreateCustomLogFile(fmt.Sprintf("%s-%s.csv", filePrefix, timeStarted.Format(logutils.DefaultLogTimeLayout)))
    54  	if err != nil {
    55  		return
    56  	}
    57  	csvPath = summaryCsv.Name()
    58  	defer ioutils.Close(summaryCsv, &err)
    59  	// Marshal JSON typed items array to CSV file
    60  	err = errorutils.CheckError(gocsv.MarshalFile(items, summaryCsv))
    61  	return
    62  }