lure/main.go

109 lines
2.7 KiB
Go
Raw 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"
"go.elara.ws/lure/internal/log"
2023-09-19 21:28:05 +00:00
"go.elara.ws/lure/internal/translations"
2023-09-22 22:21:34 +00:00
"go.elara.ws/lure/internal/config"
"go.elara.ws/lure/internal/db"
2023-09-21 23:18:18 +00:00
"go.elara.ws/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,
versionCmd,
},
Before: func(c *cli.Context) error {
args := strings.Split(c.String("pm-args"), " ")
if len(args) == 1 && args[0] == "" {
return nil
}
manager.Args = append(manager.Args, args...)
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-09-19 21:28:05 +00:00
log.Logger = translations.NewLogger(logger.NewCLI(os.Stderr), config.Language())
if !config.Config().Unsafe.AllowRunAsRoot && os.Geteuid() == 0 {
2023-02-15 23:00:07 +00:00
log.Fatal("Running LURE as root is forbidden as it may cause catastrophic damage to your system").Send()
}
2023-09-19 21:28:05 +00:00
// Set the root command to the one set in the LURE config
manager.DefaultRootCmd = config.Config().RootCmd
2023-05-04 18:53:01 +00:00
2022-09-25 21:00:15 +00:00
ctx := context.Background()
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()
}
}