Исправление путей для repo
Добавление возможности работать с несколькими репозиториями
This commit is contained in:
@@ -1,15 +1,28 @@
|
|||||||
[git]
|
# Базовый каталог для хранения всех репозиториев
|
||||||
|
reposBaseDir = "/var/cache/alr-updater"
|
||||||
|
|
||||||
|
# Конфигурация репозиториев (можно настроить несколько)
|
||||||
|
[repositories.alr-repo]
|
||||||
repoURL = "https://gitea.plemya-x.ru/Plemya-x/alr-repo.git"
|
repoURL = "https://gitea.plemya-x.ru/Plemya-x/alr-repo.git"
|
||||||
repoDir = "/etc/alr-updater/repo"
|
[repositories.alr-repo.commit]
|
||||||
[git.commit]
|
|
||||||
# Имя и адрес электронной почты, которые будут использоваться при фиксации git
|
# Имя и адрес электронной почты, которые будут использоваться при фиксации git
|
||||||
name = "CHANGE ME"
|
name = "CHANGE ME"
|
||||||
email = "CHANGE ME"
|
email = "CHANGE ME"
|
||||||
[git.credentials]
|
[repositories.alr-repo.credentials]
|
||||||
# Имя пользователя и пароль для git push. Используйте личный токен доступа в качестве пароля для Gitea.
|
# Имя пользователя и пароль для git push. Используйте личный токен доступа в качестве пароля для Gitea.
|
||||||
username = "CHANGE ME"
|
username = "CHANGE ME"
|
||||||
password = "CHANGE ME"
|
password = "CHANGE ME"
|
||||||
|
|
||||||
|
# Можно добавить дополнительные репозитории
|
||||||
|
# [repositories.another-repo]
|
||||||
|
# repoURL = "https://github.com/example/another-repo.git"
|
||||||
|
# [repositories.another-repo.commit]
|
||||||
|
# name = "Bot User"
|
||||||
|
# email = "bot@example.com"
|
||||||
|
# [repositories.another-repo.credentials]
|
||||||
|
# username = "bot-user"
|
||||||
|
# password = "github-token"
|
||||||
|
|
||||||
[webhook]
|
[webhook]
|
||||||
# Хэш пароля для webhook. Сгенерируйте его, используя `alr-updater -g`.
|
# Хэш пароля для webhook. Сгенерируйте его, используя `alr-updater -g`.
|
||||||
pwd_hash = "CHANGE ME"
|
pwd_hash = "CHANGE ME"
|
@@ -41,7 +41,7 @@ func updaterModule(cfg *config.Config) *starlarkstruct.Module {
|
|||||||
return &starlarkstruct.Module{
|
return &starlarkstruct.Module{
|
||||||
Name: "updater",
|
Name: "updater",
|
||||||
Members: starlark.StringDict{
|
Members: starlark.StringDict{
|
||||||
"repo_dir": starlark.String(cfg.Git.RepoDir),
|
"repos_base_dir": starlark.String(cfg.ReposBaseDir),
|
||||||
"pull": updaterPull(cfg),
|
"pull": updaterPull(cfg),
|
||||||
"push_changes": updaterPushChanges(cfg),
|
"push_changes": updaterPushChanges(cfg),
|
||||||
"get_package_file": getPackageFile(cfg),
|
"get_package_file": getPackageFile(cfg),
|
||||||
@@ -57,10 +57,22 @@ var repoMtx = &sync.Mutex{}
|
|||||||
|
|
||||||
func updaterPull(cfg *config.Config) *starlark.Builtin {
|
func updaterPull(cfg *config.Config) *starlark.Builtin {
|
||||||
return starlark.NewBuiltin("updater.pull", func(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
|
return starlark.NewBuiltin("updater.pull", func(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
|
||||||
|
var repoName string
|
||||||
|
err := starlark.UnpackArgs("updater.pull", args, kwargs, "repo", &repoName)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
repoConfig, exists := cfg.Repositories[repoName]
|
||||||
|
if !exists {
|
||||||
|
return nil, fmt.Errorf("repository '%s' not found in configuration", repoName)
|
||||||
|
}
|
||||||
|
|
||||||
repoMtx.Lock()
|
repoMtx.Lock()
|
||||||
defer repoMtx.Unlock()
|
defer repoMtx.Unlock()
|
||||||
|
|
||||||
repo, err := git.PlainOpen(cfg.Git.RepoDir)
|
repoDir := filepath.Join(cfg.ReposBaseDir, repoName)
|
||||||
|
repo, err := git.PlainOpen(repoDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -75,22 +87,29 @@ func updaterPull(cfg *config.Config) *starlark.Builtin {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_ = repoConfig // Избегаем неиспользованной переменной
|
||||||
return starlark.None, nil
|
return starlark.None, nil
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func updaterPushChanges(cfg *config.Config) *starlark.Builtin {
|
func updaterPushChanges(cfg *config.Config) *starlark.Builtin {
|
||||||
return starlark.NewBuiltin("updater.push_changes", func(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
|
return starlark.NewBuiltin("updater.push_changes", func(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
|
||||||
var msg string
|
var repoName, msg string
|
||||||
err := starlark.UnpackArgs("updater.push_changes", args, kwargs, "msg", &msg)
|
err := starlark.UnpackArgs("updater.push_changes", args, kwargs, "repo", &repoName, "msg", &msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
repoConfig, exists := cfg.Repositories[repoName]
|
||||||
|
if !exists {
|
||||||
|
return nil, fmt.Errorf("repository '%s' not found in configuration", repoName)
|
||||||
|
}
|
||||||
|
|
||||||
repoMtx.Lock()
|
repoMtx.Lock()
|
||||||
defer repoMtx.Unlock()
|
defer repoMtx.Unlock()
|
||||||
|
|
||||||
repo, err := git.PlainOpen(cfg.Git.RepoDir)
|
repoDir := filepath.Join(cfg.ReposBaseDir, repoName)
|
||||||
|
repo, err := git.PlainOpen(repoDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -120,8 +139,8 @@ func updaterPushChanges(cfg *config.Config) *starlark.Builtin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sig := &object.Signature{
|
sig := &object.Signature{
|
||||||
Name: cfg.Git.Commit.Name,
|
Name: repoConfig.Commit.Name,
|
||||||
Email: cfg.Git.Commit.Email,
|
Email: repoConfig.Commit.Email,
|
||||||
When: time.Now(),
|
When: time.Now(),
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -138,15 +157,15 @@ func updaterPushChanges(cfg *config.Config) *starlark.Builtin {
|
|||||||
err = repo.Push(&git.PushOptions{
|
err = repo.Push(&git.PushOptions{
|
||||||
Progress: os.Stderr,
|
Progress: os.Stderr,
|
||||||
Auth: &http.BasicAuth{
|
Auth: &http.BasicAuth{
|
||||||
Username: cfg.Git.Credentials.Username,
|
Username: repoConfig.Credentials.Username,
|
||||||
Password: cfg.Git.Credentials.Password,
|
Password: repoConfig.Credentials.Password,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debug("Successfully pushed to repo").Stringer("pos", thread.CallFrame(1).Pos).Send()
|
log.Debug("Successfully pushed to repo").Str("repo", repoName).Stringer("pos", thread.CallFrame(1).Pos).Send()
|
||||||
|
|
||||||
return starlark.None, nil
|
return starlark.None, nil
|
||||||
})
|
})
|
||||||
@@ -154,38 +173,50 @@ func updaterPushChanges(cfg *config.Config) *starlark.Builtin {
|
|||||||
|
|
||||||
func getPackageFile(cfg *config.Config) *starlark.Builtin {
|
func getPackageFile(cfg *config.Config) *starlark.Builtin {
|
||||||
return starlark.NewBuiltin("updater.get_package_file", func(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
|
return starlark.NewBuiltin("updater.get_package_file", func(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
|
||||||
var pkg, filename string
|
var repoName, pkg, filename string
|
||||||
err := starlark.UnpackArgs("updater.get_package_file", args, kwargs, "pkg", &pkg, "filename", &filename)
|
err := starlark.UnpackArgs("updater.get_package_file", args, kwargs, "repo", &repoName, "pkg", &pkg, "filename", &filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_, exists := cfg.Repositories[repoName]
|
||||||
|
if !exists {
|
||||||
|
return nil, fmt.Errorf("repository '%s' not found in configuration", repoName)
|
||||||
|
}
|
||||||
|
|
||||||
repoMtx.Lock()
|
repoMtx.Lock()
|
||||||
defer repoMtx.Unlock()
|
defer repoMtx.Unlock()
|
||||||
|
|
||||||
path := filepath.Join(cfg.Git.RepoDir, pkg, filename)
|
repoDir := filepath.Join(cfg.ReposBaseDir, repoName)
|
||||||
|
path := filepath.Join(repoDir, pkg, filename)
|
||||||
data, err := os.ReadFile(path)
|
data, err := os.ReadFile(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debug("Got package file").Str("package", pkg).Str("filename", filename).Stringer("pos", thread.CallFrame(1).Pos).Send()
|
log.Debug("Got package file").Str("repo", repoName).Str("package", pkg).Str("filename", filename).Stringer("pos", thread.CallFrame(1).Pos).Send()
|
||||||
return starlark.String(data), nil
|
return starlark.String(data), nil
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func writePackageFile(cfg *config.Config) *starlark.Builtin {
|
func writePackageFile(cfg *config.Config) *starlark.Builtin {
|
||||||
return starlark.NewBuiltin("updater.write_package_file", func(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
|
return starlark.NewBuiltin("updater.write_package_file", func(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
|
||||||
var pkg, filename, content string
|
var repoName, pkg, filename, content string
|
||||||
err := starlark.UnpackArgs("updater.write_package_file", args, kwargs, "pkg", &pkg, "filename", &filename, "content", &content)
|
err := starlark.UnpackArgs("updater.write_package_file", args, kwargs, "repo", &repoName, "pkg", &pkg, "filename", &filename, "content", &content)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_, exists := cfg.Repositories[repoName]
|
||||||
|
if !exists {
|
||||||
|
return nil, fmt.Errorf("repository '%s' not found in configuration", repoName)
|
||||||
|
}
|
||||||
|
|
||||||
repoMtx.Lock()
|
repoMtx.Lock()
|
||||||
defer repoMtx.Unlock()
|
defer repoMtx.Unlock()
|
||||||
|
|
||||||
path := filepath.Join(cfg.Git.RepoDir, pkg, filename)
|
repoDir := filepath.Join(cfg.ReposBaseDir, repoName)
|
||||||
|
path := filepath.Join(repoDir, pkg, filename)
|
||||||
|
|
||||||
// Читаем старый файл для сравнения версий
|
// Читаем старый файл для сравнения версий
|
||||||
var oldContent string
|
var oldContent string
|
||||||
@@ -200,13 +231,14 @@ func writePackageFile(cfg *config.Config) *starlark.Builtin {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
defer fl.Close()
|
||||||
|
|
||||||
_, err = io.Copy(fl, strings.NewReader(finalContent))
|
_, err = io.Copy(fl, strings.NewReader(finalContent))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debug("Wrote package file").Str("package", pkg).Str("filename", filename).Stringer("pos", thread.CallFrame(1).Pos).Send()
|
log.Debug("Wrote package file").Str("repo", repoName).Str("package", pkg).Str("filename", filename).Stringer("pos", thread.CallFrame(1).Pos).Send()
|
||||||
return starlark.None, nil
|
return starlark.None, nil
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -268,9 +300,10 @@ func updateChecksumsInContent(content string, checksums []string) string {
|
|||||||
|
|
||||||
func updateChecksums(cfg *config.Config) *starlark.Builtin {
|
func updateChecksums(cfg *config.Config) *starlark.Builtin {
|
||||||
return starlark.NewBuiltin("updater.update_checksums", func(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
|
return starlark.NewBuiltin("updater.update_checksums", func(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
|
||||||
var pkg, filename, content string
|
var repoName, pkg, filename, content string
|
||||||
var checksums *starlark.List
|
var checksums *starlark.List
|
||||||
err := starlark.UnpackArgs("updater.update_checksums", args, kwargs,
|
err := starlark.UnpackArgs("updater.update_checksums", args, kwargs,
|
||||||
|
"repo", &repoName,
|
||||||
"pkg", &pkg,
|
"pkg", &pkg,
|
||||||
"filename", &filename,
|
"filename", &filename,
|
||||||
"content", &content,
|
"content", &content,
|
||||||
@@ -279,6 +312,11 @@ func updateChecksums(cfg *config.Config) *starlark.Builtin {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_, exists := cfg.Repositories[repoName]
|
||||||
|
if !exists {
|
||||||
|
return nil, fmt.Errorf("repository '%s' not found in configuration", repoName)
|
||||||
|
}
|
||||||
|
|
||||||
// Конвертируем starlark.List в []string
|
// Конвертируем starlark.List в []string
|
||||||
checksumStrings := make([]string, 0, checksums.Len())
|
checksumStrings := make([]string, 0, checksums.Len())
|
||||||
iter := checksums.Iterate()
|
iter := checksums.Iterate()
|
||||||
@@ -297,7 +335,8 @@ func updateChecksums(cfg *config.Config) *starlark.Builtin {
|
|||||||
repoMtx.Lock()
|
repoMtx.Lock()
|
||||||
defer repoMtx.Unlock()
|
defer repoMtx.Unlock()
|
||||||
|
|
||||||
path := filepath.Join(cfg.Git.RepoDir, pkg, filename)
|
repoDir := filepath.Join(cfg.ReposBaseDir, repoName)
|
||||||
|
path := filepath.Join(repoDir, pkg, filename)
|
||||||
|
|
||||||
// Читаем старый файл для сравнения версий
|
// Читаем старый файл для сравнения версий
|
||||||
var oldContent string
|
var oldContent string
|
||||||
@@ -321,6 +360,7 @@ func updateChecksums(cfg *config.Config) *starlark.Builtin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
log.Debug("Updated package file with checksums").
|
log.Debug("Updated package file with checksums").
|
||||||
|
Str("repo", repoName).
|
||||||
Str("package", pkg).
|
Str("package", pkg).
|
||||||
Str("filename", filename).
|
Str("filename", filename).
|
||||||
Int("checksums_count", len(checksumStrings)).
|
Int("checksums_count", len(checksumStrings)).
|
||||||
|
@@ -19,15 +19,15 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Git Git `toml:"git" envPrefix:"GIT_"`
|
ReposBaseDir string `toml:"reposBaseDir" env:"REPOS_BASE_DIR"`
|
||||||
Webhook Webhook `toml:"webhook" envPrefix:"WEBHOOK_"`
|
Repositories map[string]GitRepo `toml:"repositories"`
|
||||||
|
Webhook Webhook `toml:"webhook" envPrefix:"WEBHOOK_"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Git struct {
|
type GitRepo struct {
|
||||||
RepoDir string `toml:"repoDir" env:"REPO_DIR"`
|
RepoURL string `toml:"repoURL"`
|
||||||
RepoURL string `toml:"repoURL" env:"REPO_URL"`
|
Commit Commit `toml:"commit"`
|
||||||
Commit Commit `toml:"commit" envPrefix:"COMMIT_"`
|
Credentials Credentials `toml:"credentials"`
|
||||||
Credentials Credentials `toml:"credentials" envPrefix:"CREDENTIALS_"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Credentials struct {
|
type Credentials struct {
|
||||||
|
107
main.go
107
main.go
@@ -19,10 +19,12 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
@@ -44,9 +46,39 @@ func init() {
|
|||||||
log.Logger = logger.NewPretty(os.Stderr)
|
log.Logger = logger.NewPretty(os.Stderr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// parseRepositoryFromPlugin парсит комментарий '# Repository: repo-name' из .star файла
|
||||||
|
func parseRepositoryFromPlugin(filePath string) (string, error) {
|
||||||
|
file, err := os.Open(filePath)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(file)
|
||||||
|
repoRegex := regexp.MustCompile(`^#\s*Repository:\s*(.+?)\s*$`)
|
||||||
|
|
||||||
|
// Читаем первые несколько строк файла
|
||||||
|
lineCount := 0
|
||||||
|
for scanner.Scan() && lineCount < 10 {
|
||||||
|
line := strings.TrimSpace(scanner.Text())
|
||||||
|
lineCount++
|
||||||
|
|
||||||
|
if matches := repoRegex.FindStringSubmatch(line); matches != nil {
|
||||||
|
return strings.TrimSpace(matches[1]), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Если встретили строку без комментария, прекращаем поиск
|
||||||
|
if line != "" && !strings.HasPrefix(line, "#") {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return "", scanner.Err()
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
configPath := pflag.StringP("config", "c", "/etc/alr-updater/config.toml", "Path to config file")
|
configPath := pflag.StringP("config", "c", "/etc/alr-updater/config.toml", "Path to config file")
|
||||||
dbPath := pflag.StringP("database", "d", "/etc/alr-updater/db", "Path to database file")
|
dbPath := pflag.StringP("database", "d", "/var/lib/alr-updater/db", "Path to database file")
|
||||||
pluginDir := pflag.StringP("plugin-dir", "p", "/etc/alr-updater/plugins", "Path to plugin directory")
|
pluginDir := pflag.StringP("plugin-dir", "p", "/etc/alr-updater/plugins", "Path to plugin directory")
|
||||||
serverAddr := pflag.StringP("address", "a", ":8080", "Webhook server address")
|
serverAddr := pflag.StringP("address", "a", ":8080", "Webhook server address")
|
||||||
genHash := pflag.BoolP("gen-hash", "g", false, "Generate a password hash for webhooks")
|
genHash := pflag.BoolP("gen-hash", "g", false, "Generate a password hash for webhooks")
|
||||||
@@ -99,21 +131,51 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := os.Stat(cfg.Git.RepoDir); os.IsNotExist(err) {
|
// Создаем базовый каталог для репозиториев
|
||||||
err = os.MkdirAll(cfg.Git.RepoDir, 0o755)
|
if cfg.ReposBaseDir == "" {
|
||||||
if err != nil {
|
cfg.ReposBaseDir = "/var/cache/alr-updater"
|
||||||
log.Fatal("Error creating repository directory").Err(err).Send()
|
}
|
||||||
}
|
|
||||||
|
|
||||||
_, err := git.PlainClone(cfg.Git.RepoDir, false, &git.CloneOptions{
|
if _, err := os.Stat(cfg.ReposBaseDir); os.IsNotExist(err) {
|
||||||
URL: cfg.Git.RepoURL,
|
err = os.MkdirAll(cfg.ReposBaseDir, 0o755)
|
||||||
Progress: os.Stderr,
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Error cloning repository").Err(err).Send()
|
log.Fatal("Error creating repositories base directory").Err(err).Send()
|
||||||
}
|
}
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
log.Fatal("Cannot stat configured repo directory").Err(err).Send()
|
log.Fatal("Cannot stat configured repos base directory").Err(err).Send()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Клонируем все сконфигурированные репозитории
|
||||||
|
for repoName, repoConfig := range cfg.Repositories {
|
||||||
|
repoDir := filepath.Join(cfg.ReposBaseDir, repoName)
|
||||||
|
|
||||||
|
if _, err := os.Stat(repoDir); os.IsNotExist(err) {
|
||||||
|
log.Info("Cloning repository").Str("name", repoName).Str("url", repoConfig.RepoURL).Send()
|
||||||
|
|
||||||
|
err = os.MkdirAll(repoDir, 0o755)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("Error creating repository directory").Str("repo", repoName).Err(err).Send()
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := git.PlainClone(repoDir, false, &git.CloneOptions{
|
||||||
|
URL: repoConfig.RepoURL,
|
||||||
|
Progress: os.Stderr,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("Error cloning repository").Str("repo", repoName).Err(err).Send()
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Info("Repository cloned successfully").Str("name", repoName).Send()
|
||||||
|
} else if err != nil {
|
||||||
|
log.Fatal("Cannot stat repository directory").Str("repo", repoName).Err(err).Send()
|
||||||
|
} else {
|
||||||
|
log.Info("Repository already exists").Str("name", repoName).Send()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Проверяем, что есть хотя бы один репозиторий
|
||||||
|
if len(cfg.Repositories) == 0 {
|
||||||
|
log.Fatal("No repositories configured. At least one repository is required.").Send()
|
||||||
}
|
}
|
||||||
|
|
||||||
starFiles, err := filepath.Glob(filepath.Join(*pluginDir, "*.star"))
|
starFiles, err := filepath.Glob(filepath.Join(*pluginDir, "*.star"))
|
||||||
@@ -129,9 +191,26 @@ func main() {
|
|||||||
|
|
||||||
for _, starFile := range starFiles {
|
for _, starFile := range starFiles {
|
||||||
pluginName := filepath.Base(strings.TrimSuffix(starFile, ".star"))
|
pluginName := filepath.Base(strings.TrimSuffix(starFile, ".star"))
|
||||||
|
|
||||||
|
// Парсим комментарий Repository из файла плагина
|
||||||
|
repoName, err := parseRepositoryFromPlugin(starFile)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("Error parsing repository from plugin").Str("file", starFile).Err(err).Send()
|
||||||
|
}
|
||||||
|
if repoName == "" {
|
||||||
|
log.Fatal("Plugin must specify repository").Str("file", starFile).Str("hint", "Add '# Repository: repo-name' comment at the top").Send()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Проверяем, что указанный репозиторий существует в конфигурации
|
||||||
|
if _, exists := cfg.Repositories[repoName]; !exists {
|
||||||
|
log.Fatal("Repository not found in configuration").Str("plugin", pluginName).Str("repository", repoName).Send()
|
||||||
|
}
|
||||||
|
|
||||||
thread := &starlark.Thread{Name: pluginName}
|
thread := &starlark.Thread{Name: pluginName}
|
||||||
|
|
||||||
predeclared := starlark.StringDict{}
|
predeclared := starlark.StringDict{
|
||||||
|
"REPO": starlark.String(repoName), // Передаем имя репозитория как глобальную переменную
|
||||||
|
}
|
||||||
builtins.Register(predeclared, &builtins.Options{
|
builtins.Register(predeclared, &builtins.Options{
|
||||||
Name: pluginName,
|
Name: pluginName,
|
||||||
Config: cfg,
|
Config: cfg,
|
||||||
@@ -145,7 +224,7 @@ func main() {
|
|||||||
log.Fatal("Error executing starlark file").Str("file", starFile).Err(err).Send()
|
log.Fatal("Error executing starlark file").Str("file", starFile).Err(err).Send()
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Info("Initialized plugin").Str("name", pluginName).Send()
|
log.Info("Initialized plugin").Str("name", pluginName).Str("repository", repoName).Send()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Запускаем все зарегистрированные функции немедленно если установлен флаг --now
|
// Запускаем все зарегистрированные функции немедленно если установлен флаг --now
|
||||||
|
Reference in New Issue
Block a user