diff --git a/assets/i18n-ru-badge.svg b/assets/i18n-ru-badge.svg index a019f1e..b5c7350 100644 --- a/assets/i18n-ru-badge.svg +++ b/assets/i18n-ru-badge.svg @@ -12,7 +12,7 @@ ru translate ru translate - 98.00% - 98.00% + 99.00% + 99.00% diff --git a/install.go b/install.go index cec2370..47d3e75 100644 --- a/install.go +++ b/install.go @@ -28,6 +28,7 @@ import ( "github.com/urfave/cli/v2" "gitea.plemya-x.ru/Plemya-x/ALR/internal/cliutils" + appbuilder "gitea.plemya-x.ru/Plemya-x/ALR/internal/cliutils/app_builder" "gitea.plemya-x.ru/Plemya-x/ALR/internal/config" database "gitea.plemya-x.ru/Plemya-x/ALR/internal/db" "gitea.plemya-x.ru/Plemya-x/ALR/internal/types" @@ -157,17 +158,17 @@ func RemoveCmd() *cli.Command { Usage: gotext.Get("Remove an installed package"), Aliases: []string{"rm"}, BashComplete: cliutils.BashCompleteWithError(func(c *cli.Context) error { - cfg := config.New() - err := cfg.Load() - if err != nil { - return cliutils.FormatCliExit(gotext.Get("Error loading config"), err) - } + ctx := c.Context - db := database.New(cfg) - err = db.Init(c.Context) + deps, err := appbuilder. + New(ctx). + WithConfig(). + WithDB(). + Build() if err != nil { - return cliutils.FormatCliExit(gotext.Get("Error initialization database"), err) + return cli.Exit(err, 1) } + defer deps.Defer() installedAlrPackages := map[string]string{} mgr := manager.Detect() @@ -188,7 +189,7 @@ func RemoveCmd() *cli.Command { } } - result, err := db.GetPkgs(c.Context, "true") + result, err := deps.DB.GetPkgs(c.Context, "true") if err != nil { return cliutils.FormatCliExit(gotext.Get("Error getting packages"), err) } @@ -212,16 +213,18 @@ func RemoveCmd() *cli.Command { return nil }), Action: func(c *cli.Context) error { + if err := utils.ExitIfNotRoot(); err != nil { + return err + } + args := c.Args() if args.Len() < 1 { - slog.Error(gotext.Get("Command remove expected at least 1 argument, got %d", args.Len())) - os.Exit(1) + return cliutils.FormatCliExit(gotext.Get("Command remove expected at least 1 argument, got %d", args.Len()), nil) } mgr := manager.Detect() if mgr == nil { - slog.Error(gotext.Get("Unable to detect a supported package manager on the system")) - os.Exit(1) + return cliutils.FormatCliExit(gotext.Get("Unable to detect a supported package manager on the system"), nil) } err := mgr.Remove(&manager.Opts{ diff --git a/internal/cliutils/app_builder/builder.go b/internal/cliutils/app_builder/builder.go index 0aa9bf4..a32e241 100644 --- a/internal/cliutils/app_builder/builder.go +++ b/internal/cliutils/app_builder/builder.go @@ -53,6 +53,14 @@ func New(ctx context.Context) *AppBuilder { return &AppBuilder{ctx: ctx} } +func (b *AppBuilder) UseConfig(cfg *config.ALRConfig) *AppBuilder { + if b.err != nil { + return b + } + b.deps.Cfg = cfg + return b +} + func (b *AppBuilder) WithConfig() *AppBuilder { if b.err != nil { return b @@ -92,16 +100,21 @@ func (b *AppBuilder) WithDB() *AppBuilder { } func (b *AppBuilder) WithRepos() *AppBuilder { - b.withRepos(false) + b.withRepos(true, false) return b } func (b *AppBuilder) WithReposForcePull() *AppBuilder { - b.withRepos(true) + b.withRepos(true, true) return b } -func (b *AppBuilder) withRepos(forcePull bool) *AppBuilder { +func (b *AppBuilder) WithReposNoPull() *AppBuilder { + b.withRepos(false, false) + return b +} + +func (b *AppBuilder) withRepos(enablePull, forcePull bool) *AppBuilder { if b.err != nil { return b } @@ -115,7 +128,7 @@ func (b *AppBuilder) withRepos(forcePull bool) *AppBuilder { rs := repos.New(cfg, db) - if forcePull || cfg.AutoPull() { + if enablePull && (forcePull || cfg.AutoPull()) { if err := rs.Pull(b.ctx, cfg.Repos()); err != nil { slog.Error(gotext.Get("Error pulling repositories"), "err", err) b.err = cli.Exit("", 1) diff --git a/internal/cliutils/utils.go b/internal/cliutils/utils.go index 8bcacaa..afd7a04 100644 --- a/internal/cliutils/utils.go +++ b/internal/cliutils/utils.go @@ -17,6 +17,7 @@ package cliutils import ( + "errors" "fmt" "log/slog" @@ -52,5 +53,8 @@ func FormatCliExit(msg string, err error) cli.ExitCoder { } func FormatCliExitWithCode(msg string, err error, exitCode int) cli.ExitCoder { + if err == nil { + return cli.Exit(errors.New(msg), exitCode) + } return cli.Exit(fmt.Errorf("%s: %w", msg, err), exitCode) } diff --git a/internal/translations/default.pot b/internal/translations/default.pot index 0b7281f..4acc4c3 100644 --- a/internal/translations/default.pot +++ b/internal/translations/default.pot @@ -166,31 +166,31 @@ msgstr "" msgid "Error encoding script variables" msgstr "" -#: install.go:44 +#: install.go:45 msgid "Install a new package" msgstr "" -#: install.go:58 +#: install.go:59 msgid "Command install expected at least 1 argument, got %d" msgstr "" -#: install.go:94 +#: install.go:95 msgid "Error pulling repositories" msgstr "" -#: install.go:157 +#: install.go:158 msgid "Remove an installed package" msgstr "" -#: install.go:180 +#: install.go:181 msgid "Error listing installed packages" msgstr "" -#: install.go:217 +#: install.go:222 msgid "Command remove expected at least 1 argument, got %d" msgstr "" -#: install.go:232 +#: install.go:235 msgid "Error removing packages" msgstr "" @@ -419,51 +419,47 @@ msgid "" "updating ALR if something doesn't work." msgstr "" -#: repo.go:43 +#: repo.go:40 msgid "Add a new repository" msgstr "" -#: repo.go:50 +#: repo.go:47 msgid "Name of the new repo" msgstr "" -#: repo.go:56 +#: repo.go:53 msgid "URL of the new repo" msgstr "" -#: repo.go:93 repo.go:169 +#: repo.go:80 +msgid "Repo %s already exists" +msgstr "" + +#: repo.go:91 repo.go:169 msgid "Error saving config" msgstr "" -#: repo.go:97 -msgid "Can't drop privileges" -msgstr "" - -#: repo.go:104 repo.go:110 -msgid "Error pulling repos" -msgstr "" - -#: repo.go:121 +#: repo.go:117 msgid "Remove an existing repository" msgstr "" -#: repo.go:128 +#: repo.go:124 msgid "Name of the repo to be deleted" msgstr "" -#: repo.go:156 +#: repo.go:157 msgid "Repo does not exist" msgstr "" -#: repo.go:164 +#: repo.go:165 msgid "Error removing repo directory" msgstr "" -#: repo.go:179 +#: repo.go:188 msgid "Error removing packages from database" msgstr "" -#: repo.go:190 +#: repo.go:199 msgid "Pull all repositories that have changed" msgstr "" @@ -507,6 +503,10 @@ msgstr "" msgid "Upgrade all installed packages" msgstr "" +#: upgrade.go:101 +msgid "Error pulling repos" +msgstr "" + #: upgrade.go:107 upgrade.go:124 msgid "Error checking for updates" msgstr "" diff --git a/internal/translations/po/ru/default.po b/internal/translations/po/ru/default.po index 2beca04..6661d8f 100644 --- a/internal/translations/po/ru/default.po +++ b/internal/translations/po/ru/default.po @@ -181,31 +181,31 @@ msgstr "Ошибка устранения переорпеделений" msgid "Error encoding script variables" msgstr "Ошибка кодирования переменных скрита" -#: install.go:44 +#: install.go:45 msgid "Install a new package" msgstr "Установить новый пакет" -#: install.go:58 +#: install.go:59 msgid "Command install expected at least 1 argument, got %d" msgstr "Для команды install ожидался хотя бы 1 аргумент, получено %d" -#: install.go:94 +#: install.go:95 msgid "Error pulling repositories" msgstr "Ошибка при извлечении репозиториев" -#: install.go:157 +#: install.go:158 msgid "Remove an installed package" msgstr "Удалить установленный пакет" -#: install.go:180 +#: install.go:181 msgid "Error listing installed packages" msgstr "Ошибка при составлении списка установленных пакетов" -#: install.go:217 +#: install.go:222 msgid "Command remove expected at least 1 argument, got %d" msgstr "Для команды remove ожидался хотя бы 1 аргумент, получено %d" -#: install.go:232 +#: install.go:235 msgid "Error removing packages" msgstr "Ошибка при удалении пакетов" @@ -441,52 +441,49 @@ msgstr "" "Минимальная версия ALR для ALR-репозитория выше текущей версии. Попробуйте " "обновить ALR, если что-то не работает." -#: repo.go:43 +#: repo.go:40 msgid "Add a new repository" msgstr "Добавить новый репозиторий" -#: repo.go:50 +#: repo.go:47 msgid "Name of the new repo" msgstr "Название нового репозитория" -#: repo.go:56 +#: repo.go:53 msgid "URL of the new repo" msgstr "URL-адрес нового репозитория" -#: repo.go:93 repo.go:169 +#: repo.go:80 +#, fuzzy +msgid "Repo %s already exists" +msgstr "Репозитория не существует" + +#: repo.go:91 repo.go:169 #, fuzzy msgid "Error saving config" msgstr "Ошибка при кодировании конфигурации" -#: repo.go:97 -msgid "Can't drop privileges" -msgstr "" - -#: repo.go:104 repo.go:110 -msgid "Error pulling repos" -msgstr "Ошибка при извлечении репозиториев" - -#: repo.go:121 +#: repo.go:117 msgid "Remove an existing repository" msgstr "Удалить существующий репозиторий" -#: repo.go:128 +#: repo.go:124 msgid "Name of the repo to be deleted" msgstr "Название репозитория удалён" -#: repo.go:156 +#: repo.go:157 msgid "Repo does not exist" msgstr "Репозитория не существует" -#: repo.go:164 +#: repo.go:165 msgid "Error removing repo directory" msgstr "Ошибка при удалении каталога репозитория" -#: repo.go:179 +#: repo.go:188 msgid "Error removing packages from database" msgstr "Ошибка при удалении пакетов из базы данных" -#: repo.go:190 +#: repo.go:199 msgid "Pull all repositories that have changed" msgstr "Скачать все изменённые репозитории" @@ -531,6 +528,10 @@ msgstr "Ошибка при выполнении шаблона" msgid "Upgrade all installed packages" msgstr "Обновить все установленные пакеты" +#: upgrade.go:101 +msgid "Error pulling repos" +msgstr "Ошибка при извлечении репозиториев" + #: upgrade.go:107 upgrade.go:124 msgid "Error checking for updates" msgstr "Ошибка при проверке обновлений" diff --git a/internal/utils/cmd.go b/internal/utils/cmd.go index cb10e51..09e778f 100644 --- a/internal/utils/cmd.go +++ b/internal/utils/cmd.go @@ -18,7 +18,6 @@ package utils import ( "errors" - "log/slog" "os" "os/user" "strconv" @@ -26,6 +25,8 @@ import ( "github.com/leonelquinteros/gotext" "github.com/urfave/cli/v2" + + "gitea.plemya-x.ru/Plemya-x/ALR/internal/cliutils" ) func GetUidGidAlrUserString() (string, string, error) { @@ -83,8 +84,7 @@ func DropCapsToAlrUser() error { func ExitIfCantDropCapsToAlrUser() cli.ExitCoder { err := DropCapsToAlrUser() if err != nil { - slog.Debug("dropping capabilities error", "err", err) - return cli.Exit(gotext.Get("Error dropping capabilities"), 1) + return cliutils.FormatCliExit(gotext.Get("Error dropping capabilities"), err) } return nil } diff --git a/repo.go b/repo.go index da5498c..55bf1c6 100644 --- a/repo.go +++ b/repo.go @@ -30,11 +30,8 @@ import ( "gitea.plemya-x.ru/Plemya-x/ALR/internal/cliutils" appbuilder "gitea.plemya-x.ru/Plemya-x/ALR/internal/cliutils/app_builder" - "gitea.plemya-x.ru/Plemya-x/ALR/internal/config" - database "gitea.plemya-x.ru/Plemya-x/ALR/internal/db" "gitea.plemya-x.ru/Plemya-x/ALR/internal/types" "gitea.plemya-x.ru/Plemya-x/ALR/internal/utils" - "gitea.plemya-x.ru/Plemya-x/ALR/pkg/repos" ) func AddRepoCmd() *cli.Command { @@ -57,31 +54,32 @@ func AddRepoCmd() *cli.Command { }, }, Action: func(c *cli.Context) error { - err := utils.ExitIfNotRoot() - if err != nil { + if err := utils.ExitIfNotRoot(); err != nil { return err } - ctx := c.Context - name := c.String("name") repoURL := c.String("url") - cfg := config.New() - err = cfg.Load() + ctx := c.Context + + deps, err := appbuilder. + New(ctx). + WithConfig(). + Build() if err != nil { - return cliutils.FormatCliExit(gotext.Get("Error loading config"), err) + return err } + defer deps.Defer() + + cfg := deps.Cfg reposSlice := cfg.Repos() - for _, repo := range reposSlice { if repo.URL == repoURL { - slog.Error("Repo already exists", "name", repo.Name) - os.Exit(1) + return cliutils.FormatCliExit(gotext.Get("Repo %s already exists", repo.Name), nil) } } - reposSlice = append(reposSlice, types.Repo{ Name: name, URL: repoURL, @@ -93,22 +91,20 @@ func AddRepoCmd() *cli.Command { return cliutils.FormatCliExit(gotext.Get("Error saving config"), err) } - if utils.DropCapsToAlrUser() != nil { - slog.Error(gotext.Get("Can't drop privileges")) - os.Exit(1) + if err := utils.ExitIfCantDropCapsToAlrUser(); err != nil { + return err } - db := database.New(cfg) - err = db.Init(ctx) + deps, err = appbuilder. + New(ctx). + UseConfig(cfg). + WithDB(). + WithReposForcePull(). + Build() if err != nil { - slog.Error(gotext.Get("Error pulling repos"), "err", err) - } - - rs := repos.New(cfg, db) - err = rs.Pull(ctx, cfg.Repos()) - if err != nil { - return cliutils.FormatCliExit(gotext.Get("Error pulling repos"), err) + return err } + defer deps.Defer() return nil }, @@ -129,19 +125,24 @@ func RemoveRepoCmd() *cli.Command { }, }, Action: func(c *cli.Context) error { - err := utils.ExitIfNotRoot() - if err != nil { + if err := utils.ExitIfNotRoot(); err != nil { return err } ctx := c.Context name := c.String("name") - cfg := config.New() - err = cfg.Load() + + deps, err := appbuilder. + New(ctx). + WithConfig(). + Build() if err != nil { - return cliutils.FormatCliExit(gotext.Get("Error loading config"), err) + return err } + defer deps.Defer() + + cfg := deps.Cfg found := false index := 0 @@ -163,18 +164,26 @@ func RemoveRepoCmd() *cli.Command { if err != nil { return cliutils.FormatCliExit(gotext.Get("Error removing repo directory"), err) } - err = cfg.SaveUserConfig() if err != nil { return cliutils.FormatCliExit(gotext.Get("Error saving config"), err) } - db := database.New(cfg) - err = db.Init(ctx) - if err != nil { - os.Exit(1) + if err := utils.ExitIfCantDropCapsToAlrUser(); err != nil { + return err } - err = db.DeletePkgs(ctx, "repository = ?", name) + + deps, err = appbuilder. + New(ctx). + UseConfig(cfg). + WithDB(). + Build() + if err != nil { + return err + } + defer deps.Defer() + + err = deps.DB.DeletePkgs(ctx, "repository = ?", name) if err != nil { return cliutils.FormatCliExit(gotext.Get("Error removing packages from database"), err) }