mirror of
https://tvoygit.ru/Djam/artmigrator.git
synced 2025-02-24 02:42:45 +00:00
update config
This commit is contained in:
parent
51635e0800
commit
af06d368de
4 changed files with 55 additions and 21 deletions
|
@ -1,10 +1,10 @@
|
||||||
nexus:
|
nexus:
|
||||||
url: "https://nexus.example.com"
|
url: "http://127.0.0.1:8081"
|
||||||
username: "your-nexus-username"
|
username: "your-nexus-username"
|
||||||
password: "your-nexus-password"
|
password: "your-nexus-password"
|
||||||
|
|
||||||
gitea:
|
gitea:
|
||||||
url: "https://gitea.example.com"
|
url: "http://127.0.0.1:3000"
|
||||||
token: "your-gitea-token"
|
token: "your-gitea-token"
|
||||||
|
username: "your-gitea-username"
|
||||||
|
repo: "your-gitea-repo"
|
||||||
|
|
|
@ -15,6 +15,8 @@ type Config struct {
|
||||||
Gitea struct {
|
Gitea struct {
|
||||||
URL string `yaml:"url"`
|
URL string `yaml:"url"`
|
||||||
Token string `yaml:"token"`
|
Token string `yaml:"token"`
|
||||||
|
Username string `yaml:"username"`
|
||||||
|
Repo string `yaml:"repo"`
|
||||||
} `yaml:"gitea"`
|
} `yaml:"gitea"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
10
core/core.go
10
core/core.go
|
@ -4,9 +4,9 @@ import (
|
||||||
"flag"
|
"flag"
|
||||||
|
|
||||||
"tvoygit.ru/djam/artmigrator/config"
|
"tvoygit.ru/djam/artmigrator/config"
|
||||||
|
"tvoygit.ru/djam/artmigrator/gitea"
|
||||||
"tvoygit.ru/djam/artmigrator/logger"
|
"tvoygit.ru/djam/artmigrator/logger"
|
||||||
"tvoygit.ru/djam/artmigrator/repository/gitea"
|
"tvoygit.ru/djam/artmigrator/nexus"
|
||||||
"tvoygit.ru/djam/artmigrator/repository/nexus"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type App struct {
|
type App struct {
|
||||||
|
@ -31,7 +31,7 @@ func NewApp(configFile string) (*App, error) {
|
||||||
|
|
||||||
func (a *App) Run() error {
|
func (a *App) Run() error {
|
||||||
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)
|
||||||
giteaClient := gitea.NewClient(a.Config.Gitea.URL, a.Config.Gitea.Token)
|
giteaClient := gitea.NewClient(a.Config.Gitea.URL, a.Config.Gitea.Token, a.Config)
|
||||||
|
|
||||||
repository := "your-repository-name"
|
repository := "your-repository-name"
|
||||||
artifacts, err := nexusClient.GetArtifacts(repository)
|
artifacts, err := nexusClient.GetArtifacts(repository)
|
||||||
|
@ -40,7 +40,7 @@ func (a *App) Run() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, artifact := range artifacts {
|
for _, artifact := range artifacts {
|
||||||
exists, err := giteaClient.ArtifactExists("owner", "repo", artifact)
|
exists, err := giteaClient.ArtifactExists(artifact)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -55,7 +55,7 @@ func (a *App) Run() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = giteaClient.UploadArtifact("owner", "repo", artifact, data)
|
err = giteaClient.UploadArtifact(artifact, data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,13 @@
|
||||||
package gitea
|
package gitea
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"tvoygit.ru/djam/artmigrator/config"
|
||||||
"tvoygit.ru/djam/artmigrator/logger"
|
"tvoygit.ru/djam/artmigrator/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -12,18 +15,20 @@ type Client struct {
|
||||||
BaseURL string
|
BaseURL string
|
||||||
HTTPClient *http.Client
|
HTTPClient *http.Client
|
||||||
Token string
|
Token string
|
||||||
|
Config *config.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(baseURL, token string) *Client {
|
func NewClient(baseURL, token string, config *config.Config) *Client {
|
||||||
return &Client{
|
return &Client{
|
||||||
BaseURL: baseURL,
|
BaseURL: baseURL,
|
||||||
HTTPClient: &http.Client{},
|
HTTPClient: &http.Client{},
|
||||||
Token: token,
|
Token: token,
|
||||||
|
Config: config,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) ArtifactExists(owner, repo, artifact string) (bool, error) {
|
func (c *Client) ArtifactExists(artifact string) (bool, error) {
|
||||||
url := fmt.Sprintf("%s/api/v1/repos/%s/%s/contents/%s", c.BaseURL, owner, 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 {
|
||||||
return false, err
|
return false, err
|
||||||
|
@ -42,23 +47,50 @@ func (c *Client) ArtifactExists(owner, repo, artifact string) (bool, error) {
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) UploadArtifact(owner, repo, artifactPath string, data []byte) error {
|
type UploadArtifactRequest struct {
|
||||||
url := fmt.Sprintf("%s/api/v1/repos/%s/%s/contents/%s", c.BaseURL, owner, repo, artifactPath)
|
Content string `json:"content"`
|
||||||
req, err := http.NewRequest("POST", url, nil)
|
Message string `json:"message"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) UploadArtifact(artifactPath string, data []byte) error {
|
||||||
|
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{
|
||||||
|
Content: string(data),
|
||||||
|
Message: "Upload artifact",
|
||||||
|
}
|
||||||
|
jsonRequest, err := json.Marshal(request)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonRequest))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
req.Header.Add("Authorization", fmt.Sprintf("token %s", c.Token))
|
req.Header.Add("Authorization", fmt.Sprintf("token %s", c.Token))
|
||||||
|
req.Header.Add("Content-Type", "application/json")
|
||||||
|
|
||||||
|
logger.Logger.Printf("Upload artifact request: %s", req.URL)
|
||||||
|
logger.Logger.Printf("Upload artifact headers: %v", req.Header)
|
||||||
|
logger.Logger.Printf("Upload artifact body: %s", jsonRequest)
|
||||||
|
|
||||||
// Здесь нужно заполнить тело запроса данными о загружаемом артефакте
|
|
||||||
resp, err := c.HTTPClient.Do(req)
|
resp, err := c.HTTPClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
logger.Logger.Printf("Upload artifact error: %v", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
logger.Logger.Printf("Upload artifact response status code: %d", resp.StatusCode)
|
||||||
body, _ := ioutil.ReadAll(resp.Body)
|
logger.Logger.Printf("Upload artifact response headers: %v", resp.Header)
|
||||||
|
|
||||||
|
body, err := ioutil.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
logger.Logger.Printf("Upload artifact error reading response body: %v", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
logger.Logger.Printf("Upload artifact response body: %s", body)
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusCreated {
|
||||||
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")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue