artmigrator/repository/nexus/nexus.go

118 lines
2.9 KiB
Go
Raw Permalink Normal View History

2024-11-19 17:42:08 +03:00
package nexus
import (
2024-11-21 14:20:10 +03:00
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
2024-11-19 17:42:08 +03:00
)
type Client struct {
2024-11-21 14:20:10 +03:00
BaseURL string
HTTPClient *http.Client
Username string
Password string
2024-11-19 17:42:08 +03:00
}
func NewClient(baseURL, username, password string) *Client {
2024-11-21 14:20:10 +03:00
return &Client{
BaseURL: baseURL,
HTTPClient: &http.Client{},
Username: username,
Password: password,
}
2024-11-19 17:42:08 +03:00
}
func (c *Client) GetArtifacts(repository string) ([]string, error) {
2024-11-21 14:20:10 +03:00
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
2024-11-19 17:42:08 +03:00
}
func (c *Client) GetArtifactData(repository, artifact string) ([]byte, error) {
2024-11-21 14:20:10 +03:00
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
2024-11-21 12:40:38 +03:00
}
func (c *Client) ArtifactExists(artifact string) (bool, error) {
2024-11-21 14:20:10 +03:00
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
2024-11-21 12:40:38 +03:00
}