Debian Codegraph @

moreinterp/coreutils/coreutils.go

raw
1 // Copyright (c) 2025, Andrey Nering <[email protected]>
2 // See LICENSE for licensing information
3
4 // Package coreutils provides a middleware for the interpreter that handles
5 // core utils commands like cat, chmod, cp, find, ls, mkdir, mv, rm, touch
6 // and xargs.
7 //
8 // This is particularly useful to keep the max compability on Windows where
9 // these core utils are not available, unless when installed manually by the
10 // user.
11 package coreutils
12
13 import (
14 "context"
15
16 "github.com/u-root/u-root/pkg/core"
17 "github.com/u-root/u-root/pkg/core/base64"
18 "github.com/u-root/u-root/pkg/core/cat"
19 "github.com/u-root/u-root/pkg/core/chmod"
20 "github.com/u-root/u-root/pkg/core/cp"
21 "github.com/u-root/u-root/pkg/core/find"
22 "github.com/u-root/u-root/pkg/core/gzip"
23 "github.com/u-root/u-root/pkg/core/ls"
24 "github.com/u-root/u-root/pkg/core/mkdir"
25 "github.com/u-root/u-root/pkg/core/mktemp"
26 "github.com/u-root/u-root/pkg/core/mv"
27 "github.com/u-root/u-root/pkg/core/rm"
28 "github.com/u-root/u-root/pkg/core/shasum"
29 "github.com/u-root/u-root/pkg/core/tar"
30 "github.com/u-root/u-root/pkg/core/touch"
31 "github.com/u-root/u-root/pkg/core/xargs"
32 "mvdan.cc/sh/v3/interp"
33 )
34
35 var commandBuilders = map[string]func() core.Command{
36 "cat": func() core.Command { return cat.New() },
37 "chmod": func() core.Command { return chmod.New() },
38 "cp": func() core.Command { return cp.New() },
39 "find": func() core.Command { return find.New() },
40 "ls": func() core.Command { return ls.New() },
41 "mkdir": func() core.Command { return mkdir.New() },
42 "mv": func() core.Command { return mv.New() },
43 "rm": func() core.Command { return rm.New() },
44 "touch": func() core.Command { return touch.New() },
45 "xargs": func() core.Command { return xargs.New() },
46 "base64": func() core.Command { return base64.New() },
47 "gzcat": func() core.Command { return gzip.New("gzcat") },
48 "gzip": func() core.Command { return gzip.New("gzip") },
49 "gunzip": func() core.Command { return gzip.New("gunzip") },
50 "mktemp": func() core.Command { return mktemp.New() },
51 "shasum": func() core.Command { return shasum.New() },
52 "tar": func() core.Command { return tar.New() },
53 }
54
55 // ExecHandler returns an [interp.ExecHandlerFunc] middleware that handles core
56 // utils commands.
57 //
58 // Keep in mind that this middleware has priority over the core utils available
59 // by the system. You may want to use only on Windows to ensure that the system
60 // core utils are used on other platforms, like macOS and Linux.
61 func ExecHandler(next interp.ExecHandlerFunc) interp.ExecHandlerFunc {
62 return func(ctx context.Context, args []string) error {
63 program, programArgs := args[0], args[1:]
64
65 newCoreUtil, ok := commandBuilders[program]
66 if !ok {
67 return next(ctx, args)
68 }
69
70 c := interp.HandlerCtx(ctx)
71
72 cmd := newCoreUtil()
73 cmd.SetIO(c.Stdin, c.Stdout, c.Stderr)
74 cmd.SetWorkingDir(c.Dir)
75 cmd.SetLookupEnv(func(key string) (string, bool) {
76 v := c.Env.Get(key)
77 return v.Str, v.Set
78 })
79 if err := cmd.RunContext(ctx, programArgs...); err != nil {
80 return &Error{err: err}
81 }
82 return nil
83 }
84 }