This commit is contained in:
Maxim Slipenko 2025-04-13 17:11:38 +03:00
parent a51a8ab963
commit 3603dc45a4
5 changed files with 73 additions and 64 deletions

42
fix.go

@ -38,41 +38,43 @@ func FixCmd() *cli.Command {
Name: "fix", Name: "fix",
Usage: gotext.Get("Attempt to fix problems with ALR"), Usage: gotext.Get("Attempt to fix problems with ALR"),
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
if utils.DropCapsToAlrUser() != nil { err := utils.DropCapsToAlrUser()
if err != nil {
slog.Error(gotext.Get("Can't drop privileges")) slog.Error(gotext.Get("Can't drop privileges"))
os.Exit(1) return cli.Exit(err, 1)
} }
ctx := c.Context ctx := c.Context
cfg := config.New() cfg := config.New()
err := cfg.Load() err = cfg.Load()
if err != nil { if err != nil {
slog.Error(gotext.Get("Error loading config"), "err", err) slog.Error(gotext.Get("Error loading config"))
os.Exit(1) return cli.Exit(err, 1)
} }
paths := cfg.GetPaths() 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) dir, err := os.Open(paths.CacheDir)
if err != nil { if err != nil {
slog.Error(gotext.Get("Unable to open cache directory"), "err", err) slog.Error(gotext.Get("Unable to open cache directory"))
os.Exit(1) return cli.Exit(err, 1)
} }
defer dir.Close() defer dir.Close()
entries, err := dir.Readdirnames(-1) entries, err := dir.Readdirnames(-1)
if err != nil { if err != nil {
slog.Error(gotext.Get("Unable to read cache directory contents"), "err", err) slog.Error(gotext.Get("Unable to read cache directory contents"))
os.Exit(1) return cli.Exit(err, 1)
} }
for _, entry := range entries { for _, entry := range entries {
err = os.RemoveAll(filepath.Join(paths.CacheDir, entry)) err = os.RemoveAll(filepath.Join(paths.CacheDir, entry))
if err != nil { if err != nil {
slog.Error(gotext.Get("Unable to remove cache item"), "item", entry, "err", err) slog.Error(gotext.Get("Unable to remove cache item"), "item", entry)
os.Exit(1) return cli.Exit(err, 1)
} }
} }
@ -80,28 +82,28 @@ func FixCmd() *cli.Command {
err = os.MkdirAll(paths.CacheDir, 0o755) err = os.MkdirAll(paths.CacheDir, 0o755)
if err != nil { if err != nil {
slog.Error(gotext.Get("Unable to create new cache directory"), "err", err) slog.Error(gotext.Get("Unable to create new cache directory"))
os.Exit(1) return cli.Exit(err, 1)
} }
cfg = config.New() cfg = config.New()
err = cfg.Load() err = cfg.Load()
if err != nil { if err != nil {
slog.Error(gotext.Get("Error loading config"), "err", err) slog.Error(gotext.Get("Error loading config"))
os.Exit(1) return cli.Exit(err, 1)
} }
db := database.New(cfg) db := database.New(cfg)
err = db.Init(ctx) err = db.Init(ctx)
if err != nil { if err != nil {
slog.Error(gotext.Get("Error initialization database"), "err", err) slog.Error(gotext.Get("Error initialization database"))
os.Exit(1) return cli.Exit(err, 1)
} }
rs := repos.New(cfg, db) rs := repos.New(cfg, db)
err = rs.Pull(ctx, cfg.Repos()) err = rs.Pull(ctx, cfg.Repos())
if err != nil { if err != nil {
slog.Error(gotext.Get("Error pulling repos"), "err", err) slog.Error(gotext.Get("Error pulling repos"))
os.Exit(1) return cli.Exit(err, 1)
} }
slog.Info(gotext.Get("Done")) slog.Info(gotext.Get("Done"))

@ -74,39 +74,39 @@ msgstr ""
msgid "Attempt to fix problems with ALR" msgid "Attempt to fix problems with ALR"
msgstr "" msgstr ""
#: fix.go:42 #: fix.go:43
msgid "Can't drop privileges" msgid "Can't drop privileges"
msgstr "" msgstr ""
#: fix.go:56 #: fix.go:57
msgid "Removing cache directory" msgid "Clearing cache directory"
msgstr "" msgstr ""
#: fix.go:60 #: fix.go:62
msgid "Unable to open cache directory" msgid "Unable to open cache directory"
msgstr "" msgstr ""
#: fix.go:67 #: fix.go:69
msgid "Unable to read cache directory contents" msgid "Unable to read cache directory contents"
msgstr "" msgstr ""
#: fix.go:74 #: fix.go:76
msgid "Unable to remove cache item" msgid "Unable to remove cache item"
msgstr "" msgstr ""
#: fix.go:79 #: fix.go:81
msgid "Rebuilding cache" msgid "Rebuilding cache"
msgstr "" msgstr ""
#: fix.go:83 #: fix.go:85
msgid "Unable to create new cache directory" msgid "Unable to create new cache directory"
msgstr "" msgstr ""
#: fix.go:103 #: fix.go:105
msgid "Error pulling repos" msgid "Error pulling repos"
msgstr "" msgstr ""
#: fix.go:107 #: fix.go:109
msgid "Done" msgid "Done"
msgstr "" msgstr ""
@ -316,7 +316,7 @@ msgid "ERROR"
msgstr "" msgstr ""
#: internal/utils/cmd.go:74 #: internal/utils/cmd.go:74
msgid "You need to be root" msgid "You need to be root to perform this action"
msgstr "" msgstr ""
#: list.go:41 #: list.go:41
@ -435,31 +435,31 @@ msgstr ""
msgid "URL of the new repo" msgid "URL of the new repo"
msgstr "" msgstr ""
#: repo.go:89 repo.go:166 #: repo.go:92 repo.go:172
msgid "Error saving config" msgid "Error saving config"
msgstr "" msgstr ""
#: repo.go:119 #: repo.go:122
msgid "Remove an existing repository" msgid "Remove an existing repository"
msgstr "" msgstr ""
#: repo.go:126 #: repo.go:129
msgid "Name of the repo to be deleted" msgid "Name of the repo to be deleted"
msgstr "" msgstr ""
#: repo.go:152 #: repo.go:158
msgid "Repo does not exist" msgid "Repo does not exist"
msgstr "" msgstr ""
#: repo.go:160 #: repo.go:166
msgid "Error removing repo directory" msgid "Error removing repo directory"
msgstr "" msgstr ""
#: repo.go:177 #: repo.go:183
msgid "Error removing packages from database" msgid "Error removing packages from database"
msgstr "" msgstr ""
#: repo.go:189 #: repo.go:195
msgid "Pull all repositories that have changed" msgid "Pull all repositories that have changed"
msgstr "" msgstr ""

@ -84,42 +84,43 @@ msgstr "Ошибка при перемещении пакета"
msgid "Attempt to fix problems with ALR" msgid "Attempt to fix problems with ALR"
msgstr "Попытка устранить проблемы с ALR" msgstr "Попытка устранить проблемы с ALR"
#: fix.go:42 #: fix.go:43
msgid "Can't drop privileges" msgid "Can't drop privileges"
msgstr "" msgstr ""
#: fix.go:56 #: fix.go:57
msgid "Removing cache directory" #, fuzzy
msgid "Clearing cache directory"
msgstr "Удаление каталога кэша" msgstr "Удаление каталога кэша"
#: fix.go:60 #: fix.go:62
#, fuzzy #, fuzzy
msgid "Unable to open cache directory" msgid "Unable to open cache directory"
msgstr "Не удалось удалить каталог кэша" msgstr "Не удалось удалить каталог кэша"
#: fix.go:67 #: fix.go:69
#, fuzzy #, fuzzy
msgid "Unable to read cache directory contents" msgid "Unable to read cache directory contents"
msgstr "Не удалось удалить каталог кэша" msgstr "Не удалось удалить каталог кэша"
#: fix.go:74 #: fix.go:76
#, fuzzy #, fuzzy
msgid "Unable to remove cache item" msgid "Unable to remove cache item"
msgstr "Не удалось удалить каталог кэша" msgstr "Не удалось удалить каталог кэша"
#: fix.go:79 #: fix.go:81
msgid "Rebuilding cache" msgid "Rebuilding cache"
msgstr "Восстановление кэша" msgstr "Восстановление кэша"
#: fix.go:83 #: fix.go:85
msgid "Unable to create new cache directory" msgid "Unable to create new cache directory"
msgstr "Не удалось создать новый каталог кэша" msgstr "Не удалось создать новый каталог кэша"
#: fix.go:103 #: fix.go:105
msgid "Error pulling repos" msgid "Error pulling repos"
msgstr "Ошибка при извлечении репозиториев" msgstr "Ошибка при извлечении репозиториев"
#: fix.go:107 #: fix.go:109
msgid "Done" msgid "Done"
msgstr "Сделано" msgstr "Сделано"
@ -330,7 +331,7 @@ msgid "ERROR"
msgstr "ОШИБКА" msgstr "ОШИБКА"
#: internal/utils/cmd.go:74 #: internal/utils/cmd.go:74
msgid "You need to be root" msgid "You need to be root to perform this action"
msgstr "" msgstr ""
#: list.go:41 #: list.go:41
@ -455,32 +456,32 @@ msgstr "Название нового репозитория"
msgid "URL of the new repo" msgid "URL of the new repo"
msgstr "URL-адрес нового репозитория" msgstr "URL-адрес нового репозитория"
#: repo.go:89 repo.go:166 #: repo.go:92 repo.go:172
#, fuzzy #, fuzzy
msgid "Error saving config" msgid "Error saving config"
msgstr "Ошибка при кодировании конфигурации" msgstr "Ошибка при кодировании конфигурации"
#: repo.go:119 #: repo.go:122
msgid "Remove an existing repository" msgid "Remove an existing repository"
msgstr "Удалить существующий репозиторий" msgstr "Удалить существующий репозиторий"
#: repo.go:126 #: repo.go:129
msgid "Name of the repo to be deleted" msgid "Name of the repo to be deleted"
msgstr "Название репозитория удалён" msgstr "Название репозитория удалён"
#: repo.go:152 #: repo.go:158
msgid "Repo does not exist" msgid "Repo does not exist"
msgstr "Репозитория не существует" msgstr "Репозитория не существует"
#: repo.go:160 #: repo.go:166
msgid "Error removing repo directory" msgid "Error removing repo directory"
msgstr "Ошибка при удалении каталога репозитория" msgstr "Ошибка при удалении каталога репозитория"
#: repo.go:177 #: repo.go:183
msgid "Error removing packages from database" msgid "Error removing packages from database"
msgstr "Ошибка при удалении пакетов из базы данных" msgstr "Ошибка при удалении пакетов из базы данных"
#: repo.go:189 #: repo.go:195
msgid "Pull all repositories that have changed" msgid "Pull all repositories that have changed"
msgstr "Скачать все изменённые репозитории" msgstr "Скачать все изменённые репозитории"

@ -17,13 +17,13 @@
package utils package utils
import ( import (
"log/slog"
"os" "os"
"os/user" "os/user"
"strconv" "strconv"
"syscall" "syscall"
"github.com/leonelquinteros/gotext" "github.com/leonelquinteros/gotext"
"github.com/urfave/cli/v2"
) )
func GetUidGidAlrUserString() (string, string, error) { func GetUidGidAlrUserString() (string, string, error) {
@ -69,9 +69,9 @@ func DropCapsToAlrUser() error {
return nil return nil
} }
func ExitIfNotRoot() { func ExitIfNotRoot() error {
if os.Getuid() != 0 { if os.Getuid() != 0 {
slog.Error(gotext.Get("You need to be root")) return cli.Exit(gotext.Get("You need to be root to perform this action"), 1)
os.Exit(1)
} }
return nil
} }

14
repo.go

@ -55,7 +55,10 @@ func AddRepoCmd() *cli.Command {
}, },
}, },
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
utils.ExitIfNotRoot() err := utils.ExitIfNotRoot()
if err != nil {
return err
}
ctx := c.Context ctx := c.Context
@ -63,7 +66,7 @@ func AddRepoCmd() *cli.Command {
repoURL := c.String("url") repoURL := c.String("url")
cfg := config.New() cfg := config.New()
err := cfg.Load() err = cfg.Load()
if err != nil { if err != nil {
slog.Error(gotext.Get("Error loading config"), "err", err) slog.Error(gotext.Get("Error loading config"), "err", err)
os.Exit(1) os.Exit(1)
@ -127,13 +130,16 @@ func RemoveRepoCmd() *cli.Command {
}, },
}, },
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
utils.ExitIfNotRoot() err := utils.ExitIfNotRoot()
if err != nil {
return err
}
ctx := c.Context ctx := c.Context
name := c.String("name") name := c.String("name")
cfg := config.New() cfg := config.New()
err := cfg.Load() err = cfg.Load()
if err != nil { if err != nil {
slog.Error(gotext.Get("Error loading config"), "err", err) slog.Error(gotext.Get("Error loading config"), "err", err)
os.Exit(1) os.Exit(1)