ALR
/
info.go
106 строк · 2.6 Кб
1/*
2* ALR - Any Linux Repository
3* Copyright (C) 2024 Евгений Храмов
4*
5* This program is free software: you can redistribute it and/or modify
6* it under the terms of the GNU General Public License as published by
7* the Free Software Foundation, either version 3 of the License, or
8* (at your option) any later version.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19package main
20
21import (
22"fmt"
23"os"
24
25"github.com/urfave/cli/v2"
26"plemya-x.ru/alr/internal/cliutils"
27"plemya-x.ru/alr/internal/config"
28"plemya-x.ru/alr/internal/overrides"
29"plemya-x.ru/alr/pkg/distro"
30"plemya-x.ru/alr/pkg/loggerctx"
31"plemya-x.ru/alr/pkg/repos"
32"gopkg.in/yaml.v3"
33)
34
35var infoCmd = &cli.Command{
36Name: "info",
37Usage: "Print information about a package",
38Flags: []cli.Flag{
39&cli.BoolFlag{
40Name: "all",
41Aliases: []string{"a"},
42Usage: "Show all information, not just for the current distro",
43},
44},
45Action: func(c *cli.Context) error {
46ctx := c.Context
47log := loggerctx.From(ctx)
48
49args := c.Args()
50if args.Len() < 1 {
51log.Fatalf("Command info expected at least 1 argument, got %d", args.Len()).Send()
52}
53
54err := repos.Pull(ctx, config.Config(ctx).Repos)
55if err != nil {
56log.Fatal("Error pulling repositories").Err(err).Send()
57}
58
59found, _, err := repos.FindPkgs(ctx, args.Slice())
60if err != nil {
61log.Fatal("Error finding packages").Err(err).Send()
62}
63
64if len(found) == 0 {
65os.Exit(1)
66}
67
68pkgs := cliutils.FlattenPkgs(ctx, found, "show", c.Bool("interactive"))
69
70var names []string
71all := c.Bool("all")
72
73if !all {
74info, err := distro.ParseOSRelease(ctx)
75if err != nil {
76log.Fatal("Error parsing os-release file").Err(err).Send()
77}
78names, err = overrides.Resolve(
79info,
80overrides.DefaultOpts.
81WithLanguages([]string{config.SystemLang()}),
82)
83if err != nil {
84log.Fatal("Error resolving overrides").Err(err).Send()
85}
86}
87
88for _, pkg := range pkgs {
89if !all {
90err = yaml.NewEncoder(os.Stdout).Encode(overrides.ResolvePackage(&pkg, names))
91if err != nil {
92log.Fatal("Error encoding script variables").Err(err).Send()
93}
94} else {
95err = yaml.NewEncoder(os.Stdout).Encode(pkg)
96if err != nil {
97log.Fatal("Error encoding script variables").Err(err).Send()
98}
99}
100
101fmt.Println("---")
102}
103
104return nil
105},
106}
107