diff --git a/assets/coverage-badge.svg b/assets/coverage-badge.svg index 5a9d2fc..51c5462 100644 --- a/assets/coverage-badge.svg +++ b/assets/coverage-badge.svg @@ -11,7 +11,7 @@ coverage coverage - 15.8% - 15.8% + 16.1% + 16.1% diff --git a/build.go b/build.go index 1c284a8..d91ffc0 100644 --- a/build.go +++ b/build.go @@ -30,6 +30,7 @@ import ( "github.com/leonelquinteros/gotext" "github.com/urfave/cli/v2" + "gitea.plemya-x.ru/Plemya-x/ALR/internal/cliutils" "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/osutils" @@ -71,13 +72,11 @@ func BuildCmd() *cli.Command { Action: func(c *cli.Context) error { wd, err := os.Getwd() if err != nil { - slog.Error(gotext.Get("Error getting working directory"), "err", err) - os.Exit(1) + return cliutils.FormatCliExit(gotext.Get("Error getting working directory"), err) } executable, err := os.Executable() if err != nil { - slog.Error(gotext.Get("Error getting working directory"), "err", err) - os.Exit(1) + return cliutils.FormatCliExit(gotext.Get("Error getting working directory"), err) } cmd := exec.Command(executable, "_internal-mount", wd) @@ -86,8 +85,7 @@ func BuildCmd() *cli.Command { cmd.Stderr = os.Stderr err = cmd.Run() if err != nil { - slog.Error(gotext.Get("Error getting working directory"), "err", err) - os.Exit(1) + return cliutils.FormatCliExit(gotext.Get("Error getting working directory"), err) } wd = stdout.String() @@ -99,37 +97,29 @@ func BuildCmd() *cli.Command { cmd.Stdout = &stdout cmd.Stderr = os.Stderr err = cmd.Run() - if err != nil { - slog.Error(gotext.Get("Error getting working directory"), "err", err) - os.Exit(1) - } }() err = utils.DropCapsToAlrUser() if err != nil { - slog.Error(gotext.Get("Error dropping capabilities"), "err", err) - os.Exit(1) + return cliutils.FormatCliExit(gotext.Get("Error dropping capabilities"), err) } _, err = os.Stat(wd) if err != nil { - slog.Error(gotext.Get("Error dropping capabilities"), "err", err) - os.Exit(1) + return cliutils.FormatCliExit(gotext.Get("Error dropping capabilities"), err) } ctx := c.Context cfg := config.New() err = cfg.Load() if err != nil { - slog.Error(gotext.Get("Error loading config"), "err", err) - os.Exit(1) + return cliutils.FormatCliExit(gotext.Get("Error loading config"), err) } db := database.New(cfg) rs := repos.New(cfg, db) err = db.Init(ctx) if err != nil { - slog.Error(gotext.Get("Error initialization database"), "err", err) - os.Exit(1) + return cliutils.FormatCliExit(gotext.Get("Error initialization database"), err) } var script string @@ -144,8 +134,7 @@ func BuildCmd() *cli.Command { info, err := distro.ParseOSRelease(ctx) if err != nil { - slog.Error(gotext.Get("Error parsing os release"), "err", err) - os.Exit(1) + return cliutils.FormatCliExit(gotext.Get("Error parsing os release"), err) } builder := build.NewMainBuilder( @@ -176,8 +165,7 @@ func BuildCmd() *cli.Command { }, ) if err != nil { - slog.Error(gotext.Get("Error building package"), "err", err) - os.Exit(1) + return cliutils.FormatCliExit(gotext.Get("Error building package"), err) } case c.IsSet("package"): // TODO: handle multiple packages @@ -218,8 +206,7 @@ func BuildCmd() *cli.Command { }, ) if err != nil { - slog.Error(gotext.Get("Error building package"), "err", err) - os.Exit(1) + return cliutils.FormatCliExit(gotext.Get("Error building package"), err) } default: slog.Error(gotext.Get("Nothing to build")) @@ -231,8 +218,7 @@ func BuildCmd() *cli.Command { name := filepath.Base(pkgPath) err = osutils.Move(pkgPath, filepath.Join(wd, name)) if err != nil { - slog.Error(gotext.Get("Error moving the package"), "err", err) - os.Exit(1) + return cliutils.FormatCliExit(gotext.Get("Error moving the package"), err) } } diff --git a/fix.go b/fix.go index ed08ad7..05cd2ae 100644 --- a/fix.go +++ b/fix.go @@ -27,6 +27,7 @@ import ( "github.com/leonelquinteros/gotext" "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/utils" ) @@ -60,22 +61,19 @@ func FixCmd() *cli.Command { dir, err := os.Open(paths.CacheDir) if err != nil { - slog.Error(gotext.Get("Unable to open cache directory")) - return cli.Exit(err, 1) + return cliutils.FormatCliExit(gotext.Get("Unable to open cache directory"), err) } defer dir.Close() entries, err := dir.Readdirnames(-1) if err != nil { - slog.Error(gotext.Get("Unable to read cache directory contents")) - return cli.Exit(err, 1) + return cliutils.FormatCliExit(gotext.Get("Unable to read cache directory contents"), err) } 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) - return cli.Exit(err, 1) + return cliutils.FormatCliExit(gotext.Get("Unable to remove cache item (%s)", entry), err) } } @@ -83,15 +81,14 @@ func FixCmd() *cli.Command { err = os.MkdirAll(paths.CacheDir, 0o755) if err != nil { - slog.Error(gotext.Get("Unable to create new cache directory")) - return cli.Exit(err, 1) + return cliutils.FormatCliExit(gotext.Get("Unable to create new cache directory"), err) } deps, err = appbuilder. New(ctx). WithConfig(). WithDB(). - WithRepos(). + WithReposForcePull(). Build() if err != nil { return cli.Exit(err, 1) diff --git a/helper.go b/helper.go index 19eaf39..f87cc88 100644 --- a/helper.go +++ b/helper.go @@ -30,6 +30,7 @@ import ( "mvdan.cc/sh/v3/expand" "mvdan.cc/sh/v3/interp" + "gitea.plemya-x.ru/Plemya-x/ALR/internal/cliutils" "gitea.plemya-x.ru/Plemya-x/ALR/internal/cpu" "gitea.plemya-x.ru/Plemya-x/ALR/internal/shutils/helpers" "gitea.plemya-x.ru/Plemya-x/ALR/pkg/distro" @@ -76,14 +77,12 @@ func HelperCmd() *cli.Command { wd, err := os.Getwd() if err != nil { - slog.Error(gotext.Get("Error getting working directory")) - return cli.Exit(err, 1) + return cliutils.FormatCliExit(gotext.Get("Error getting working directory"), err) } info, err := distro.ParseOSRelease(ctx) if err != nil { - slog.Error(gotext.Get("Error parsing os-release file")) - return cli.Exit(err, 1) + return cliutils.FormatCliExit(gotext.Get("Error parsing os-release file"), err) } hc := interp.HandlerContext{ diff --git a/info.go b/info.go index a2555be..ea17b9b 100644 --- a/info.go +++ b/info.go @@ -21,7 +21,6 @@ package main import ( "fmt" - "log/slog" "os" "github.com/jeandeaual/go-locale" @@ -66,8 +65,7 @@ func InfoCmd() *cli.Command { result, err := deps.DB.GetPkgs(c.Context, "true") if err != nil { - slog.Error(gotext.Get("Error getting packages")) - return cli.Exit(err, 1) + return cliutils.FormatCliExit(gotext.Get("Error getting packages"), err) } defer result.Close() @@ -75,8 +73,7 @@ func InfoCmd() *cli.Command { var pkg database.Package err = result.StructScan(&pkg) if err != nil { - slog.Error(gotext.Get("Error iterating over packages")) - return cli.Exit(err, 1) + return cliutils.FormatCliExit(gotext.Get("Error iterating over packages"), err) } fmt.Println(pkg.Name) @@ -110,13 +107,11 @@ func InfoCmd() *cli.Command { found, _, err := rs.FindPkgs(ctx, args.Slice()) if err != nil { - slog.Error(gotext.Get("Error finding packages")) - return cli.Exit(err, 1) + return cliutils.FormatCliExit(gotext.Get("Error finding packages"), err) } if len(found) == 0 { - slog.Error(gotext.Get("Package not found")) - return cli.Exit(err, 1) + return cliutils.FormatCliExit(gotext.Get("Package not found"), err) } pkgs := cliutils.FlattenPkgs(ctx, found, "show", c.Bool("interactive")) @@ -126,8 +121,7 @@ func InfoCmd() *cli.Command { systemLang, err := locale.GetLanguage() if err != nil { - slog.Error(gotext.Get("Can't detect system language")) - return cli.Exit(err, 1) + return cliutils.FormatCliExit(gotext.Get("Can't detect system language"), err) } if systemLang == "" { systemLang = "en" @@ -136,8 +130,7 @@ func InfoCmd() *cli.Command { if !all { info, err := distro.ParseOSRelease(ctx) if err != nil { - slog.Error(gotext.Get("Error parsing os-release file")) - return cli.Exit(err, 1) + return cliutils.FormatCliExit(gotext.Get("Error parsing os-release file"), err) } names, err = overrides.Resolve( info, @@ -145,8 +138,7 @@ func InfoCmd() *cli.Command { WithLanguages([]string{systemLang}), ) if err != nil { - slog.Error(gotext.Get("Error resolving overrides")) - return cli.Exit(err, 1) + return cliutils.FormatCliExit(gotext.Get("Error resolving overrides"), err) } } @@ -154,14 +146,12 @@ func InfoCmd() *cli.Command { if !all { err = yaml.NewEncoder(os.Stdout).Encode(overrides.ResolvePackage(&pkg, names)) if err != nil { - slog.Error(gotext.Get("Error encoding script variables")) - return cli.Exit(err, 1) + return cliutils.FormatCliExit(gotext.Get("Error encoding script variables"), err) } } else { err = yaml.NewEncoder(os.Stdout).Encode(pkg) if err != nil { - slog.Error(gotext.Get("Error encoding script variables")) - return cli.Exit(err, 1) + return cliutils.FormatCliExit(gotext.Get("Error encoding script variables"), err) } } diff --git a/install.go b/install.go index a839be5..cec2370 100644 --- a/install.go +++ b/install.go @@ -27,6 +27,7 @@ import ( "github.com/leonelquinteros/gotext" "github.com/urfave/cli/v2" + "gitea.plemya-x.ru/Plemya-x/ALR/internal/cliutils" "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" @@ -67,22 +68,19 @@ func InstallCmd() *cli.Command { cfg := config.New() err := cfg.Load() if err != nil { - slog.Error(gotext.Get("Error loading config"), "err", err) - os.Exit(1) + return cliutils.FormatCliExit(gotext.Get("Error loading config"), err) } db := database.New(cfg) rs := repos.New(cfg, db) err = db.Init(ctx) if err != nil { - slog.Error(gotext.Get("Error initialization database"), "err", err) - os.Exit(1) + return cliutils.FormatCliExit(gotext.Get("Error initialization database"), err) } err = utils.DropCapsToAlrUser() if err != nil { - slog.Error(gotext.Get("Error dropping capabilities"), "err", err) - os.Exit(1) + return cliutils.FormatCliExit(gotext.Get("Error dropping capabilities"), err) } builder := build.NewMainBuilder( @@ -93,15 +91,13 @@ func InstallCmd() *cli.Command { if cfg.AutoPull() { err := rs.Pull(ctx, cfg.Repos()) if err != nil { - slog.Error(gotext.Get("Error pulling repositories"), "err", err) - os.Exit(1) + return cliutils.FormatCliExit(gotext.Get("Error pulling repositories"), err) } } info, err := distro.ParseOSRelease(ctx) if err != nil { - slog.Error(gotext.Get("Error parsing os release"), "err", err) - os.Exit(1) + return cliutils.FormatCliExit(gotext.Get("Error parsing os release"), err) } err = builder.InstallPkgs( @@ -117,30 +113,26 @@ func InstallCmd() *cli.Command { args.Slice(), ) if err != nil { - slog.Error(gotext.Get("Error parsing os release"), "err", err) - os.Exit(1) + return cliutils.FormatCliExit(gotext.Get("Error parsing os release"), err) } return nil }, - BashComplete: func(c *cli.Context) { + BashComplete: cliutils.BashCompleteWithError(func(c *cli.Context) error { cfg := config.New() err := cfg.Load() if err != nil { - slog.Error(gotext.Get("Error loading config"), "err", err) - os.Exit(1) + return cliutils.FormatCliExit(gotext.Get("Error loading config"), err) } db := database.New(cfg) err = db.Init(c.Context) if err != nil { - slog.Error(gotext.Get("Error initialization database"), "err", err) - os.Exit(1) + return cliutils.FormatCliExit(gotext.Get("Error initialization database"), err) } result, err := db.GetPkgs(c.Context, "true") if err != nil { - slog.Error(gotext.Get("Error getting packages"), "err", err) - os.Exit(1) + return cliutils.FormatCliExit(gotext.Get("Error getting packages"), err) } defer result.Close() @@ -148,13 +140,14 @@ func InstallCmd() *cli.Command { var pkg database.Package err = result.StructScan(&pkg) if err != nil { - slog.Error(gotext.Get("Error iterating over packages"), "err", err) - os.Exit(1) + return cliutils.FormatCliExit(gotext.Get("Error iterating over packages"), err) } fmt.Println(pkg.Name) } - }, + + return nil + }), } } @@ -163,19 +156,17 @@ func RemoveCmd() *cli.Command { Name: "remove", Usage: gotext.Get("Remove an installed package"), Aliases: []string{"rm"}, - BashComplete: func(c *cli.Context) { + BashComplete: cliutils.BashCompleteWithError(func(c *cli.Context) error { cfg := config.New() err := cfg.Load() if err != nil { - slog.Error(gotext.Get("Error loading config"), "err", err) - os.Exit(1) + return cliutils.FormatCliExit(gotext.Get("Error loading config"), err) } db := database.New(cfg) err = db.Init(c.Context) if err != nil { - slog.Error(gotext.Get("Error initialization database"), "err", err) - os.Exit(1) + return cliutils.FormatCliExit(gotext.Get("Error initialization database"), err) } installedAlrPackages := map[string]string{} @@ -186,8 +177,7 @@ func RemoveCmd() *cli.Command { } installed, err := mgr.ListInstalled(&manager.Opts{AsRoot: false}) if err != nil { - slog.Error(gotext.Get("Error listing installed packages"), "err", err) - os.Exit(1) + return cliutils.FormatCliExit(gotext.Get("Error listing installed packages"), err) } for pkgName, version := range installed { matches := build.RegexpALRPackageName.FindStringSubmatch(pkgName) @@ -200,8 +190,7 @@ func RemoveCmd() *cli.Command { result, err := db.GetPkgs(c.Context, "true") if err != nil { - slog.Error(gotext.Get("Error getting packages"), "err", err) - os.Exit(1) + return cliutils.FormatCliExit(gotext.Get("Error getting packages"), err) } defer result.Close() @@ -209,8 +198,7 @@ func RemoveCmd() *cli.Command { var pkg database.Package err = result.StructScan(&pkg) if err != nil { - slog.Error(gotext.Get("Error iterating over packages"), "err", err) - os.Exit(1) + return cliutils.FormatCliExit(gotext.Get("Error iterating over packages"), err) } _, ok := installedAlrPackages[fmt.Sprintf("%s/%s", pkg.Repository, pkg.Name)] @@ -220,7 +208,9 @@ func RemoveCmd() *cli.Command { fmt.Println(pkg.Name) } - }, + + return nil + }), Action: func(c *cli.Context) error { args := c.Args() if args.Len() < 1 { @@ -239,8 +229,7 @@ func RemoveCmd() *cli.Command { NoConfirm: !c.Bool("interactive"), }, c.Args().Slice()...) if err != nil { - slog.Error(gotext.Get("Error removing packages"), "err", err) - os.Exit(1) + return cliutils.FormatCliExit(gotext.Get("Error removing packages"), err) } return nil diff --git a/internal.go b/internal.go index 4adecdf..8282be1 100644 --- a/internal.go +++ b/internal.go @@ -31,6 +31,7 @@ import ( "github.com/leonelquinteros/gotext" "github.com/urfave/cli/v2" + "gitea.plemya-x.ru/Plemya-x/ALR/internal/cliutils" "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/logger" @@ -55,8 +56,7 @@ func InternalBuildCmd() *cli.Command { cfg := config.New() err = cfg.Load() if err != nil { - slog.Error(gotext.Get("Error loading config"), "err", err) - os.Exit(1) + return cliutils.FormatCliExit(gotext.Get("Error loading config"), err) } logger := hclog.New(&hclog.LoggerOptions{ @@ -96,16 +96,14 @@ func InternalInstallCmd() *cli.Command { cfg := config.New() err = cfg.Load() if err != nil { - slog.Error(gotext.Get("Error loading config"), "err", err) - os.Exit(1) + return cliutils.FormatCliExit(gotext.Get("Error loading config"), err) } db := database.New(cfg) rs := repos.New(cfg, db) err = db.Init(c.Context) if err != nil { - slog.Error(gotext.Get("Error initialization database"), "err", err) - os.Exit(1) + return cliutils.FormatCliExit(gotext.Get("Error initialization database"), err) } logger := hclog.New(&hclog.LoggerOptions{ diff --git a/internal/cliutils/utils.go b/internal/cliutils/utils.go index fca0c8d..8bcacaa 100644 --- a/internal/cliutils/utils.go +++ b/internal/cliutils/utils.go @@ -46,3 +46,11 @@ func HandleExitCoder(err error) { return } } + +func FormatCliExit(msg string, err error) cli.ExitCoder { + return FormatCliExitWithCode(msg, err, 1) +} + +func FormatCliExitWithCode(msg string, err error, exitCode int) cli.ExitCoder { + return cli.Exit(fmt.Errorf("%s: %w", msg, err), exitCode) +} diff --git a/internal/translations/default.pot b/internal/translations/default.pot index ee8b45c..0b7281f 100644 --- a/internal/translations/default.pot +++ b/internal/translations/default.pot @@ -9,76 +9,76 @@ msgstr "" "Content-Transfer-Encoding: 8bit\n" "Plural-Forms: nplurals=2; plural=(n != 1);\n" -#: build.go:47 +#: build.go:48 msgid "Build a local package" msgstr "" -#: build.go:53 +#: build.go:54 msgid "Path to the build script" msgstr "" -#: build.go:58 +#: build.go:59 msgid "Specify subpackage in script (for multi package script only)" msgstr "" -#: build.go:63 +#: build.go:64 msgid "Name of the package to build and its repo (example: default/go-bin)" msgstr "" -#: build.go:68 +#: build.go:69 msgid "" "Build package from scratch even if there's an already built package available" msgstr "" -#: build.go:74 build.go:79 build.go:89 build.go:103 +#: build.go:75 build.go:79 build.go:88 msgid "Error getting working directory" msgstr "" -#: build.go:110 build.go:115 +#: build.go:104 build.go:108 msgid "Error dropping capabilities" msgstr "" -#: build.go:123 +#: build.go:115 msgid "Error loading config" msgstr "" -#: build.go:131 +#: build.go:122 msgid "Error initialization database" msgstr "" -#: build.go:141 +#: build.go:131 msgid "Unable to detect a supported package manager on the system" msgstr "" -#: build.go:147 +#: build.go:137 msgid "Error parsing os release" msgstr "" -#: build.go:179 build.go:221 +#: build.go:168 build.go:209 msgid "Error building package" msgstr "" -#: build.go:197 +#: build.go:185 msgid "Package not found" msgstr "" -#: build.go:225 +#: build.go:212 msgid "Nothing to build" msgstr "" -#: build.go:234 +#: build.go:221 msgid "Error moving the package" msgstr "" -#: fix.go:37 +#: fix.go:38 msgid "Attempt to fix problems with ALR" msgstr "" -#: fix.go:58 +#: fix.go:59 msgid "Clearing cache directory" msgstr "" -#: fix.go:63 +#: fix.go:64 msgid "Unable to open cache directory" msgstr "" @@ -86,19 +86,19 @@ msgstr "" msgid "Unable to read cache directory contents" msgstr "" -#: fix.go:77 -msgid "Unable to remove cache item" +#: fix.go:76 +msgid "Unable to remove cache item (%s)" msgstr "" -#: fix.go:82 +#: fix.go:80 msgid "Rebuilding cache" msgstr "" -#: fix.go:86 +#: fix.go:84 msgid "Unable to create new cache directory" msgstr "" -#: fix.go:101 +#: fix.go:98 msgid "Done" msgstr "" @@ -110,19 +110,19 @@ msgstr "" msgid "Generate a ALR script for a pip module" msgstr "" -#: helper.go:41 +#: helper.go:42 msgid "List all the available helper commands" msgstr "" -#: helper.go:53 +#: helper.go:54 msgid "Run a ALR helper command" msgstr "" -#: helper.go:60 +#: helper.go:61 msgid "The directory that the install commands will install to" msgstr "" -#: helper.go:73 helper.go:74 +#: helper.go:74 helper.go:75 msgid "No such helper command" msgstr "" @@ -130,67 +130,67 @@ msgstr "" msgid "Error parsing os-release file" msgstr "" -#: info.go:43 +#: info.go:42 msgid "Print information about a package" msgstr "" -#: info.go:48 +#: info.go:47 msgid "Show all information, not just for the current distro" msgstr "" -#: info.go:69 +#: info.go:68 msgid "Error getting packages" msgstr "" -#: info.go:78 +#: info.go:76 msgid "Error iterating over packages" msgstr "" -#: info.go:93 +#: info.go:90 msgid "Command info expected at least 1 argument, got %d" msgstr "" -#: info.go:113 +#: info.go:110 msgid "Error finding packages" msgstr "" -#: info.go:129 +#: info.go:124 msgid "Can't detect system language" msgstr "" -#: info.go:148 +#: info.go:141 msgid "Error resolving overrides" msgstr "" -#: info.go:157 info.go:163 +#: info.go:149 info.go:154 msgid "Error encoding script variables" msgstr "" -#: install.go:43 +#: install.go:44 msgid "Install a new package" msgstr "" -#: install.go:57 +#: install.go:58 msgid "Command install expected at least 1 argument, got %d" msgstr "" -#: install.go:96 +#: install.go:94 msgid "Error pulling repositories" msgstr "" -#: install.go:164 +#: install.go:157 msgid "Remove an installed package" msgstr "" -#: install.go:189 +#: install.go:180 msgid "Error listing installed packages" msgstr "" -#: install.go:227 +#: install.go:217 msgid "Command remove expected at least 1 argument, got %d" msgstr "" -#: install.go:242 +#: install.go:232 msgid "Error removing packages" msgstr "" @@ -315,7 +315,7 @@ msgstr "" msgid "You need to be root to perform this action" msgstr "" -#: list.go:40 +#: list.go:41 msgid "List ALR repo packages" msgstr "" @@ -419,51 +419,51 @@ msgid "" "updating ALR if something doesn't work." msgstr "" -#: repo.go:42 +#: repo.go:43 msgid "Add a new repository" msgstr "" -#: repo.go:49 +#: repo.go:50 msgid "Name of the new repo" msgstr "" -#: repo.go:55 +#: repo.go:56 msgid "URL of the new repo" msgstr "" -#: repo.go:93 repo.go:173 +#: repo.go:93 repo.go:169 msgid "Error saving config" msgstr "" -#: repo.go:98 +#: repo.go:97 msgid "Can't drop privileges" msgstr "" -#: repo.go:105 repo.go:111 +#: repo.go:104 repo.go:110 msgid "Error pulling repos" msgstr "" -#: repo.go:123 +#: repo.go:121 msgid "Remove an existing repository" msgstr "" -#: repo.go:130 +#: repo.go:128 msgid "Name of the repo to be deleted" msgstr "" -#: repo.go:159 +#: repo.go:156 msgid "Repo does not exist" msgstr "" -#: repo.go:167 +#: repo.go:164 msgid "Error removing repo directory" msgstr "" -#: repo.go:184 +#: repo.go:179 msgid "Error removing packages from database" msgstr "" -#: repo.go:196 +#: repo.go:190 msgid "Pull all repositories that have changed" msgstr "" @@ -495,22 +495,22 @@ msgstr "" msgid "Error while executing search" msgstr "" -#: search.go:105 +#: search.go:104 msgid "Error parsing format template" msgstr "" -#: search.go:114 +#: search.go:112 msgid "Error executing template" msgstr "" -#: upgrade.go:48 +#: upgrade.go:49 msgid "Upgrade all installed packages" msgstr "" -#: upgrade.go:111 upgrade.go:129 +#: upgrade.go:107 upgrade.go:124 msgid "Error checking for updates" msgstr "" -#: upgrade.go:133 +#: upgrade.go:127 msgid "There is nothing to do." msgstr "" diff --git a/internal/translations/po/ru/default.po b/internal/translations/po/ru/default.po index e7deda5..2beca04 100644 --- a/internal/translations/po/ru/default.po +++ b/internal/translations/po/ru/default.po @@ -16,80 +16,80 @@ msgstr "" "%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);\n" "X-Generator: Gtranslator 47.1\n" -#: build.go:47 +#: build.go:48 msgid "Build a local package" msgstr "Сборка локального пакета" -#: build.go:53 +#: build.go:54 msgid "Path to the build script" msgstr "Путь к скрипту сборки" -#: build.go:58 +#: build.go:59 msgid "Specify subpackage in script (for multi package script only)" msgstr "Укажите подпакет в скрипте (только для многопакетного скрипта)" -#: build.go:63 +#: build.go:64 msgid "Name of the package to build and its repo (example: default/go-bin)" msgstr "Имя пакета для сборки и его репозиторий (пример: default/go-bin)" -#: build.go:68 +#: build.go:69 msgid "" "Build package from scratch even if there's an already built package available" msgstr "Создайте пакет с нуля, даже если уже имеется готовый пакет" -#: build.go:74 build.go:79 build.go:89 build.go:103 +#: build.go:75 build.go:79 build.go:88 msgid "Error getting working directory" msgstr "Ошибка при получении рабочего каталога" -#: build.go:110 build.go:115 +#: build.go:104 build.go:108 #, fuzzy msgid "Error dropping capabilities" msgstr "Ошибка при открытии базы данных" -#: build.go:123 +#: build.go:115 #, fuzzy msgid "Error loading config" msgstr "Ошибка при кодировании конфигурации" -#: build.go:131 +#: build.go:122 msgid "Error initialization database" msgstr "Ошибка инициализации базы данных" -#: build.go:141 +#: build.go:131 msgid "Unable to detect a supported package manager on the system" msgstr "Не удалось обнаружить поддерживаемый менеджер пакетов в системе" -#: build.go:147 +#: build.go:137 msgid "Error parsing os release" msgstr "Ошибка при разборе файла выпуска операционной системы" -#: build.go:179 build.go:221 +#: build.go:168 build.go:209 msgid "Error building package" msgstr "Ошибка при сборке пакета" -#: build.go:197 +#: build.go:185 msgid "Package not found" msgstr "Пакет не найден" -#: build.go:225 +#: build.go:212 #, fuzzy msgid "Nothing to build" msgstr "Исполнение build()" -#: build.go:234 +#: build.go:221 msgid "Error moving the package" msgstr "Ошибка при перемещении пакета" -#: fix.go:37 +#: fix.go:38 msgid "Attempt to fix problems with ALR" msgstr "Попытка устранить проблемы с ALR" -#: fix.go:58 +#: fix.go:59 #, fuzzy msgid "Clearing cache directory" msgstr "Удаление каталога кэша" -#: fix.go:63 +#: fix.go:64 #, fuzzy msgid "Unable to open cache directory" msgstr "Не удалось удалить каталог кэша" @@ -99,20 +99,20 @@ msgstr "Не удалось удалить каталог кэша" msgid "Unable to read cache directory contents" msgstr "Не удалось удалить каталог кэша" -#: fix.go:77 +#: fix.go:76 #, fuzzy -msgid "Unable to remove cache item" +msgid "Unable to remove cache item (%s)" msgstr "Не удалось удалить каталог кэша" -#: fix.go:82 +#: fix.go:80 msgid "Rebuilding cache" msgstr "Восстановление кэша" -#: fix.go:86 +#: fix.go:84 msgid "Unable to create new cache directory" msgstr "Не удалось создать новый каталог кэша" -#: fix.go:101 +#: fix.go:98 msgid "Done" msgstr "Сделано" @@ -124,19 +124,19 @@ msgstr "Генерация скрипта ALR из шаблона" msgid "Generate a ALR script for a pip module" msgstr "Генерация скрипта ALR для модуля pip" -#: helper.go:41 +#: helper.go:42 msgid "List all the available helper commands" msgstr "Список всех доступных вспомогательных команды" -#: helper.go:53 +#: helper.go:54 msgid "Run a ALR helper command" msgstr "Запустить вспомогательную команду ALR" -#: helper.go:60 +#: helper.go:61 msgid "The directory that the install commands will install to" msgstr "Каталог, в который будут устанавливать команды установки" -#: helper.go:73 helper.go:74 +#: helper.go:74 helper.go:75 msgid "No such helper command" msgstr "Такой вспомогательной команды нет" @@ -144,68 +144,68 @@ msgstr "Такой вспомогательной команды нет" msgid "Error parsing os-release file" msgstr "Ошибка при разборе файла выпуска операционной системы" -#: info.go:43 +#: info.go:42 msgid "Print information about a package" msgstr "Отобразить информацию о пакете" -#: info.go:48 +#: info.go:47 msgid "Show all information, not just for the current distro" msgstr "Показывать всю информацию, не только для текущего дистрибутива" -#: info.go:69 +#: info.go:68 msgid "Error getting packages" msgstr "Ошибка при получении пакетов" -#: info.go:78 +#: info.go:76 msgid "Error iterating over packages" msgstr "Ошибка при переборе пакетов" -#: info.go:93 +#: info.go:90 msgid "Command info expected at least 1 argument, got %d" msgstr "Для команды info ожидался хотя бы 1 аргумент, получено %d" -#: info.go:113 +#: info.go:110 msgid "Error finding packages" msgstr "Ошибка при поиске пакетов" -#: info.go:129 +#: info.go:124 #, fuzzy msgid "Can't detect system language" msgstr "Ошибка при парсинге языка системы" -#: info.go:148 +#: info.go:141 msgid "Error resolving overrides" msgstr "Ошибка устранения переорпеделений" -#: info.go:157 info.go:163 +#: info.go:149 info.go:154 msgid "Error encoding script variables" msgstr "Ошибка кодирования переменных скрита" -#: install.go:43 +#: install.go:44 msgid "Install a new package" msgstr "Установить новый пакет" -#: install.go:57 +#: install.go:58 msgid "Command install expected at least 1 argument, got %d" msgstr "Для команды install ожидался хотя бы 1 аргумент, получено %d" -#: install.go:96 +#: install.go:94 msgid "Error pulling repositories" msgstr "Ошибка при извлечении репозиториев" -#: install.go:164 +#: install.go:157 msgid "Remove an installed package" msgstr "Удалить установленный пакет" -#: install.go:189 +#: install.go:180 msgid "Error listing installed packages" msgstr "Ошибка при составлении списка установленных пакетов" -#: install.go:227 +#: install.go:217 msgid "Command remove expected at least 1 argument, got %d" msgstr "Для команды remove ожидался хотя бы 1 аргумент, получено %d" -#: install.go:242 +#: install.go:232 msgid "Error removing packages" msgstr "Ошибка при удалении пакетов" @@ -331,7 +331,7 @@ msgstr "ОШИБКА" msgid "You need to be root to perform this action" msgstr "" -#: list.go:40 +#: list.go:41 msgid "List ALR repo packages" msgstr "Список пакетов репозитория ALR" @@ -441,52 +441,52 @@ msgstr "" "Минимальная версия ALR для ALR-репозитория выше текущей версии. Попробуйте " "обновить ALR, если что-то не работает." -#: repo.go:42 +#: repo.go:43 msgid "Add a new repository" msgstr "Добавить новый репозиторий" -#: repo.go:49 +#: repo.go:50 msgid "Name of the new repo" msgstr "Название нового репозитория" -#: repo.go:55 +#: repo.go:56 msgid "URL of the new repo" msgstr "URL-адрес нового репозитория" -#: repo.go:93 repo.go:173 +#: repo.go:93 repo.go:169 #, fuzzy msgid "Error saving config" msgstr "Ошибка при кодировании конфигурации" -#: repo.go:98 +#: repo.go:97 msgid "Can't drop privileges" msgstr "" -#: repo.go:105 repo.go:111 +#: repo.go:104 repo.go:110 msgid "Error pulling repos" msgstr "Ошибка при извлечении репозиториев" -#: repo.go:123 +#: repo.go:121 msgid "Remove an existing repository" msgstr "Удалить существующий репозиторий" -#: repo.go:130 +#: repo.go:128 msgid "Name of the repo to be deleted" msgstr "Название репозитория удалён" -#: repo.go:159 +#: repo.go:156 msgid "Repo does not exist" msgstr "Репозитория не существует" -#: repo.go:167 +#: repo.go:164 msgid "Error removing repo directory" msgstr "Ошибка при удалении каталога репозитория" -#: repo.go:184 +#: repo.go:179 msgid "Error removing packages from database" msgstr "Ошибка при удалении пакетов из базы данных" -#: repo.go:196 +#: repo.go:190 msgid "Pull all repositories that have changed" msgstr "Скачать все изменённые репозитории" @@ -519,23 +519,23 @@ msgstr "Формат выходных данных с использование msgid "Error while executing search" msgstr "Ошибка при запуске приложения" -#: search.go:105 +#: search.go:104 msgid "Error parsing format template" msgstr "Ошибка при разборе шаблона" -#: search.go:114 +#: search.go:112 msgid "Error executing template" msgstr "Ошибка при выполнении шаблона" -#: upgrade.go:48 +#: upgrade.go:49 msgid "Upgrade all installed packages" msgstr "Обновить все установленные пакеты" -#: upgrade.go:111 upgrade.go:129 +#: upgrade.go:107 upgrade.go:124 msgid "Error checking for updates" msgstr "Ошибка при проверке обновлений" -#: upgrade.go:133 +#: upgrade.go:127 msgid "There is nothing to do." msgstr "Здесь нечего делать." diff --git a/list.go b/list.go index c92e87b..304464c 100644 --- a/list.go +++ b/list.go @@ -27,6 +27,7 @@ import ( "github.com/urfave/cli/v2" "golang.org/x/exp/slices" + "gitea.plemya-x.ru/Plemya-x/ALR/internal/cliutils" appbuilder "gitea.plemya-x.ru/Plemya-x/ALR/internal/cliutils/app_builder" database "gitea.plemya-x.ru/Plemya-x/ALR/internal/db" "gitea.plemya-x.ru/Plemya-x/ALR/internal/utils" @@ -76,8 +77,7 @@ func ListCmd() *cli.Command { result, err := db.GetPkgs(ctx, where, args...) if err != nil { - slog.Error(gotext.Get("Error getting packages")) - return cli.Exit(err, 1) + return cliutils.FormatCliExit(gotext.Get("Error getting packages"), err) } defer result.Close() diff --git a/repo.go b/repo.go index 2c5428c..da5498c 100644 --- a/repo.go +++ b/repo.go @@ -28,6 +28,7 @@ import ( "github.com/urfave/cli/v2" "golang.org/x/exp/slices" + "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" @@ -69,8 +70,7 @@ func AddRepoCmd() *cli.Command { cfg := config.New() err = cfg.Load() if err != nil { - slog.Error(gotext.Get("Error loading config"), "err", err) - os.Exit(1) + return cliutils.FormatCliExit(gotext.Get("Error loading config"), err) } reposSlice := cfg.Repos() @@ -90,8 +90,7 @@ func AddRepoCmd() *cli.Command { err = cfg.SaveUserConfig() if err != nil { - slog.Error(gotext.Get("Error saving config"), "err", err) - os.Exit(1) + return cliutils.FormatCliExit(gotext.Get("Error saving config"), err) } if utils.DropCapsToAlrUser() != nil { @@ -108,8 +107,7 @@ func AddRepoCmd() *cli.Command { 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) + return cliutils.FormatCliExit(gotext.Get("Error pulling repos"), err) } return nil @@ -142,8 +140,7 @@ func RemoveRepoCmd() *cli.Command { cfg := config.New() err = cfg.Load() if err != nil { - slog.Error(gotext.Get("Error loading config"), "err", err) - os.Exit(1) + return cliutils.FormatCliExit(gotext.Get("Error loading config"), err) } found := false @@ -164,14 +161,12 @@ func RemoveRepoCmd() *cli.Command { err = os.RemoveAll(filepath.Join(cfg.GetPaths().RepoDir, name)) if err != nil { - slog.Error(gotext.Get("Error removing repo directory"), "err", err) - os.Exit(1) + return cliutils.FormatCliExit(gotext.Get("Error removing repo directory"), err) } err = cfg.SaveUserConfig() if err != nil { - slog.Error(gotext.Get("Error saving config"), "err", err) - os.Exit(1) + return cliutils.FormatCliExit(gotext.Get("Error saving config"), err) } db := database.New(cfg) @@ -181,8 +176,7 @@ func RemoveRepoCmd() *cli.Command { } err = db.DeletePkgs(ctx, "repository = ?", name) if err != nil { - slog.Error(gotext.Get("Error removing packages from database"), "err", err) - os.Exit(1) + return cliutils.FormatCliExit(gotext.Get("Error removing packages from database"), err) } return nil diff --git a/search.go b/search.go index dda4757..8724b68 100644 --- a/search.go +++ b/search.go @@ -18,13 +18,13 @@ package main import ( "fmt" - "log/slog" "os" "text/template" "github.com/leonelquinteros/gotext" "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/utils" "gitea.plemya-x.ru/Plemya-x/ALR/pkg/search" @@ -93,8 +93,7 @@ func SearchCmd() *cli.Command { Build(), ) if err != nil { - slog.Error(gotext.Get("Error while executing search")) - return cli.Exit(err, 1) + return cliutils.FormatCliExit(gotext.Get("Error while executing search"), err) } format := c.String("format") @@ -102,8 +101,7 @@ func SearchCmd() *cli.Command { if format != "" { tmpl, err = template.New("format").Parse(format) if err != nil { - slog.Error(gotext.Get("Error parsing format template")) - return cli.Exit(err, 1) + return cliutils.FormatCliExit(gotext.Get("Error parsing format template"), err) } } @@ -111,8 +109,7 @@ func SearchCmd() *cli.Command { if tmpl != nil { err = tmpl.Execute(os.Stdout, dbPkg) if err != nil { - slog.Error(gotext.Get("Error executing template")) - return cli.Exit(err, 1) + return cliutils.FormatCliExit(gotext.Get("Error executing template"), err) } fmt.Println() } else { diff --git a/upgrade.go b/upgrade.go index 57009ef..05254eb 100644 --- a/upgrade.go +++ b/upgrade.go @@ -30,6 +30,7 @@ import ( "go.elara.ws/vercmp" "golang.org/x/exp/maps" + "gitea.plemya-x.ru/Plemya-x/ALR/internal/cliutils" "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/overrides" @@ -57,8 +58,7 @@ func UpgradeCmd() *cli.Command { Action: func(c *cli.Context) error { err := utils.DropCapsToAlrUser() if err != nil { - slog.Error(gotext.Get("Error dropping capabilities"), "err", err) - os.Exit(1) + return cliutils.FormatCliExit(gotext.Get("Error dropping capabilities"), err) } ctx := c.Context @@ -66,16 +66,14 @@ func UpgradeCmd() *cli.Command { cfg := config.New() err = cfg.Load() if err != nil { - slog.Error(gotext.Get("Error loading config"), "err", err) - os.Exit(1) + return cliutils.FormatCliExit(gotext.Get("Error loading config"), err) } db := database.New(cfg) rs := repos.New(cfg, db) err = db.Init(ctx) if err != nil { - slog.Error(gotext.Get("Error initialization database"), "err", err) - os.Exit(1) + return cliutils.FormatCliExit(gotext.Get("Error initialization database"), err) } slog.Debug("builder setup") @@ -87,8 +85,7 @@ func UpgradeCmd() *cli.Command { info, err := distro.ParseOSRelease(ctx) slog.Debug("ParseOSRelease", "err", err) if err != nil { - slog.Error(gotext.Get("Error parsing os-release file"), "err", err) - os.Exit(1) + return cliutils.FormatCliExit(gotext.Get("Error parsing os-release file"), err) } mgr := manager.Detect() @@ -101,15 +98,13 @@ func UpgradeCmd() *cli.Command { slog.Debug("autopull") err = rs.Pull(ctx, cfg.Repos()) if err != nil { - slog.Error(gotext.Get("Error pulling repos"), "err", err) - os.Exit(1) + return cliutils.FormatCliExit(gotext.Get("Error pulling repos"), err) } } updates, err := checkForUpdates(ctx, mgr, cfg, db, rs, info) if err != nil { - slog.Error(gotext.Get("Error checking for updates"), "err", err) - os.Exit(1) + return cliutils.FormatCliExit(gotext.Get("Error checking for updates"), err) } if len(updates) > 0 { @@ -126,8 +121,7 @@ func UpgradeCmd() *cli.Command { updates, ) if err != nil { - slog.Error(gotext.Get("Error checking for updates"), "err", err) - os.Exit(1) + return cliutils.FormatCliExit(gotext.Get("Error checking for updates"), err) } } else { slog.Info(gotext.Get("There is nothing to do."))