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 "fmt"
16
17 "github.com/u-root/u-root/pkg/core"
18 "github.com/u-root/u-root/pkg/core/base64"
19 "github.com/u-root/u-root/pkg/core/cat"
20 "github.com/u-root/u-root/pkg/core/chmod"
21 "github.com/u-root/u-root/pkg/core/cp"
22 "github.com/u-root/u-root/pkg/core/find"
23 "github.com/u-root/u-root/pkg/core/gzip"
24 "github.com/u-root/u-root/pkg/core/ls"
25 "github.com/u-root/u-root/pkg/core/mkdir"
26 "github.com/u-root/u-root/pkg/core/mktemp"
27 "github.com/u-root/u-root/pkg/core/mv"
28 "github.com/u-root/u-root/pkg/core/rm"
29 "github.com/u-root/u-root/pkg/core/shasum"
30 "github.com/u-root/u-root/pkg/core/tar"
31 "github.com/u-root/u-root/pkg/core/touch"
32 "github.com/u-root/u-root/pkg/core/xargs"
33 "mvdan.cc/sh/v3/interp"
34 )
35
36 var commandBuilders = map[string]func() core.Command{
37 "cat": func() core.Command { return cat.New() },
38 "chmod": func() core.Command { return chmod.New() },
39 "cp": func() core.Command { return cp.New() },
40 "find": func() core.Command { return find.New() },
41 "ls": func() core.Command { return ls.New() },
42 "mkdir": func() core.Command { return mkdir.New() },
43 "mv": func() core.Command { return mv.New() },
44 "rm": func() core.Command { return rm.New() },
45 "touch": func() core.Command { return touch.New() },
46 "xargs": func() core.Command { return xargs.New() },
47 "base64": func() core.Command { return base64.New() },
48 "gzcat": func() core.Command { return gzip.New("gzcat") },
49 "gzip": func() core.Command { return gzip.New("gzip") },
50 "gunzip": func() core.Command { return gzip.New("gunzip") },
51 "mktemp": func() core.Command { return mktemp.New() },
52 "shasum": func() core.Command { return shasum.New() },
53 "tar": func() core.Command { return tar.New() },
54 }
55
56 // ExecHandler returns an [interp.ExecHandlerFunc] middleware that handles core
57 // utils commands.
58 //
59 // Keep in mind that this middleware has priority over the core utils available
60 // by the system. You may want to use only on Windows to ensure that the system
61 // core utils are used on other platforms, like macOS and Linux.
62 func ExecHandler(next interp.ExecHandlerFunc) interp.ExecHandlerFunc {
63 return func(ctx context.Context, args []string) error {
64 program, programArgs := args[0], args[1:]
65
66 newCoreUtil, ok := commandBuilders[program]
67 if !ok {
68 return next(ctx, args)
69 }
70
71 c := interp.HandlerCtx(ctx)
72
73 cmd := newCoreUtil()
74 cmd.SetIO(c.Stdin, c.Stdout, c.Stderr)
75 cmd.SetWorkingDir(c.Dir)
76 cmd.SetLookupEnv(func(key string) (string, bool) {
77 v := c.Env.Get(key)
78 return v.Str, v.Set
79 })
80 if err := cmd.RunContext(ctx, programArgs...); err != nil {
81 // Print the error like a real command would, as the returned
82 // exit status may not carry the message.
83 fmt.Fprintf(c.Stderr, "%s: %v\n", program, err)
84 return &Error{err: err}
85 }
86 return nil
87 }
88 }