feat: add import info from alr-repo.toml

This commit is contained in:
2025-07-07 17:45:20 +03:00
parent 1cc408ad7d
commit f42be105ad
19 changed files with 403 additions and 82 deletions

View File

@ -24,6 +24,7 @@ import (
"strings"
"sync"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-plugin"
"gitea.plemya-x.ru/Plemya-x/ALR/internal/logger"
@ -32,6 +33,7 @@ import (
var pluginMap = map[string]plugin.Plugin{
"script-executor": &ScriptExecutorPlugin{},
"installer": &InstallerExecutorPlugin{},
"repos": &ReposExecutorPlugin{},
}
var HandshakeConfig = plugin.HandshakeConfig{
@ -57,6 +59,19 @@ func setCommonCmdEnv(cmd *exec.Cmd) {
}
}
func GetPluginServeCommonConfig() *plugin.ServeConfig {
return &plugin.ServeConfig{
HandshakeConfig: HandshakeConfig,
Logger: hclog.New(&hclog.LoggerOptions{
Name: "plugin",
Output: os.Stderr,
Level: hclog.Trace,
JSONFormat: true,
DisableTime: true,
}),
}
}
func GetSafeInstaller() (InstallerExecutor, func(), error) {
return getSafeExecutor[InstallerExecutor]("_internal-installer", "installer")
}
@ -65,6 +80,10 @@ func GetSafeScriptExecutor() (ScriptExecutor, func(), error) {
return getSafeExecutor[ScriptExecutor]("_internal-safe-script-executor", "script-executor")
}
func GetSafeReposExecutor() (ReposExecutor, func(), error) {
return getSafeExecutor[ReposExecutor]("_internal-repos", "repos")
}
func getSafeExecutor[T any](subCommand, pluginName string) (T, func(), error) {
var err error

View File

@ -21,9 +21,10 @@ import (
"gitea.plemya-x.ru/Plemya-x/ALR/internal/manager"
"gitea.plemya-x.ru/Plemya-x/ALR/pkg/alrsh"
"gitea.plemya-x.ru/Plemya-x/ALR/pkg/types"
)
//go:generate go run ../../generators/plugin-generator InstallerExecutor ScriptExecutor
//go:generate go run ../../generators/plugin-generator InstallerExecutor ScriptExecutor ReposExecutor
// The Executors interfaces must use context.Context as the first parameter,
// because the plugin-generator cannot generate code without it.
@ -53,3 +54,7 @@ type ScriptExecutor interface {
basePkg string,
) ([]*BuiltDep, error)
}
type ReposExecutor interface {
PullOneAndUpdateFromConfig(ctx context.Context, repo *types.Repo) (types.Repo, error)
}

View File

@ -24,6 +24,7 @@ import (
"context"
"gitea.plemya-x.ru/Plemya-x/ALR/internal/manager"
"gitea.plemya-x.ru/Plemya-x/ALR/pkg/alrsh"
"gitea.plemya-x.ru/Plemya-x/ALR/pkg/types"
"github.com/hashicorp/go-plugin"
)
@ -67,6 +68,26 @@ func (p *ScriptExecutorPlugin) Server(*plugin.MuxBroker) (interface{}, error) {
return &ScriptExecutorRPCServer{Impl: p.Impl}, nil
}
type ReposExecutorPlugin struct {
Impl ReposExecutor
}
type ReposExecutorRPCServer struct {
Impl ReposExecutor
}
type ReposExecutorRPC struct {
client *rpc.Client
}
func (p *ReposExecutorPlugin) Client(b *plugin.MuxBroker, c *rpc.Client) (interface{}, error) {
return &ReposExecutorRPC{client: c}, nil
}
func (p *ReposExecutorPlugin) Server(*plugin.MuxBroker) (interface{}, error) {
return &ReposExecutorRPCServer{Impl: p.Impl}, nil
}
type InstallerExecutorInstallLocalArgs struct {
Paths []string
Opts *manager.Opts
@ -316,3 +337,33 @@ func (s *ScriptExecutorRPCServer) ExecuteSecondPass(args *ScriptExecutorExecuteS
}
return nil
}
type ReposExecutorPullOneAndUpdateFromConfigArgs struct {
Repo *types.Repo
}
type ReposExecutorPullOneAndUpdateFromConfigResp struct {
Result0 types.Repo
}
func (s *ReposExecutorRPC) PullOneAndUpdateFromConfig(ctx context.Context, repo *types.Repo) (types.Repo, error) {
var resp *ReposExecutorPullOneAndUpdateFromConfigResp
err := s.client.Call("Plugin.PullOneAndUpdateFromConfig", &ReposExecutorPullOneAndUpdateFromConfigArgs{
Repo: repo,
}, &resp)
if err != nil {
return types.Repo{}, err
}
return resp.Result0, nil
}
func (s *ReposExecutorRPCServer) PullOneAndUpdateFromConfig(args *ReposExecutorPullOneAndUpdateFromConfigArgs, resp *ReposExecutorPullOneAndUpdateFromConfigResp) error {
result0, err := s.Impl.PullOneAndUpdateFromConfig(context.Background(), args.Repo)
if err != nil {
return err
}
*resp = ReposExecutorPullOneAndUpdateFromConfigResp{
Result0: result0,
}
return nil
}

View File

@ -0,0 +1,37 @@
// ALR - Any Linux Repository
// Copyright (C) 2025 The ALR Authors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package build
import (
"context"
"gitea.plemya-x.ru/Plemya-x/ALR/internal/repos"
"gitea.plemya-x.ru/Plemya-x/ALR/pkg/types"
)
type reposExecutor struct{ r *repos.Repos }
func NewRepos(r *repos.Repos) ReposExecutor {
return &reposExecutor{r}
}
func (r *reposExecutor) PullOneAndUpdateFromConfig(ctx context.Context, repo *types.Repo) (types.Repo, error) {
if err := r.r.PullOneAndUpdateFromConfig(ctx, repo); err != nil {
return *repo, err
}
return *repo, nil
}

View File

@ -68,7 +68,7 @@ func (rs *Repos) Pull(ctx context.Context, repos []types.Repo) error {
}
for _, repo := range repos {
err := rs.pullRepo(ctx, repo)
err := rs.pullRepo(ctx, &repo, false)
if err != nil {
return err
}
@ -77,7 +77,16 @@ func (rs *Repos) Pull(ctx context.Context, repos []types.Repo) error {
return nil
}
func (rs *Repos) pullRepo(ctx context.Context, repo types.Repo) error {
func (rs *Repos) PullOneAndUpdateFromConfig(ctx context.Context, repo *types.Repo) error {
err := rs.pullRepo(ctx, repo, true)
if err != nil {
return err
}
return nil
}
func (rs *Repos) pullRepo(ctx context.Context, repo *types.Repo, updateRepoFromToml bool) error {
urls := []string{repo.URL}
urls = append(urls, repo.Mirrors...)
@ -88,7 +97,7 @@ func (rs *Repos) pullRepo(ctx context.Context, repo types.Repo) error {
slog.Info(gotext.Get("Trying mirror"), "repo", repo.Name, "mirror", repoURL)
}
err := rs.pullRepoFromURL(ctx, repoURL, repo)
err := rs.pullRepoFromURL(ctx, repoURL, repo, updateRepoFromToml)
if err != nil {
lastErr = err
slog.Warn(gotext.Get("Failed to pull from URL"), "repo", repo.Name, "url", repoURL, "error", err)
@ -149,7 +158,7 @@ func readGitRepo(repoDir, repoUrl string) (*git.Repository, bool, error) {
return r, true, nil
}
func (rs *Repos) pullRepoFromURL(ctx context.Context, rawRepoUrl string, repo types.Repo) error {
func (rs *Repos) pullRepoFromURL(ctx context.Context, rawRepoUrl string, repo *types.Repo, update bool) error {
repoURL, err := url.Parse(rawRepoUrl)
if err != nil {
return fmt.Errorf("invalid URL %s: %w", rawRepoUrl, err)
@ -214,12 +223,12 @@ func (rs *Repos) pullRepoFromURL(ctx context.Context, rawRepoUrl string, repo ty
// empty. In this case, we need to update the DB fully
// rather than just incrementally.
if rs.db.IsEmpty() || freshGit {
err = rs.processRepoFull(ctx, repo, repoDir)
err = rs.processRepoFull(ctx, *repo, repoDir)
if err != nil {
return err
}
} else {
err = rs.processRepoChanges(ctx, repo, r, w, old, new)
err = rs.processRepoChanges(ctx, *repo, r, w, old, new)
if err != nil {
return err
}
@ -247,6 +256,18 @@ func (rs *Repos) pullRepoFromURL(ctx context.Context, rawRepoUrl string, repo ty
}
}
if update {
if repoCfg.Repo.URL != "" {
repo.URL = repoCfg.Repo.URL
}
if repoCfg.Repo.Ref != "" {
repo.Ref = repoCfg.Repo.Ref
}
if len(repoCfg.Repo.Mirrors) > 0 {
repo.Mirrors = repoCfg.Repo.Mirrors
}
}
return nil
}

View File

@ -407,27 +407,27 @@ msgstr ""
msgid "ERROR"
msgstr ""
#: internal/repos/pull.go:88
#: internal/repos/pull.go:97
msgid "Trying mirror"
msgstr ""
#: internal/repos/pull.go:94
#: internal/repos/pull.go:103
msgid "Failed to pull from URL"
msgstr ""
#: internal/repos/pull.go:158
#: internal/repos/pull.go:167
msgid "Pulling repository"
msgstr ""
#: internal/repos/pull.go:195
#: internal/repos/pull.go:204
msgid "Repository up to date"
msgstr ""
#: internal/repos/pull.go:230
#: internal/repos/pull.go:239
msgid "Git repository does not appear to be a valid ALR repo"
msgstr ""
#: internal/repos/pull.go:246
#: internal/repos/pull.go:255
msgid ""
"ALR repo's minimum ALR version is greater than the current version. Try "
"updating ALR if something doesn't work."
@ -481,11 +481,11 @@ msgstr ""
msgid "Enable interactive questions and prompts"
msgstr ""
#: main.go:147
#: main.go:148
msgid "Show help"
msgstr ""
#: main.go:151
#: main.go:152
msgid "Error while running app"
msgstr ""
@ -517,44 +517,44 @@ msgstr ""
msgid "Pull all repositories that have changed"
msgstr ""
#: repo.go:41
#: repo.go:42
msgid "Manage repos"
msgstr ""
#: repo.go:55 repo.go:625
#: repo.go:56 repo.go:625
msgid "Remove an existing repository"
msgstr ""
#: repo.go:57 repo.go:521
#: repo.go:58 repo.go:521
msgid "<name>"
msgstr ""
#: repo.go:102 repo.go:465 repo.go:568
#: repo.go:103 repo.go:465 repo.go:568
msgid "Repo \"%s\" does not exist"
msgstr ""
#: repo.go:109
#: repo.go:110
msgid "Error removing repo directory"
msgstr ""
#: repo.go:113 repo.go:180 repo.go:253 repo.go:316 repo.go:389 repo.go:504
#: repo.go:114 repo.go:195 repo.go:253 repo.go:316 repo.go:389 repo.go:504
#: repo.go:576
msgid "Error saving config"
msgstr ""
#: repo.go:132
#: repo.go:133
msgid "Error removing packages from database"
msgstr ""
#: repo.go:143 repo.go:595
#: repo.go:144 repo.go:595
msgid "Add a new repository"
msgstr ""
#: repo.go:144 repo.go:270 repo.go:345 repo.go:402
#: repo.go:145 repo.go:270 repo.go:345 repo.go:402
msgid "<name> <url>"
msgstr ""
#: repo.go:169
#: repo.go:170
msgid "Repo \"%s\" already exists"
msgstr ""

View File

@ -421,27 +421,27 @@ msgstr ""
msgid "ERROR"
msgstr "ОШИБКА"
#: internal/repos/pull.go:88
#: internal/repos/pull.go:97
msgid "Trying mirror"
msgstr "Пробую зеркало"
#: internal/repos/pull.go:94
#: internal/repos/pull.go:103
msgid "Failed to pull from URL"
msgstr "Не удалось извлечь из URL"
#: internal/repos/pull.go:158
#: internal/repos/pull.go:167
msgid "Pulling repository"
msgstr "Скачивание репозитория"
#: internal/repos/pull.go:195
#: internal/repos/pull.go:204
msgid "Repository up to date"
msgstr "Репозиторий уже обновлён"
#: internal/repos/pull.go:230
#: internal/repos/pull.go:239
msgid "Git repository does not appear to be a valid ALR repo"
msgstr "Репозиторий Git не поддерживается репозиторием ALR"
#: internal/repos/pull.go:246
#: internal/repos/pull.go:255
msgid ""
"ALR repo's minimum ALR version is greater than the current version. Try "
"updating ALR if something doesn't work."
@ -497,11 +497,11 @@ msgstr "Аргументы, которые будут переданы мене
msgid "Enable interactive questions and prompts"
msgstr "Включение интерактивных вопросов и запросов"
#: main.go:147
#: main.go:148
msgid "Show help"
msgstr "Показать справку"
#: main.go:151
#: main.go:152
msgid "Error while running app"
msgstr "Ошибка при запуске приложения"
@ -533,44 +533,44 @@ msgstr "%s %s загружается — %s/с\n"
msgid "Pull all repositories that have changed"
msgstr "Скачать все изменённые репозитории"
#: repo.go:41
#: repo.go:42
msgid "Manage repos"
msgstr "Управление репозиториями"
#: repo.go:55 repo.go:625
#: repo.go:56 repo.go:625
msgid "Remove an existing repository"
msgstr "Удалить существующий репозиторий"
#: repo.go:57 repo.go:521
#: repo.go:58 repo.go:521
msgid "<name>"
msgstr "<имя>"
#: repo.go:102 repo.go:465 repo.go:568
#: repo.go:103 repo.go:465 repo.go:568
msgid "Repo \"%s\" does not exist"
msgstr "Репозитория \"%s\" не существует"
#: repo.go:109
#: repo.go:110
msgid "Error removing repo directory"
msgstr "Ошибка при удалении каталога репозитория"
#: repo.go:113 repo.go:180 repo.go:253 repo.go:316 repo.go:389 repo.go:504
#: repo.go:114 repo.go:195 repo.go:253 repo.go:316 repo.go:389 repo.go:504
#: repo.go:576
msgid "Error saving config"
msgstr "Ошибка при сохранении конфигурации"
#: repo.go:132
#: repo.go:133
msgid "Error removing packages from database"
msgstr "Ошибка при удалении пакетов из базы данных"
#: repo.go:143 repo.go:595
#: repo.go:144 repo.go:595
msgid "Add a new repository"
msgstr "Добавить новый репозиторий"
#: repo.go:144 repo.go:270 repo.go:345 repo.go:402
#: repo.go:145 repo.go:270 repo.go:345 repo.go:402
msgid "<name> <url>"
msgstr "<имя> <url>"
#: repo.go:169
#: repo.go:170
msgid "Repo \"%s\" already exists"
msgstr "Репозиторий \"%s\" уже существует"

View File

@ -131,11 +131,11 @@ func EnsureIsAlrUser() error {
}
newUid := syscall.Getuid()
if newUid != uid {
return errors.New("new uid don't matches requested")
return errors.New("uid don't matches requested")
}
newGid := syscall.Getgid()
if newGid != gid {
return errors.New("new gid don't matches requested")
return errors.New("gid don't matches requested")
}
return nil
}