lure/main.go

116 lines
2.8 KiB
Go
Raw Permalink Normal View History

2022-09-26 19:28:21 +00:00
/*
* LURE - Linux User REpository
2023-09-20 22:38:22 +00:00
* Copyright (C) 2023 Elara Musayelyan
2022-09-26 19:28:21 +00:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2022-09-25 21:00:15 +00:00
package main
import (
"context"
"os"
"os/signal"
"strings"
2022-09-25 21:00:15 +00:00
"syscall"
"github.com/mattn/go-isatty"
2022-09-25 21:00:15 +00:00
"github.com/urfave/cli/v2"
2023-04-21 03:01:05 +00:00
"go.elara.ws/logger"
2023-10-08 00:34:39 +00:00
"lure.sh/lure/internal/config"
"lure.sh/lure/internal/db"
"lure.sh/lure/internal/translations"
"lure.sh/lure/pkg/loggerctx"
"lure.sh/lure/pkg/manager"
2022-09-25 21:00:15 +00:00
)
2023-09-19 21:28:05 +00:00
var app = &cli.App{
Name: "lure",
Usage: "Linux User REpository",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "pm-args",
Aliases: []string{"P"},
Usage: "Arguments to be passed on to the package manager",
},
&cli.BoolFlag{
Name: "interactive",
Aliases: []string{"i"},
Value: isatty.IsTerminal(os.Stdin.Fd()),
Usage: "Enable interactive questions and prompts",
},
},
Commands: []*cli.Command{
installCmd,
removeCmd,
upgradeCmd,
infoCmd,
listCmd,
buildCmd,
addrepoCmd,
removerepoCmd,
refreshCmd,
fixCmd,
2023-10-10 20:38:39 +00:00
genCmd,
2023-10-10 06:06:48 +00:00
helperCmd,
2023-10-10 20:38:39 +00:00
versionCmd,
2023-09-19 21:28:05 +00:00
},
Before: func(c *cli.Context) error {
2023-10-10 06:06:48 +00:00
ctx := c.Context
log := loggerctx.From(ctx)
cmd := c.Args().First()
if cmd != "helper" && !config.Config(ctx).Unsafe.AllowRunAsRoot && os.Geteuid() == 0 {
log.Fatal("Running LURE as root is forbidden as it may cause catastrophic damage to your system").Send()
}
if trimmed := strings.TrimSpace(c.String("pm-args")); trimmed != "" {
args := strings.Split(trimmed, " ")
manager.Args = append(manager.Args, args...)
2023-09-19 21:28:05 +00:00
}
2023-10-10 06:06:48 +00:00
2023-09-19 21:28:05 +00:00
return nil
},
After: func(ctx *cli.Context) error {
return db.Close()
},
EnableBashCompletion: true,
}
2023-01-13 03:41:52 +00:00
2023-09-19 21:28:05 +00:00
var versionCmd = &cli.Command{
Name: "version",
Usage: "Print the current LURE version and exit",
Action: func(ctx *cli.Context) error {
println(config.Version)
return nil
},
2022-11-30 18:00:50 +00:00
}
2022-09-25 21:00:15 +00:00
func main() {
2023-10-06 21:21:12 +00:00
ctx := context.Background()
log := translations.NewLogger(ctx, logger.NewCLI(os.Stderr), config.Language(ctx))
ctx = loggerctx.With(ctx, log)
2023-09-19 21:28:05 +00:00
// Set the root command to the one set in the LURE config
2023-10-06 21:21:12 +00:00
manager.DefaultRootCmd = config.Config(ctx).RootCmd
2023-05-04 18:53:01 +00:00
2022-09-25 21:00:15 +00:00
ctx, cancel := signal.NotifyContext(ctx, syscall.SIGINT, syscall.SIGTERM)
defer cancel()
2023-09-19 21:28:05 +00:00
err := app.RunContext(ctx, os.Args)
2022-09-25 21:00:15 +00:00
if err != nil {
log.Error("Error while running app").Err(err).Send()
}
}