update config

This commit is contained in:
Sergey Zhemoytel 2024-11-20 17:38:54 +03:00
parent 51635e0800
commit af06d368de
4 changed files with 55 additions and 21 deletions

View file

@ -1,10 +1,10 @@
nexus:
url: "https://nexus.example.com"
url: "http://127.0.0.1:8081"
username: "your-nexus-username"
password: "your-nexus-password"
gitea:
url: "https://gitea.example.com"
url: "http://127.0.0.1:3000"
token: "your-gitea-token"
username: "your-gitea-username"
repo: "your-gitea-repo"

View file

@ -15,6 +15,8 @@ type Config struct {
Gitea struct {
URL string `yaml:"url"`
Token string `yaml:"token"`
Username string `yaml:"username"`
Repo string `yaml:"repo"`
} `yaml:"gitea"`
}

View file

@ -4,9 +4,9 @@ import (
"flag"
"tvoygit.ru/djam/artmigrator/config"
"tvoygit.ru/djam/artmigrator/gitea"
"tvoygit.ru/djam/artmigrator/logger"
"tvoygit.ru/djam/artmigrator/repository/gitea"
"tvoygit.ru/djam/artmigrator/repository/nexus"
"tvoygit.ru/djam/artmigrator/nexus"
)
type App struct {
@ -31,7 +31,7 @@ func NewApp(configFile string) (*App, error) {
func (a *App) Run() error {
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"
artifacts, err := nexusClient.GetArtifacts(repository)
@ -40,7 +40,7 @@ func (a *App) Run() error {
}
for _, artifact := range artifacts {
exists, err := giteaClient.ArtifactExists("owner", "repo", artifact)
exists, err := giteaClient.ArtifactExists(artifact)
if err != nil {
return err
}
@ -55,7 +55,7 @@ func (a *App) Run() error {
return err
}
err = giteaClient.UploadArtifact("owner", "repo", artifact, data)
err = giteaClient.UploadArtifact(artifact, data)
if err != nil {
return err
}

View file

@ -1,10 +1,13 @@
package gitea
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"tvoygit.ru/djam/artmigrator/config"
"tvoygit.ru/djam/artmigrator/logger"
)
@ -12,18 +15,20 @@ type Client struct {
BaseURL string
HTTPClient *http.Client
Token string
Config *config.Config
}
func NewClient(baseURL, token string) *Client {
func NewClient(baseURL, token string, config *config.Config) *Client {
return &Client{
BaseURL: baseURL,
HTTPClient: &http.Client{},
Token: token,
Config: config,
}
}
func (c *Client) ArtifactExists(owner, repo, artifact string) (bool, error) {
url := fmt.Sprintf("%s/api/v1/repos/%s/%s/contents/%s", c.BaseURL, owner, repo, artifact)
func (c *Client) ArtifactExists(artifact string) (bool, error) {
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)
if err != nil {
return false, err
@ -42,23 +47,50 @@ func (c *Client) ArtifactExists(owner, repo, artifact string) (bool, error) {
return true, nil
}
func (c *Client) UploadArtifact(owner, repo, artifactPath string, data []byte) error {
url := fmt.Sprintf("%s/api/v1/repos/%s/%s/contents/%s", c.BaseURL, owner, repo, artifactPath)
req, err := http.NewRequest("POST", url, nil)
type UploadArtifactRequest struct {
Content string `json:"content"`
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 {
return err
}
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)
if err != nil {
logger.Logger.Printf("Upload artifact error: %v", err)
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := ioutil.ReadAll(resp.Body)
logger.Logger.Printf("Upload artifact response status code: %d", resp.StatusCode)
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)
return fmt.Errorf("failed to upload artifact")
}