ALR
/
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
19package main20
21import (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
38var app = &cli.App{39Name: "alr",40Usage: "Any Linux Repository",41Flags: []cli.Flag{42&cli.StringFlag{43Name: "pm-args",44Aliases: []string{"P"},45Usage: "Arguments to be passed on to the package manager",46},47&cli.BoolFlag{48Name: "interactive",49Aliases: []string{"i"},50Value: isatty.IsTerminal(os.Stdin.Fd()),51Usage: "Enable interactive questions and prompts",52},53},54Commands: []*cli.Command{55installCmd,56removeCmd,57upgradeCmd,58infoCmd,59listCmd,60buildCmd,61addrepoCmd,62removerepoCmd,63refreshCmd,64fixCmd,65genCmd,66helperCmd,67versionCmd,68},69Before: func(c *cli.Context) error {70ctx := c.Context71log := loggerctx.From(ctx)72
73cmd := c.Args().First()74if cmd != "helper" && !config.Config(ctx).Unsafe.AllowRunAsRoot && os.Geteuid() == 0 {75log.Fatal("Running ALR as root is forbidden as it may cause catastrophic damage to your system").Send()76}77
78if trimmed := strings.TrimSpace(c.String("pm-args")); trimmed != "" {79args := strings.Split(trimmed, " ")80manager.Args = append(manager.Args, args...)81}82
83return nil84},85After: func(ctx *cli.Context) error {86return db.Close()87},88EnableBashCompletion: true,89}
90
91var versionCmd = &cli.Command{92Name: "version",93Usage: "Print the current ALR version and exit",94Action: func(ctx *cli.Context) error {95println(config.Version)96return nil97},98}
99
100func main() {101ctx := context.Background()102log := translations.NewLogger(ctx, logger.NewCLI(os.Stderr), config.Language(ctx))103ctx = loggerctx.With(ctx, log)104
105// Set the root command to the one set in the ALR config106manager.DefaultRootCmd = config.Config(ctx).RootCmd107
108ctx, cancel := signal.NotifyContext(ctx, syscall.SIGINT, syscall.SIGTERM)109defer cancel()110
111err := app.RunContext(ctx, os.Args)112if err != nil {113log.Error("Error while running app").Err(err).Send()114}115}
116