github.com/cobalt77/jfrog-client-go@v0.14.5/bintray/services/packages/packages.go (about) 1 package packages 2 3 import ( 4 "encoding/json" 5 "errors" 6 "github.com/cobalt77/jfrog-client-go/bintray/auth" 7 "github.com/cobalt77/jfrog-client-go/httpclient" 8 clientutils "github.com/cobalt77/jfrog-client-go/utils" 9 "github.com/cobalt77/jfrog-client-go/utils/errorutils" 10 "github.com/cobalt77/jfrog-client-go/utils/log" 11 "net/http" 12 "path" 13 "strings" 14 ) 15 16 func NewService(client *httpclient.HttpClient) *PackageService { 17 us := &PackageService{client: client} 18 return us 19 } 20 21 func NewPackageParams() *Params { 22 return &Params{Path: &Path{}} 23 } 24 25 type PackageService struct { 26 client *httpclient.HttpClient 27 BintrayDetails auth.BintrayDetails 28 } 29 30 type Path struct { 31 Subject string 32 Repo string 33 Package string 34 } 35 36 type Params struct { 37 *Path 38 Desc string 39 Labels string 40 Licenses string 41 CustomLicenses string 42 VcsUrl string 43 WebsiteUrl string 44 IssueTrackerUrl string 45 GithubRepo string 46 GithubReleaseNotesFile string 47 PublicDownloadNumbers bool 48 PublicStats bool 49 } 50 51 func (ps *PackageService) Create(params *Params) error { 52 log.Info("Creating package...") 53 resp, body, err := ps.doCreatePackage(params) 54 if err != nil { 55 return err 56 } 57 if resp.StatusCode != http.StatusCreated { 58 return errorutils.CheckError(errors.New("Bintray response: " + resp.Status + "\n" + clientutils.IndentJson(body))) 59 } 60 61 log.Debug("Bintray response:", resp.Status) 62 log.Output(clientutils.IndentJson(body)) 63 return nil 64 } 65 66 func (ps *PackageService) Update(params *Params) error { 67 if ps.BintrayDetails.GetUser() == "" { 68 ps.BintrayDetails.SetUser(params.Subject) 69 } 70 content, err := createPackageContent(params) 71 if err != nil { 72 return err 73 } 74 75 url := ps.BintrayDetails.GetApiUrl() + path.Join("packages", params.Subject, params.Repo, params.Package) 76 77 log.Info("Updating package...") 78 httpClientsDetails := ps.BintrayDetails.CreateHttpClientDetails() 79 client, err := httpclient.ClientBuilder().Build() 80 if err != nil { 81 return err 82 } 83 resp, body, err := client.SendPatch(url, []byte(content), httpClientsDetails) 84 if err != nil { 85 return err 86 } 87 88 if resp.StatusCode != http.StatusOK { 89 return errorutils.CheckError(errors.New("Bintray response: " + resp.Status + "\n" + clientutils.IndentJson(body))) 90 } 91 92 log.Debug("Bintray response:", resp.Status) 93 log.Info("Updated package", params.Package+".") 94 return nil 95 } 96 97 func (ps *PackageService) Delete(packagePath *Path) error { 98 if ps.BintrayDetails.GetUser() == "" { 99 ps.BintrayDetails.SetUser(packagePath.Subject) 100 } 101 url := ps.BintrayDetails.GetApiUrl() + path.Join("packages", packagePath.Subject, packagePath.Repo, packagePath.Package) 102 103 log.Info("Deleting package...") 104 httpClientsDetails := ps.BintrayDetails.CreateHttpClientDetails() 105 client, err := httpclient.ClientBuilder().Build() 106 if err != nil { 107 return err 108 } 109 resp, body, err := client.SendDelete(url, nil, httpClientsDetails) 110 if err != nil { 111 return err 112 } 113 if resp.StatusCode != http.StatusOK { 114 return errorutils.CheckError(errors.New("Bintray response: " + resp.Status + "\n" + clientutils.IndentJson(body))) 115 } 116 117 log.Debug("Bintray response:", resp.Status) 118 log.Info("Deleted package", packagePath.Package+".") 119 return nil 120 } 121 122 func (ps *PackageService) Show(packagePath *Path) error { 123 if ps.BintrayDetails.GetUser() == "" { 124 ps.BintrayDetails.SetUser(packagePath.Subject) 125 } 126 url := ps.BintrayDetails.GetApiUrl() + path.Join("packages", packagePath.Subject, packagePath.Repo, packagePath.Package) 127 128 log.Info("Getting package details...") 129 httpClientsDetails := ps.BintrayDetails.CreateHttpClientDetails() 130 client, err := httpclient.ClientBuilder().Build() 131 if err != nil { 132 return err 133 } 134 resp, body, _, _ := client.SendGet(url, true, httpClientsDetails) 135 if resp.StatusCode != http.StatusOK { 136 return errorutils.CheckError(errors.New("Bintray response: " + resp.Status + "\n" + clientutils.IndentJson(body))) 137 } 138 139 log.Debug("Bintray response:", resp.Status) 140 log.Output(clientutils.IndentJson(body)) 141 return nil 142 } 143 144 func (ps *PackageService) IsPackageExists(packagePath *Path) (bool, error) { 145 url := ps.BintrayDetails.GetApiUrl() + path.Join("packages", packagePath.Subject, packagePath.Repo, packagePath.Package) 146 httpClientsDetails := ps.BintrayDetails.CreateHttpClientDetails() 147 148 client, err := httpclient.ClientBuilder().Build() 149 if err != nil { 150 return false, err 151 } 152 resp, _, err := client.SendHead(url, httpClientsDetails) 153 if err != nil { 154 return false, err 155 } 156 if resp.StatusCode == http.StatusOK { 157 return true, nil 158 } 159 if resp.StatusCode == http.StatusNotFound { 160 return false, nil 161 } 162 163 return false, errorutils.CheckError(errors.New("Bintray response: " + resp.Status)) 164 } 165 166 func (ps *PackageService) doCreatePackage(params *Params) (*http.Response, []byte, error) { 167 if ps.BintrayDetails.GetUser() == "" { 168 ps.BintrayDetails.SetUser(params.Subject) 169 } 170 content, err := createPackageContent(params) 171 if err != nil { 172 return nil, []byte{}, err 173 } 174 175 url := ps.BintrayDetails.GetApiUrl() + path.Join("packages", params.Subject, params.Repo) 176 httpClientsDetails := ps.BintrayDetails.CreateHttpClientDetails() 177 client, err := httpclient.ClientBuilder().Build() 178 if err != nil { 179 return nil, []byte{}, err 180 } 181 return client.SendPost(url, content, httpClientsDetails) 182 } 183 184 func createPackageContent(params *Params) ([]byte, error) { 185 labels := []string{} 186 if params.Labels != "" { 187 labels = strings.Split(params.Labels, ",") 188 } 189 licenses := []string{} 190 if params.Licenses != "" { 191 licenses = strings.Split(params.Licenses, ",") 192 } 193 customLicenses := []string{} 194 if params.CustomLicenses != "" { 195 customLicenses = strings.Split(params.CustomLicenses, ",") 196 } 197 198 Config := contentConfig{ 199 Name: params.Package, 200 Desc: params.Desc, 201 Labels: labels, 202 Licenses: licenses, 203 CustomLicenses: customLicenses, 204 VcsUrl: params.VcsUrl, 205 WebsiteUrl: params.WebsiteUrl, 206 IssueTrackerUrl: params.IssueTrackerUrl, 207 GithubRepo: params.GithubRepo, 208 GithubReleaseNotesFile: params.GithubReleaseNotesFile, 209 PublicDownloadNumbers: params.PublicDownloadNumbers, 210 PublicStats: params.PublicStats, 211 } 212 requestContent, err := json.Marshal(Config) 213 if err != nil { 214 return nil, errorutils.CheckError(errors.New("Failed to execute request.")) 215 } 216 return requestContent, nil 217 } 218 219 type contentConfig struct { 220 Name string `json:"name,omitempty"` 221 Desc string `json:"desc,omitempty"` 222 Labels []string `json:"labels,omitempty"` 223 Licenses []string `json:"licenses,omitempty"` 224 CustomLicenses []string `json:"custom_licenses,omitempty"` 225 VcsUrl string `json:"vcs_url,omitempty"` 226 WebsiteUrl string `json:"website_url,omitempty"` 227 IssueTrackerUrl string `json:"issue_tracker_url,omitempty"` 228 GithubRepo string `json:"github_repo,omitempty"` 229 GithubReleaseNotesFile string `json:"github_release_notes_file,omitempty"` 230 PublicDownloadNumbers bool `json:"public_download_numbers,omitempty"` 231 PublicStats bool `json:"public_stats,omitempty"` 232 }