Add helper command
ci/woodpecker/push/woodpecker Pipeline was successful
Details
ci/woodpecker/push/woodpecker Pipeline was successful
Details
This commit is contained in:
parent
f32dddee63
commit
1133785dbd
|
@ -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
21
main.go
|
@ -63,13 +63,22 @@ var app = &cli.App{
|
||||||
refreshCmd,
|
refreshCmd,
|
||||||
fixCmd,
|
fixCmd,
|
||||||
versionCmd,
|
versionCmd,
|
||||||
|
helperCmd,
|
||||||
},
|
},
|
||||||
Before: func(c *cli.Context) error {
|
Before: func(c *cli.Context) error {
|
||||||
args := strings.Split(c.String("pm-args"), " ")
|
ctx := c.Context
|
||||||
if len(args) == 1 && args[0] == "" {
|
log := loggerctx.From(ctx)
|
||||||
return nil
|
|
||||||
|
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
|
return nil
|
||||||
},
|
},
|
||||||
After: func(ctx *cli.Context) error {
|
After: func(ctx *cli.Context) error {
|
||||||
|
@ -92,10 +101,6 @@ func main() {
|
||||||
log := translations.NewLogger(ctx, logger.NewCLI(os.Stderr), config.Language(ctx))
|
log := translations.NewLogger(ctx, logger.NewCLI(os.Stderr), config.Language(ctx))
|
||||||
ctx = loggerctx.With(ctx, log)
|
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
|
// Set the root command to the one set in the LURE config
|
||||||
manager.DefaultRootCmd = config.Config(ctx).RootCmd
|
manager.DefaultRootCmd = config.Config(ctx).RootCmd
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue