From a32c16cb8720bc4288cbd81b1999aad232528e77 Mon Sep 17 00:00:00 2001 From: Andrey Nering Date: Thu, 7 Aug 2025 19:14:06 -0300 Subject: [PATCH] refactor: use core utils middleware from `mvdan/sh` (#323) * https://github.com/mvdan/sh/pull/1179 * https://github.com/mvdan/sh/pull/1186 --- go.mod | 7 ++++- go.sum | 14 +++++++-- internal/shell/coreutils.go | 59 ------------------------------------- internal/shell/shell.go | 3 +- 4 files changed, 20 insertions(+), 63 deletions(-) delete mode 100644 internal/shell/coreutils.go diff --git a/go.mod b/go.mod index 035e050bdfd6d6c07e876770c73716ce5f014f64..1bd12814d507a120d768dbe3aca6baa8eba9fa15 100644 --- a/go.mod +++ b/go.mod @@ -42,7 +42,6 @@ require ( github.com/srwiley/rasterx v0.0.0-20220730225603-2ab79fcdd4ef github.com/stretchr/testify v1.10.0 github.com/tidwall/sjson v1.2.5 - github.com/u-root/u-root v0.14.1-0.20250724181933-b01901710169 github.com/zeebo/xxh3 v1.0.2 gopkg.in/natefinch/lumberjack.v2 v2.2.1 mvdan.cc/sh/v3 v3.12.1-0.20250726150758-e256f53bade8 @@ -96,7 +95,9 @@ require ( github.com/gorilla/css v1.0.1 // indirect github.com/gorilla/websocket v1.5.3 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/klauspost/compress v1.18.0 // indirect github.com/klauspost/cpuid/v2 v2.0.9 // indirect + github.com/klauspost/pgzip v1.2.6 // indirect github.com/lucasb-eyer/go-colorful v1.2.0 github.com/mailru/easyjson v0.7.7 // indirect github.com/mattn/go-isatty v0.0.20 // indirect @@ -109,6 +110,7 @@ require ( github.com/muesli/mango-pflag v0.1.0 // indirect github.com/muesli/roff v0.1.0 // indirect github.com/ncruces/julianday v1.0.0 // indirect + github.com/pierrec/lz4/v4 v4.1.22 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/rivo/uniseg v0.4.7 github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect @@ -119,6 +121,8 @@ require ( github.com/tidwall/gjson v1.18.0 // indirect github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/pretty v1.2.1 // indirect + github.com/u-root/u-root v0.14.1-0.20250807200646-5e7721023dc7 // indirect + github.com/u-root/uio v0.0.0-20240224005618-d2acac8f3701 // indirect github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect github.com/yosida95/uritemplate/v3 v3.0.2 // indirect @@ -148,4 +152,5 @@ require ( google.golang.org/protobuf v1.36.6 // indirect gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect + mvdan.cc/sh/moreinterp v0.0.0-20250807215248-5a1a658912aa ) diff --git a/go.sum b/go.sum index 5d7d3f91c9d346d632fc16667a165a90f4131082..c7c543d4c2fe34ad7830b46a428e3229bc42bf2c 100644 --- a/go.sum +++ b/go.sum @@ -165,8 +165,12 @@ github.com/invopop/jsonschema v0.13.0/go.mod h1:ffZ5Km5SWWRAIN6wbDXItl95euhFz2uO github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= +github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo= +github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ= github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4= github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= +github.com/klauspost/pgzip v1.2.6 h1:8RXeL5crjEUFnR2/Sn6GJNWtSQ3Dk8pq4CL3jvdDyjU= +github.com/klauspost/pgzip v1.2.6/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= @@ -214,6 +218,8 @@ github.com/nxadm/tail v1.4.11 h1:8feyoE3OzPrcshW5/MJ4sGESc5cqmGkGCWlco4l0bqY= github.com/nxadm/tail v1.4.11/go.mod h1:OTaG3NK980DZzxbRq6lEuzgU+mug70nY11sMd4JXXHc= github.com/openai/openai-go v1.11.1 h1:fTQ4Sr9eoRiWFAoHzXiZZpVi6KtLeoTMyGrcOCudjNU= github.com/openai/openai-go v1.11.1/go.mod h1:g461MYGXEXBVdV5SaR/5tNzNbSfwTBBefwc+LlDCK0Y= +github.com/pierrec/lz4/v4 v4.1.22 h1:cKFw6uJDK+/gfw5BcDL0JL5aBsAFdsIT18eRtLj7VIU= +github.com/pierrec/lz4/v4 v4.1.22/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4= github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c h1:+mdjkGKdHQG3305AYmdv1U2eRNDiU2ErMBj1gwrq8eQ= github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjLxUqIJNnCWiEdr3bn6IUYi15bNlnbCCU= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= @@ -272,8 +278,10 @@ github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4= github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY= github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28= -github.com/u-root/u-root v0.14.1-0.20250724181933-b01901710169 h1:f4cp2yGKkMuGpCwAyNEjzcw8szgVXmemK/wfOu4l5gc= -github.com/u-root/u-root v0.14.1-0.20250724181933-b01901710169/go.mod h1:/0Qr7qJeDwWxoKku2xKQ4Szc+SwBE3g9VE8jNiamsmc= +github.com/u-root/u-root v0.14.1-0.20250807200646-5e7721023dc7 h1:ax+jBy7xFhh+Ka0IGLmH5mft+YDuqvzEjSgWuAP0nsM= +github.com/u-root/u-root v0.14.1-0.20250807200646-5e7721023dc7/go.mod h1:/0Qr7qJeDwWxoKku2xKQ4Szc+SwBE3g9VE8jNiamsmc= +github.com/u-root/uio v0.0.0-20240224005618-d2acac8f3701 h1:pyC9PaHYZFgEKFdlp3G8RaCKgVpHZnecvArXvPXcFkM= +github.com/u-root/uio v0.0.0-20240224005618-d2acac8f3701/go.mod h1:P3a5rG4X7tI17Nn3aOIAYr5HbIMukwXG0urG0WuL8OA= github.com/wk8/go-ordered-map/v2 v2.1.8 h1:5h/BUHu93oj4gIdvHHHGsScSTMijfx5PeYkE/fJgbpc= github.com/wk8/go-ordered-map/v2 v2.1.8/go.mod h1:5nJHM5DyteebpVlHnWMV0rPz6Zp7+xBAnxjb1X5vnTw= github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no= @@ -412,5 +420,7 @@ modernc.org/memory v1.9.1 h1:V/Z1solwAVmMW1yttq3nDdZPJqV1rM05Ccq6KMSZ34g= modernc.org/memory v1.9.1/go.mod h1:/JP4VbVC+K5sU2wZi9bHoq2MAkCnrt2r98UGeSK7Mjw= modernc.org/sqlite v1.36.2 h1:vjcSazuoFve9Wm0IVNHgmJECoOXLZM1KfMXbcX2axHA= modernc.org/sqlite v1.36.2/go.mod h1:ADySlx7K4FdY5MaJcEv86hTJ0PjedAloTUuif0YS3ws= +mvdan.cc/sh/moreinterp v0.0.0-20250807215248-5a1a658912aa h1:sRmA9AmA5+9CbK6a7N52q9W9jAeoBy1EJ7cncm+SLxw= +mvdan.cc/sh/moreinterp v0.0.0-20250807215248-5a1a658912aa/go.mod h1:Of9PCedbLDYT8b3EyiYG64rNnx5nOp27OLCVdDrjJyo= mvdan.cc/sh/v3 v3.12.1-0.20250726150758-e256f53bade8 h1:yOKqXg3uKDx7VxqnrKRUzB+InP3whTBi7jeggyFkfX0= mvdan.cc/sh/v3 v3.12.1-0.20250726150758-e256f53bade8/go.mod h1:Se6Cj17eYSn+sNooLZiEUnNNmNxg0imoYlTu4CyaGyg= diff --git a/internal/shell/coreutils.go b/internal/shell/coreutils.go deleted file mode 100644 index 5669d578987ba5a8792430c96e6fc869d8b5cf55..0000000000000000000000000000000000000000 --- a/internal/shell/coreutils.go +++ /dev/null @@ -1,59 +0,0 @@ -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...) - } - } -} diff --git a/internal/shell/shell.go b/internal/shell/shell.go index 751aeeccd9c7dba977366f859f8f0d910c96dbab..0d2df7b11abc6c8261a77f46b7eb4c93e9c63e70 100644 --- a/internal/shell/shell.go +++ b/internal/shell/shell.go @@ -19,6 +19,7 @@ import ( "strings" "sync" + "mvdan.cc/sh/moreinterp/coreutils" "mvdan.cc/sh/v3/expand" "mvdan.cc/sh/v3/interp" "mvdan.cc/sh/v3/syntax" @@ -221,7 +222,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(), s.coreUtilsHandler()), + interp.ExecHandlers(s.blockHandler(), coreutils.ExecHandler), ) if err != nil { return "", "", fmt.Errorf("could not run command: %w", err)