mirror of
https://github.com/charmbracelet/crush.git
synced 2025-08-02 05:20:46 +03:00
feat(shell): use coreutils from u-root
This commit is contained in:
committed by
Christian Rocha
parent
cb3bcaa55f
commit
ec7894ff9f
59
internal/shell/coreutils.go
Normal file
59
internal/shell/coreutils.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package shell
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/u-root/u-root/pkg/core"
|
||||
"github.com/u-root/u-root/pkg/core/cat"
|
||||
"github.com/u-root/u-root/pkg/core/chmod"
|
||||
"github.com/u-root/u-root/pkg/core/cp"
|
||||
"github.com/u-root/u-root/pkg/core/find"
|
||||
"github.com/u-root/u-root/pkg/core/ls"
|
||||
"github.com/u-root/u-root/pkg/core/mkdir"
|
||||
"github.com/u-root/u-root/pkg/core/mv"
|
||||
"github.com/u-root/u-root/pkg/core/rm"
|
||||
"github.com/u-root/u-root/pkg/core/touch"
|
||||
"github.com/u-root/u-root/pkg/core/xargs"
|
||||
"mvdan.cc/sh/v3/interp"
|
||||
)
|
||||
|
||||
var coreUtils = map[string]func() core.Command{
|
||||
"cat": func() core.Command { return cat.New() },
|
||||
"chmod": func() core.Command { return chmod.New() },
|
||||
"cp": func() core.Command { return cp.New() },
|
||||
"find": func() core.Command { return find.New() },
|
||||
"ls": func() core.Command { return ls.New() },
|
||||
"mkdir": func() core.Command { return mkdir.New() },
|
||||
"mv": func() core.Command { return mv.New() },
|
||||
"rm": func() core.Command { return rm.New() },
|
||||
"touch": func() core.Command { return touch.New() },
|
||||
"xargs": func() core.Command { return xargs.New() },
|
||||
}
|
||||
|
||||
func (s *Shell) coreUtilsHandler() func(next interp.ExecHandlerFunc) interp.ExecHandlerFunc {
|
||||
return func(next interp.ExecHandlerFunc) interp.ExecHandlerFunc {
|
||||
return func(ctx context.Context, args []string) error {
|
||||
if len(args) == 0 {
|
||||
return next(ctx, args)
|
||||
}
|
||||
|
||||
program, programArgs := args[0], args[1:]
|
||||
|
||||
newCoreUtil, ok := coreUtils[program]
|
||||
if !ok {
|
||||
return next(ctx, args)
|
||||
}
|
||||
|
||||
c := interp.HandlerCtx(ctx)
|
||||
|
||||
cmd := newCoreUtil()
|
||||
cmd.SetIO(c.Stdin, c.Stdout, c.Stderr)
|
||||
cmd.SetWorkingDir(c.Dir)
|
||||
cmd.SetLookupEnv(func(key string) (string, bool) {
|
||||
v := c.Env.Get(key)
|
||||
return v.Str, v.Set
|
||||
})
|
||||
return cmd.RunContext(ctx, programArgs...)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -221,7 +221,7 @@ func (s *Shell) execPOSIX(ctx context.Context, command string) (string, string,
|
||||
interp.Interactive(false),
|
||||
interp.Env(expand.ListEnviron(s.env...)),
|
||||
interp.Dir(s.cwd),
|
||||
interp.ExecHandlers(s.blockHandler()),
|
||||
interp.ExecHandlers(s.blockHandler(), s.coreUtilsHandler()),
|
||||
)
|
||||
if err != nil {
|
||||
return "", "", fmt.Errorf("could not run command: %w", err)
|
||||
|
||||
Reference in New Issue
Block a user