feat: add new logger and translation
This commit is contained in:
120
main.go
120
main.go
@ -28,68 +28,18 @@ import (
|
||||
|
||||
"github.com/mattn/go-isatty"
|
||||
"github.com/urfave/cli/v2"
|
||||
"go.elara.ws/logger"
|
||||
|
||||
oldLogger "go.elara.ws/logger"
|
||||
|
||||
"gitea.plemya-x.ru/Plemya-x/ALR/internal/config"
|
||||
"gitea.plemya-x.ru/Plemya-x/ALR/internal/db"
|
||||
"gitea.plemya-x.ru/Plemya-x/ALR/internal/translations"
|
||||
"gitea.plemya-x.ru/Plemya-x/ALR/pkg/loggerctx"
|
||||
"gitea.plemya-x.ru/Plemya-x/ALR/pkg/manager"
|
||||
|
||||
"gitea.plemya-x.ru/Plemya-x/ALR/internal/logger"
|
||||
)
|
||||
|
||||
var app = &cli.App{
|
||||
Name: "alr",
|
||||
Usage: "Any Linux 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,
|
||||
genCmd,
|
||||
helperCmd,
|
||||
versionCmd,
|
||||
},
|
||||
Before: func(c *cli.Context) error {
|
||||
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 ALR 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...)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
After: func(ctx *cli.Context) error {
|
||||
return db.Close()
|
||||
},
|
||||
EnableBashCompletion: true,
|
||||
}
|
||||
|
||||
var versionCmd = &cli.Command{
|
||||
Name: "version",
|
||||
Usage: "Print the current ALR version and exit",
|
||||
@ -99,9 +49,69 @@ var versionCmd = &cli.Command{
|
||||
},
|
||||
}
|
||||
|
||||
func GetApp() *cli.App {
|
||||
return &cli.App{
|
||||
Name: "alr",
|
||||
Usage: "Any Linux 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,
|
||||
GetInfoCmd(),
|
||||
listCmd,
|
||||
buildCmd,
|
||||
addrepoCmd,
|
||||
removerepoCmd,
|
||||
refreshCmd,
|
||||
fixCmd,
|
||||
genCmd,
|
||||
helperCmd,
|
||||
versionCmd,
|
||||
},
|
||||
Before: func(c *cli.Context) error {
|
||||
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 ALR 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...)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
After: func(ctx *cli.Context) error {
|
||||
return db.Close()
|
||||
},
|
||||
EnableBashCompletion: true,
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
translations.Setup()
|
||||
logger.SetupDefault()
|
||||
|
||||
app := GetApp()
|
||||
|
||||
ctx := context.Background()
|
||||
log := translations.NewLogger(ctx, logger.NewCLI(os.Stderr), config.Language(ctx))
|
||||
log := translations.NewLogger(ctx, oldLogger.NewCLI(os.Stderr), config.Language(ctx))
|
||||
ctx = loggerctx.With(ctx, log)
|
||||
|
||||
// Set the root command to the one set in the ALR config
|
||||
|
Reference in New Issue
Block a user