diff --git a/fix.go b/fix.go index 163b709..218b389 100644 --- a/fix.go +++ b/fix.go @@ -38,41 +38,43 @@ func FixCmd() *cli.Command { Name: "fix", Usage: gotext.Get("Attempt to fix problems with ALR"), Action: func(c *cli.Context) error { - if utils.DropCapsToAlrUser() != nil { + err := utils.DropCapsToAlrUser() + if err != nil { slog.Error(gotext.Get("Can't drop privileges")) - os.Exit(1) + return cli.Exit(err, 1) } ctx := c.Context cfg := config.New() - err := cfg.Load() + err = cfg.Load() if err != nil { - slog.Error(gotext.Get("Error loading config"), "err", err) - os.Exit(1) + slog.Error(gotext.Get("Error loading config")) + return cli.Exit(err, 1) } paths := cfg.GetPaths() - slog.Info(gotext.Get("Removing cache directory")) + slog.Info(gotext.Get("Clearing cache directory")) + // Remove all nested directories of paths.CacheDir dir, err := os.Open(paths.CacheDir) if err != nil { - slog.Error(gotext.Get("Unable to open cache directory"), "err", err) - os.Exit(1) + slog.Error(gotext.Get("Unable to open cache directory")) + return cli.Exit(err, 1) } defer dir.Close() entries, err := dir.Readdirnames(-1) if err != nil { - slog.Error(gotext.Get("Unable to read cache directory contents"), "err", err) - os.Exit(1) + slog.Error(gotext.Get("Unable to read cache directory contents")) + return cli.Exit(err, 1) } for _, entry := range entries { err = os.RemoveAll(filepath.Join(paths.CacheDir, entry)) if err != nil { - slog.Error(gotext.Get("Unable to remove cache item"), "item", entry, "err", err) - os.Exit(1) + slog.Error(gotext.Get("Unable to remove cache item"), "item", entry) + return cli.Exit(err, 1) } } @@ -80,28 +82,28 @@ func FixCmd() *cli.Command { err = os.MkdirAll(paths.CacheDir, 0o755) if err != nil { - slog.Error(gotext.Get("Unable to create new cache directory"), "err", err) - os.Exit(1) + slog.Error(gotext.Get("Unable to create new cache directory")) + return cli.Exit(err, 1) } cfg = config.New() err = cfg.Load() if err != nil { - slog.Error(gotext.Get("Error loading config"), "err", err) - os.Exit(1) + slog.Error(gotext.Get("Error loading config")) + return cli.Exit(err, 1) } db := database.New(cfg) err = db.Init(ctx) if err != nil { - slog.Error(gotext.Get("Error initialization database"), "err", err) - os.Exit(1) + slog.Error(gotext.Get("Error initialization database")) + return cli.Exit(err, 1) } rs := repos.New(cfg, db) err = rs.Pull(ctx, cfg.Repos()) if err != nil { - slog.Error(gotext.Get("Error pulling repos"), "err", err) - os.Exit(1) + slog.Error(gotext.Get("Error pulling repos")) + return cli.Exit(err, 1) } slog.Info(gotext.Get("Done")) diff --git a/internal/translations/default.pot b/internal/translations/default.pot index 7322f70..596b505 100644 --- a/internal/translations/default.pot +++ b/internal/translations/default.pot @@ -74,39 +74,39 @@ msgstr "" msgid "Attempt to fix problems with ALR" msgstr "" -#: fix.go:42 +#: fix.go:43 msgid "Can't drop privileges" msgstr "" -#: fix.go:56 -msgid "Removing cache directory" +#: fix.go:57 +msgid "Clearing cache directory" msgstr "" -#: fix.go:60 +#: fix.go:62 msgid "Unable to open cache directory" msgstr "" -#: fix.go:67 +#: fix.go:69 msgid "Unable to read cache directory contents" msgstr "" -#: fix.go:74 +#: fix.go:76 msgid "Unable to remove cache item" msgstr "" -#: fix.go:79 +#: fix.go:81 msgid "Rebuilding cache" msgstr "" -#: fix.go:83 +#: fix.go:85 msgid "Unable to create new cache directory" msgstr "" -#: fix.go:103 +#: fix.go:105 msgid "Error pulling repos" msgstr "" -#: fix.go:107 +#: fix.go:109 msgid "Done" msgstr "" @@ -316,7 +316,7 @@ msgid "ERROR" msgstr "" #: internal/utils/cmd.go:74 -msgid "You need to be root" +msgid "You need to be root to perform this action" msgstr "" #: list.go:41 @@ -435,31 +435,31 @@ msgstr "" msgid "URL of the new repo" msgstr "" -#: repo.go:89 repo.go:166 +#: repo.go:92 repo.go:172 msgid "Error saving config" msgstr "" -#: repo.go:119 +#: repo.go:122 msgid "Remove an existing repository" msgstr "" -#: repo.go:126 +#: repo.go:129 msgid "Name of the repo to be deleted" msgstr "" -#: repo.go:152 +#: repo.go:158 msgid "Repo does not exist" msgstr "" -#: repo.go:160 +#: repo.go:166 msgid "Error removing repo directory" msgstr "" -#: repo.go:177 +#: repo.go:183 msgid "Error removing packages from database" msgstr "" -#: repo.go:189 +#: repo.go:195 msgid "Pull all repositories that have changed" msgstr "" diff --git a/internal/translations/po/ru/default.po b/internal/translations/po/ru/default.po index 05978bc..b83cae7 100644 --- a/internal/translations/po/ru/default.po +++ b/internal/translations/po/ru/default.po @@ -84,42 +84,43 @@ msgstr "Ошибка при перемещении пакета" msgid "Attempt to fix problems with ALR" msgstr "Попытка устранить проблемы с ALR" -#: fix.go:42 +#: fix.go:43 msgid "Can't drop privileges" msgstr "" -#: fix.go:56 -msgid "Removing cache directory" +#: fix.go:57 +#, fuzzy +msgid "Clearing cache directory" msgstr "Удаление каталога кэша" -#: fix.go:60 +#: fix.go:62 #, fuzzy msgid "Unable to open cache directory" msgstr "Не удалось удалить каталог кэша" -#: fix.go:67 +#: fix.go:69 #, fuzzy msgid "Unable to read cache directory contents" msgstr "Не удалось удалить каталог кэша" -#: fix.go:74 +#: fix.go:76 #, fuzzy msgid "Unable to remove cache item" msgstr "Не удалось удалить каталог кэша" -#: fix.go:79 +#: fix.go:81 msgid "Rebuilding cache" msgstr "Восстановление кэша" -#: fix.go:83 +#: fix.go:85 msgid "Unable to create new cache directory" msgstr "Не удалось создать новый каталог кэша" -#: fix.go:103 +#: fix.go:105 msgid "Error pulling repos" msgstr "Ошибка при извлечении репозиториев" -#: fix.go:107 +#: fix.go:109 msgid "Done" msgstr "Сделано" @@ -330,7 +331,7 @@ msgid "ERROR" msgstr "ОШИБКА" #: internal/utils/cmd.go:74 -msgid "You need to be root" +msgid "You need to be root to perform this action" msgstr "" #: list.go:41 @@ -455,32 +456,32 @@ msgstr "Название нового репозитория" msgid "URL of the new repo" msgstr "URL-адрес нового репозитория" -#: repo.go:89 repo.go:166 +#: repo.go:92 repo.go:172 #, fuzzy msgid "Error saving config" msgstr "Ошибка при кодировании конфигурации" -#: repo.go:119 +#: repo.go:122 msgid "Remove an existing repository" msgstr "Удалить существующий репозиторий" -#: repo.go:126 +#: repo.go:129 msgid "Name of the repo to be deleted" msgstr "Название репозитория удалён" -#: repo.go:152 +#: repo.go:158 msgid "Repo does not exist" msgstr "Репозитория не существует" -#: repo.go:160 +#: repo.go:166 msgid "Error removing repo directory" msgstr "Ошибка при удалении каталога репозитория" -#: repo.go:177 +#: repo.go:183 msgid "Error removing packages from database" msgstr "Ошибка при удалении пакетов из базы данных" -#: repo.go:189 +#: repo.go:195 msgid "Pull all repositories that have changed" msgstr "Скачать все изменённые репозитории" diff --git a/internal/utils/cmd.go b/internal/utils/cmd.go index b851f91..b4439a0 100644 --- a/internal/utils/cmd.go +++ b/internal/utils/cmd.go @@ -17,13 +17,13 @@ package utils import ( - "log/slog" "os" "os/user" "strconv" "syscall" "github.com/leonelquinteros/gotext" + "github.com/urfave/cli/v2" ) func GetUidGidAlrUserString() (string, string, error) { @@ -69,9 +69,9 @@ func DropCapsToAlrUser() error { return nil } -func ExitIfNotRoot() { +func ExitIfNotRoot() error { if os.Getuid() != 0 { - slog.Error(gotext.Get("You need to be root")) - os.Exit(1) + return cli.Exit(gotext.Get("You need to be root to perform this action"), 1) } + return nil } diff --git a/repo.go b/repo.go index d2ad6af..7a599c2 100644 --- a/repo.go +++ b/repo.go @@ -55,7 +55,10 @@ func AddRepoCmd() *cli.Command { }, }, Action: func(c *cli.Context) error { - utils.ExitIfNotRoot() + err := utils.ExitIfNotRoot() + if err != nil { + return err + } ctx := c.Context @@ -63,7 +66,7 @@ func AddRepoCmd() *cli.Command { repoURL := c.String("url") cfg := config.New() - err := cfg.Load() + err = cfg.Load() if err != nil { slog.Error(gotext.Get("Error loading config"), "err", err) os.Exit(1) @@ -127,13 +130,16 @@ func RemoveRepoCmd() *cli.Command { }, }, Action: func(c *cli.Context) error { - utils.ExitIfNotRoot() + err := utils.ExitIfNotRoot() + if err != nil { + return err + } ctx := c.Context name := c.String("name") cfg := config.New() - err := cfg.Load() + err = cfg.Load() if err != nil { slog.Error(gotext.Get("Error loading config"), "err", err) os.Exit(1)