From 1133785dbde13dfd9290d655d5c51b20fd23eff0 Mon Sep 17 00:00:00 2001 From: Elara Musayelyan Date: Mon, 9 Oct 2023 23:06:48 -0700 Subject: [PATCH] Add helper command --- helper.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 21 +++++++++++++-------- 2 files changed, 61 insertions(+), 8 deletions(-) create mode 100644 helper.go diff --git a/helper.go b/helper.go new file mode 100644 index 0000000..7daa038 --- /dev/null +++ b/helper.go @@ -0,0 +1,48 @@ +package main + +import ( + "os" + + "github.com/urfave/cli/v2" + "lure.sh/lure/internal/shutils/helpers" + "lure.sh/lure/pkg/loggerctx" + "mvdan.cc/sh/v3/expand" + "mvdan.cc/sh/v3/interp" +) + +var helperCmd = &cli.Command{ + Name: "helper", + Usage: "Run a lure helper command", + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "dest-dir", + Aliases: []string{"d"}, + Usage: "The directory that the install commands will install to", + Value: "dest", + }, + }, + Action: func(c *cli.Context) error { + ctx := c.Context + log := loggerctx.From(ctx) + + wd, err := os.Getwd() + if err != nil { + log.Fatal("Error getting working directory").Err(err).Send() + } + + helper, ok := helpers.Helpers[c.Args().First()] + if !ok { + log.Fatal("No such helper command").Str("handler", c.Args().First()).Send() + } + + hc := interp.HandlerContext{ + Env: expand.ListEnviron("pkgdir=" + c.String("dest-dir")), + Dir: wd, + Stdin: os.Stdin, + Stdout: os.Stdout, + Stderr: os.Stderr, + } + + return helper(hc, c.Args().First(), c.Args().Slice()[1:]) + }, +} diff --git a/main.go b/main.go index e6745c1..e63c61f 100644 --- a/main.go +++ b/main.go @@ -63,13 +63,22 @@ var app = &cli.App{ refreshCmd, fixCmd, versionCmd, + helperCmd, }, Before: func(c *cli.Context) error { - args := strings.Split(c.String("pm-args"), " ") - if len(args) == 1 && args[0] == "" { - return nil + 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() } - manager.Args = append(manager.Args, args...) + + 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 { @@ -92,10 +101,6 @@ func main() { log := translations.NewLogger(ctx, logger.NewCLI(os.Stderr), config.Language(ctx)) ctx = loggerctx.With(ctx, log) - if !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() - } - // Set the root command to the one set in the LURE config manager.DefaultRootCmd = config.Config(ctx).RootCmd