Add helper command
ci/woodpecker/push/woodpecker Pipeline was successful Details

This commit is contained in:
Elara 2023-10-09 23:06:48 -07:00
parent f32dddee63
commit 1133785dbd
2 changed files with 61 additions and 8 deletions

48
helper.go Normal file
View File

@ -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:])
},
}

21
main.go
View File

@ -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