ALR

Форк
1
/
main.go 
115 строк · 2.9 Кб
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

19
package main
20

21
import (
22
	"context"
23
	"os"
24
	"os/signal"
25
	"strings"
26
	"syscall"
27

28
	"github.com/mattn/go-isatty"
29
	"github.com/urfave/cli/v2"
30
	"go.elara.ws/logger"
31
	"plemya-x.ru/alr/internal/config"
32
	"plemya-x.ru/alr/internal/db"
33
	"plemya-x.ru/alr/internal/translations"
34
	"plemya-x.ru/alr/pkg/loggerctx"
35
	"plemya-x.ru/alr/pkg/manager"
36
)
37

38
var app = &cli.App{
39
	Name:  "alr",
40
	Usage: "Any Linux Repository",
41
	Flags: []cli.Flag{
42
		&cli.StringFlag{
43
			Name:    "pm-args",
44
			Aliases: []string{"P"},
45
			Usage:   "Arguments to be passed on to the package manager",
46
		},
47
		&cli.BoolFlag{
48
			Name:    "interactive",
49
			Aliases: []string{"i"},
50
			Value:   isatty.IsTerminal(os.Stdin.Fd()),
51
			Usage:   "Enable interactive questions and prompts",
52
		},
53
	},
54
	Commands: []*cli.Command{
55
		installCmd,
56
		removeCmd,
57
		upgradeCmd,
58
		infoCmd,
59
		listCmd,
60
		buildCmd,
61
		addrepoCmd,
62
		removerepoCmd,
63
		refreshCmd,
64
		fixCmd,
65
		genCmd,
66
		helperCmd,
67
		versionCmd,
68
	},
69
	Before: func(c *cli.Context) error {
70
		ctx := c.Context
71
		log := loggerctx.From(ctx)
72

73
		cmd := c.Args().First()
74
		if cmd != "helper" && !config.Config(ctx).Unsafe.AllowRunAsRoot && os.Geteuid() == 0 {
75
			log.Fatal("Running ALR as root is forbidden as it may cause catastrophic damage to your system").Send()
76
		}
77

78
		if trimmed := strings.TrimSpace(c.String("pm-args")); trimmed != "" {
79
			args := strings.Split(trimmed, " ")
80
			manager.Args = append(manager.Args, args...)
81
		}
82

83
		return nil
84
	},
85
	After: func(ctx *cli.Context) error {
86
		return db.Close()
87
	},
88
	EnableBashCompletion: true,
89
}
90

91
var versionCmd = &cli.Command{
92
	Name:  "version",
93
	Usage: "Print the current ALR version and exit",
94
	Action: func(ctx *cli.Context) error {
95
		println(config.Version)
96
		return nil
97
	},
98
}
99

100
func main() {
101
	ctx := context.Background()
102
	log := translations.NewLogger(ctx, logger.NewCLI(os.Stderr), config.Language(ctx))
103
	ctx = loggerctx.With(ctx, log)
104

105
	// Set the root command to the one set in the ALR config
106
	manager.DefaultRootCmd = config.Config(ctx).RootCmd
107

108
	ctx, cancel := signal.NotifyContext(ctx, syscall.SIGINT, syscall.SIGTERM)
109
	defer cancel()
110

111
	err := app.RunContext(ctx, os.Args)
112
	if err != nil {
113
		log.Error("Error while running app").Err(err).Send()
114
	}
115
}
116

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.