mirror of
https://tvoygit.ru/Djam/artmigrator.git
synced 2025-02-23 10:22:45 +00:00
117 lines
2.9 KiB
Go
117 lines
2.9 KiB
Go
package nexus
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"log"
|
|
"net/http"
|
|
)
|
|
|
|
type Client struct {
|
|
BaseURL string
|
|
HTTPClient *http.Client
|
|
Username string
|
|
Password string
|
|
}
|
|
|
|
func NewClient(baseURL, username, password string) *Client {
|
|
return &Client{
|
|
BaseURL: baseURL,
|
|
HTTPClient: &http.Client{},
|
|
Username: username,
|
|
Password: password,
|
|
}
|
|
}
|
|
|
|
func (c *Client) GetArtifacts(repository string) ([]string, error) {
|
|
url := fmt.Sprintf("%s/service/rest/v1/search/assets?sort=repository&repository=%s", c.BaseURL, repository)
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req.SetBasicAuth(c.Username, c.Password)
|
|
|
|
log.Printf("Запрос на получение артефактов: %s", req.URL)
|
|
log.Printf("Заголовки запроса: %v", req.Header)
|
|
|
|
resp, err := c.HTTPClient.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
log.Printf("Код ответа: %d", resp.StatusCode)
|
|
log.Printf("Заголовки ответа: %v", resp.Header)
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
log.Printf("Тело ответа: %s", string(body))
|
|
|
|
var assets struct {
|
|
Items []struct {
|
|
DownloadUrl string `json:"downloadUrl"`
|
|
} `json:"items"`
|
|
}
|
|
err = json.Unmarshal(body, &assets)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var result []string
|
|
for _, asset := range assets.Items {
|
|
result = append(result, asset.DownloadUrl)
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func (c *Client) GetArtifactData(repository, artifact string) ([]byte, error) {
|
|
log.Printf("Загрузка артефакта %s из репозитория Nexus", artifact)
|
|
url := fmt.Sprintf("%s/repository/%s/%s", c.BaseURL, repository, artifact)
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req.SetBasicAuth(c.Username, c.Password)
|
|
|
|
resp, err := c.HTTPClient.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
log.Printf("Артефакт %s загружен из репозитория Nexus", artifact)
|
|
return body, nil
|
|
}
|
|
|
|
func (c *Client) ArtifactExists(artifact string) (bool, error) {
|
|
log.Printf("Проверка наличия артефакта %s в репозитории Nexus", artifact)
|
|
url := fmt.Sprintf("%s/repository/%s/%s", c.BaseURL, "your-repository-name", artifact)
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
req.SetBasicAuth(c.Username, c.Password)
|
|
|
|
resp, err := c.HTTPClient.Do(req)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode == http.StatusNotFound {
|
|
log.Printf("Артефакт %s не существует в репозитории Nexus", artifact)
|
|
return false, nil
|
|
}
|
|
|
|
log.Printf("Артефакт %s существует в репозитории Nexus", artifact)
|
|
return true, nil
|
|
}
|