command info

This commit is contained in:
2025-04-13 17:49:02 +03:00
parent 3603dc45a4
commit 93d568ec00
5 changed files with 85 additions and 46 deletions

58
info.go
View File

@ -33,6 +33,7 @@ import (
"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"
"gitea.plemya-x.ru/Plemya-x/ALR/internal/utils"
"gitea.plemya-x.ru/Plemya-x/ALR/pkg/distro"
"gitea.plemya-x.ru/Plemya-x/ALR/pkg/repos"
)
@ -49,9 +50,15 @@ func InfoCmd() *cli.Command {
},
},
BashComplete: func(c *cli.Context) {
err := utils.ExitIfCantDropCapsToAlrUser()
if err != nil {
slog.Error("Can't drop caps")
os.Exit(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)
@ -83,45 +90,50 @@ func InfoCmd() *cli.Command {
}
},
Action: func(c *cli.Context) error {
err := utils.ExitIfCantDropCapsToAlrUser()
if err != nil {
return err
}
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)
}
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)
args := c.Args()
if args.Len() < 1 {
slog.Error(gotext.Get("Command info expected at least 1 argument, got %d", args.Len()))
os.Exit(1)
return cli.Exit(gotext.Get("Command info expected at least 1 argument, got %d", args.Len()), 1)
}
if cfg.AutoPull() {
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)
}
}
found, _, err := rs.FindPkgs(ctx, args.Slice())
if err != nil {
slog.Error(gotext.Get("Error finding packages"), "err", err)
os.Exit(1)
slog.Error(gotext.Get("Error finding packages"))
return cli.Exit(err, 1)
}
if len(found) == 0 {
os.Exit(1)
slog.Error(gotext.Get("Package not found"))
return cli.Exit(err, 1)
}
pkgs := cliutils.FlattenPkgs(ctx, found, "show", c.Bool("interactive"))
@ -131,8 +143,8 @@ func InfoCmd() *cli.Command {
systemLang, err := locale.GetLanguage()
if err != nil {
slog.Error("Can't detect system language", "err", err)
os.Exit(1)
slog.Error(gotext.Get("Can't detect system language"))
return cli.Exit(err, 1)
}
if systemLang == "" {
systemLang = "en"
@ -141,8 +153,8 @@ func InfoCmd() *cli.Command {
if !all {
info, err := distro.ParseOSRelease(ctx)
if err != nil {
slog.Error(gotext.Get("Error parsing os-release file"), "err", err)
os.Exit(1)
slog.Error(gotext.Get("Error parsing os-release file"))
return cli.Exit(err, 1)
}
names, err = overrides.Resolve(
info,
@ -150,8 +162,8 @@ func InfoCmd() *cli.Command {
WithLanguages([]string{systemLang}),
)
if err != nil {
slog.Error(gotext.Get("Error resolving overrides"), "err", err)
os.Exit(1)
slog.Error(gotext.Get("Error resolving overrides"))
return cli.Exit(err, 1)
}
}
@ -159,14 +171,14 @@ 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"), "err", err)
os.Exit(1)
slog.Error(gotext.Get("Error encoding script variables"))
return cli.Exit(err, 1)
}
} else {
err = yaml.NewEncoder(os.Stdout).Encode(pkg)
if err != nil {
slog.Error(gotext.Get("Error encoding script variables"), "err", err)
os.Exit(1)
slog.Error(gotext.Get("Error encoding script variables"))
return cli.Exit(err, 1)
}
}