artmigrator/repository/nexus/nexus.go

105 lines
2.9 KiB
Go
Raw Normal View History

2024-11-19 17:42:08 +03:00
package nexus
import (
"fmt"
"io/ioutil"
2024-11-21 12:40:38 +03:00
"log"
2024-11-19 17:42:08 +03:00
"net/http"
"strings"
)
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) {
2024-11-21 12:40:38 +03:00
log.Printf("Получение артефактов из репозитория Nexus")
2024-11-19 17:42:08 +03:00
url := fmt.Sprintf("%s/repository/%s/", c.BaseURL, repository)
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
}
2024-11-21 12:40:38 +03:00
log.Printf("Артефакты получены из репозитория Nexus")
2024-11-19 17:42:08 +03:00
artifacts := strings.Split(string(body), "\n")
2024-11-21 12:40:38 +03:00
var result []string
for _, artifact := range artifacts {
if strings.Contains(artifact, ".jar") || strings.Contains(artifact, ".pom") {
result = append(result, artifact)
}
}
return result, nil
2024-11-19 17:42:08 +03:00
}
func (c *Client) GetArtifactData(repository, artifact string) ([]byte, error) {
2024-11-21 12:40:38 +03:00
log.Printf("Загрузка артефакта %s из репозитория Nexus", artifact)
2024-11-19 17:42:08 +03:00
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
}
2024-11-21 12:40:38 +03:00
log.Printf("Артефакт %s загружен из репозитория Nexus", artifact)
2024-11-19 17:42:08 +03:00
return body, nil
2024-11-21 12:40:38 +03:00
}
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
}