mirror of
https://tvoygit.ru/Djam/artmigrator.git
synced 2025-02-23 18:32:46 +00:00
update
This commit is contained in:
parent
af06d368de
commit
cb816f8d66
9 changed files with 88 additions and 66 deletions
|
@ -2,6 +2,7 @@ package artifact_types
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MavenArtifact struct {
|
type MavenArtifact struct {
|
||||||
|
@ -16,8 +17,8 @@ func (a MavenArtifact) String() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseMavenArtifact(artifact string) (*MavenArtifact, error) {
|
func ParseMavenArtifact(artifact string) (*MavenArtifact, error) {
|
||||||
parts := splitArtifact(artifact, 4)
|
parts := strings.Split(artifact, ":")
|
||||||
if len(parts) < 4 {
|
if len(parts) != 4 {
|
||||||
return nil, fmt.Errorf("invalid Maven artifact format: %s", artifact)
|
return nil, fmt.Errorf("invalid Maven artifact format: %s", artifact)
|
||||||
}
|
}
|
||||||
return &MavenArtifact{
|
return &MavenArtifact{
|
||||||
|
@ -27,27 +28,3 @@ func ParseMavenArtifact(artifact string) (*MavenArtifact, error) {
|
||||||
Packaging: parts[3],
|
Packaging: parts[3],
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func splitArtifact(artifact string, n int) []string {
|
|
||||||
parts := make([]string, n)
|
|
||||||
for i := 0; i < n-1; i++ {
|
|
||||||
index := findNextColon(artifact)
|
|
||||||
if index == -1 {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
parts[i] = artifact[:index]
|
|
||||||
artifact = artifact[index+1:]
|
|
||||||
}
|
|
||||||
parts[n-1] = artifact
|
|
||||||
return parts
|
|
||||||
}
|
|
||||||
|
|
||||||
func findNextColon(s string) int {
|
|
||||||
for i, r := range s {
|
|
||||||
if r == ':' {
|
|
||||||
return i
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return -1
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
|
@ -30,4 +30,3 @@ func ParseRpmArtifact(artifact string) (*RpmArtifact, error) {
|
||||||
Arch: matches[4],
|
Arch: matches[4],
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
40
core/core.go
40
core/core.go
|
@ -2,11 +2,11 @@ package core
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
|
"log"
|
||||||
|
|
||||||
"tvoygit.ru/djam/artmigrator/config"
|
"tvoygit.ru/djam/artmigrator/config"
|
||||||
"tvoygit.ru/djam/artmigrator/gitea"
|
"tvoygit.ru/djam/artmigrator/repository/gitea"
|
||||||
"tvoygit.ru/djam/artmigrator/logger"
|
"tvoygit.ru/djam/artmigrator/repository/nexus"
|
||||||
"tvoygit.ru/djam/artmigrator/nexus"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type App struct {
|
type App struct {
|
||||||
|
@ -14,6 +14,7 @@ type App struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewApp(configFile string) (*App, error) {
|
func NewApp(configFile string) (*App, error) {
|
||||||
|
log.Println("Загрузка конфигурации из файла", configFile)
|
||||||
cfg, err := config.LoadConfig(configFile)
|
cfg, err := config.LoadConfig(configFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -30,36 +31,43 @@ func NewApp(configFile string) (*App, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *App) Run() error {
|
func (a *App) Run() error {
|
||||||
|
log.Println("Начало процесса загрузки артефакта")
|
||||||
|
|
||||||
nexusClient := nexus.NewClient(a.Config.Nexus.URL, a.Config.Nexus.Username, a.Config.Nexus.Password)
|
nexusClient := nexus.NewClient(a.Config.Nexus.URL, a.Config.Nexus.Username, a.Config.Nexus.Password)
|
||||||
|
log.Println("Авторизация в Nexus прошла успешно")
|
||||||
|
|
||||||
giteaClient := gitea.NewClient(a.Config.Gitea.URL, a.Config.Gitea.Token, a.Config)
|
giteaClient := gitea.NewClient(a.Config.Gitea.URL, a.Config.Gitea.Token, a.Config)
|
||||||
|
log.Println("Авторизация в Gitea прошла успешно")
|
||||||
|
|
||||||
repository := "your-repository-name"
|
repository := "your-repository-name"
|
||||||
|
log.Println("Начало процесса загрузки артефактов...")
|
||||||
artifacts, err := nexusClient.GetArtifacts(repository)
|
artifacts, err := nexusClient.GetArtifacts(repository)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
log.Printf("Найдены артефакты для загрузки в Gitea: %v", artifacts)
|
||||||
|
|
||||||
|
if len(artifacts) == 0 {
|
||||||
|
log.Println("Нет артефактов для загрузки в Gitea")
|
||||||
|
} else {
|
||||||
for _, artifact := range artifacts {
|
for _, artifact := range artifacts {
|
||||||
exists, err := giteaClient.ArtifactExists(artifact)
|
log.Printf("Загрузка артефакта %s из репозитория Nexus...", artifact)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if exists {
|
|
||||||
logger.Logger.Printf("Artifact %s already exists in Gitea", artifact)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Загрузка артефакта
|
|
||||||
data, err := nexusClient.GetArtifactData(repository, artifact)
|
data, err := nexusClient.GetArtifactData(repository, artifact)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
log.Printf("Ошибка загрузки артефакта %s: %v", artifact, err)
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
log.Printf("Артефакт %s загружен из репозитория Nexus", artifact)
|
||||||
|
|
||||||
|
log.Printf("Загрузка артефакта %s в репозиторий Gitea...", artifact)
|
||||||
err = giteaClient.UploadArtifact(artifact, data)
|
err = giteaClient.UploadArtifact(artifact, data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
log.Printf("Ошибка загрузки артефакта %s в Gitea: %v", artifact, err)
|
||||||
|
} else {
|
||||||
|
log.Printf("Артефакт %s успешно загружен в Gitea", artifact)
|
||||||
}
|
}
|
||||||
logger.Logger.Printf("Artifact %s uploaded to Gitea", artifact)
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
log.Println("Процесс загрузки артефактов завершен.")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
2
go.mod
2
go.mod
|
@ -1,6 +1,6 @@
|
||||||
module tvoygit.ru/djam/artmigrator
|
module tvoygit.ru/djam/artmigrator
|
||||||
|
|
||||||
go 1.19
|
go 1.23.3
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/stretchr/testify v1.9.0
|
github.com/stretchr/testify v1.9.0
|
||||||
|
|
8
go.sum
8
go.sum
|
@ -1,19 +1,11 @@
|
||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
|
||||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
|
||||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
|
||||||
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
|
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
|
||||||
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
|
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
|
||||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
|
||||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
|
||||||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
|
||||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
|
8
main.go
8
main.go
|
@ -7,13 +7,17 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
log.Println("Начало программы")
|
||||||
|
|
||||||
app, err := core.NewApp("config.yaml")
|
app, err := core.NewApp("config.yaml")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to create app: %v", err)
|
log.Fatalf("Ошибка загрузки конфигурации: %v", err)
|
||||||
}
|
}
|
||||||
|
log.Println("Конфигурация загружена успешно")
|
||||||
|
|
||||||
err = app.Run()
|
err = app.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to run app: %v", err)
|
log.Fatalf("Ошибка выполнения программы: %v", err)
|
||||||
}
|
}
|
||||||
|
log.Println("Программа выполнена успешно")
|
||||||
}
|
}
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"tvoygit.ru/djam/artmigrator/config"
|
"tvoygit.ru/djam/artmigrator/config"
|
||||||
|
@ -28,6 +29,7 @@ func NewClient(baseURL, token string, config *config.Config) *Client {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) ArtifactExists(artifact string) (bool, error) {
|
func (c *Client) ArtifactExists(artifact string) (bool, error) {
|
||||||
|
log.Printf("Проверка наличия артефакта %s в репозитории Gitea", artifact)
|
||||||
url := fmt.Sprintf("%s/api/v1/repos/%s/%s/contents/%s", c.BaseURL, c.Config.Gitea.Username, c.Config.Gitea.Repo, artifact)
|
url := fmt.Sprintf("%s/api/v1/repos/%s/%s/contents/%s", c.BaseURL, c.Config.Gitea.Username, c.Config.Gitea.Repo, artifact)
|
||||||
req, err := http.NewRequest("GET", url, nil)
|
req, err := http.NewRequest("GET", url, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -42,8 +44,11 @@ func (c *Client) ArtifactExists(artifact string) (bool, error) {
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
if resp.StatusCode == http.StatusNotFound {
|
if resp.StatusCode == http.StatusNotFound {
|
||||||
|
log.Printf("Артефакт %s не существует в репозитории Gitea", artifact)
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Printf("Артефакт %s существует в репозитории Gitea", artifact)
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,6 +58,7 @@ type UploadArtifactRequest struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) UploadArtifact(artifactPath string, data []byte) error {
|
func (c *Client) UploadArtifact(artifactPath string, data []byte) error {
|
||||||
|
log.Printf("Загрузка артефакта %s в репозиторий Gitea", artifactPath)
|
||||||
url := fmt.Sprintf("%s/api/v1/repos/%s/%s/contents/%s?ref=master", c.BaseURL, c.Config.Gitea.Username, c.Config.Gitea.Repo, artifactPath)
|
url := fmt.Sprintf("%s/api/v1/repos/%s/%s/contents/%s?ref=master", c.BaseURL, c.Config.Gitea.Username, c.Config.Gitea.Repo, artifactPath)
|
||||||
request := UploadArtifactRequest{
|
request := UploadArtifactRequest{
|
||||||
Content: string(data),
|
Content: string(data),
|
||||||
|
@ -94,5 +100,7 @@ func (c *Client) UploadArtifact(artifactPath string, data []byte) error {
|
||||||
logger.Logger.Printf("Failed to upload artifact: %s", body)
|
logger.Logger.Printf("Failed to upload artifact: %s", body)
|
||||||
return fmt.Errorf("failed to upload artifact")
|
return fmt.Errorf("failed to upload artifact")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Printf("Артефакт %s загружен в репозиторий Gitea", artifactPath)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,9 +3,9 @@ package nexus
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
|
@ -25,6 +25,7 @@ func NewClient(baseURL, username, password string) *Client {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) GetArtifacts(repository string) ([]string, error) {
|
func (c *Client) GetArtifacts(repository string) ([]string, error) {
|
||||||
|
log.Printf("Получение артефактов из репозитория Nexus")
|
||||||
url := fmt.Sprintf("%s/repository/%s/", c.BaseURL, repository)
|
url := fmt.Sprintf("%s/repository/%s/", c.BaseURL, repository)
|
||||||
req, err := http.NewRequest("GET", url, nil)
|
req, err := http.NewRequest("GET", url, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -43,12 +44,19 @@ func (c *Client) GetArtifacts(repository string) ([]string, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Здесь нужно парсить ответ и вытащить список артефактов
|
log.Printf("Артефакты получены из репозитория Nexus")
|
||||||
artifacts := strings.Split(string(body), "\n")
|
artifacts := strings.Split(string(body), "\n")
|
||||||
return artifacts, nil
|
var result []string
|
||||||
|
for _, artifact := range artifacts {
|
||||||
|
if strings.Contains(artifact, ".jar") || strings.Contains(artifact, ".pom") {
|
||||||
|
result = append(result, artifact)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) GetArtifactData(repository, artifact string) ([]byte, error) {
|
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)
|
url := fmt.Sprintf("%s/repository/%s/%s", c.BaseURL, repository, artifact)
|
||||||
req, err := http.NewRequest("GET", url, nil)
|
req, err := http.NewRequest("GET", url, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -66,5 +74,31 @@ func (c *Client) GetArtifactData(repository, artifact string) ([]byte, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Printf("Артефакт %s загружен из репозитория Nexus", artifact)
|
||||||
return body, nil
|
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
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue