github.com/jfrog/jfrog-client-go@v1.40.2/distribution/services/version.go (about)

     1  package services
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"net/http"
     7  	"strings"
     8  
     9  	"github.com/jfrog/jfrog-client-go/auth"
    10  	"github.com/jfrog/jfrog-client-go/http/jfroghttpclient"
    11  	"github.com/jfrog/jfrog-client-go/utils/errorutils"
    12  )
    13  
    14  type VersionService struct {
    15  	client      *jfroghttpclient.JfrogHttpClient
    16  	DistDetails auth.ServiceDetails
    17  }
    18  
    19  func NewVersionService(client *jfroghttpclient.JfrogHttpClient) *VersionService {
    20  	return &VersionService{client: client}
    21  }
    22  
    23  func (vs *VersionService) GetDistDetails() auth.ServiceDetails {
    24  	return vs.DistDetails
    25  }
    26  
    27  func (vs *VersionService) GetDistributionVersion() (string, error) {
    28  	httpDetails := vs.DistDetails.CreateHttpClientDetails()
    29  	resp, body, _, err := vs.client.SendGet(vs.DistDetails.GetUrl()+"api/v1/system/info", true, &httpDetails)
    30  	if err != nil {
    31  		return "", errors.New("failed while attempting to get JFrog Distribution version: " + err.Error())
    32  	}
    33  
    34  	if err = errorutils.CheckResponseStatusWithBody(resp, body, http.StatusOK); err != nil {
    35  		return "", errors.New("got unexpected server response while attempting to get JFrog Distribution version:\n" + err.Error())
    36  	}
    37  	var version distributionVersion
    38  	if err = json.Unmarshal(body, &version); err != nil {
    39  		return "", errorutils.CheckErrorf("couldn't parse JFrog Distribution server version version response: " + err.Error())
    40  	}
    41  	return strings.TrimSpace(version.Version), nil
    42  }
    43  
    44  type distributionVersion struct {
    45  	Version string `json:"version,omitempty"`
    46  }