on main help sign in
refactor: use core utils middleware from `mvdan/sh` (#323)
a32c16cAndrey Nering
go.mod+6−1
@@ -42,7 +42,6 @@ require (
42 github.com/srwiley/rasterx v0.0.0-20220730225603-2ab79fcdd4ef
42 github.com/srwiley/rasterx v0.0.0-20220730225603-2ab79fcdd4ef
43 github.com/stretchr/testify v1.10.0
43 github.com/stretchr/testify v1.10.0
44 github.com/tidwall/sjson v1.2.5
44 github.com/tidwall/sjson v1.2.5
45− github.com/u-root/u-root v0.14.1-0.20250724181933-b01901710169
46 github.com/zeebo/xxh3 v1.0.2
45 github.com/zeebo/xxh3 v1.0.2
47 gopkg.in/natefinch/lumberjack.v2 v2.2.1
46 gopkg.in/natefinch/lumberjack.v2 v2.2.1
48 mvdan.cc/sh/v3 v3.12.1-0.20250726150758-e256f53bade8
47 mvdan.cc/sh/v3 v3.12.1-0.20250726150758-e256f53bade8
@@ -96,7 +95,9 @@ require (
96 github.com/gorilla/css v1.0.1 // indirect
95 github.com/gorilla/css v1.0.1 // indirect
97 github.com/gorilla/websocket v1.5.3 // indirect
96 github.com/gorilla/websocket v1.5.3 // indirect
98 github.com/inconshreveable/mousetrap v1.1.0 // indirect
97 github.com/inconshreveable/mousetrap v1.1.0 // indirect
98+ github.com/klauspost/compress v1.18.0 // indirect
99 github.com/klauspost/cpuid/v2 v2.0.9 // indirect
99 github.com/klauspost/cpuid/v2 v2.0.9 // indirect
100+ github.com/klauspost/pgzip v1.2.6 // indirect
100 github.com/lucasb-eyer/go-colorful v1.2.0
101 github.com/lucasb-eyer/go-colorful v1.2.0
101 github.com/mailru/easyjson v0.7.7 // indirect
102 github.com/mailru/easyjson v0.7.7 // indirect
102 github.com/mattn/go-isatty v0.0.20 // indirect
103 github.com/mattn/go-isatty v0.0.20 // indirect
@@ -109,6 +110,7 @@ require (
109 github.com/muesli/mango-pflag v0.1.0 // indirect
110 github.com/muesli/mango-pflag v0.1.0 // indirect
110 github.com/muesli/roff v0.1.0 // indirect
111 github.com/muesli/roff v0.1.0 // indirect
111 github.com/ncruces/julianday v1.0.0 // indirect
112 github.com/ncruces/julianday v1.0.0 // indirect
113+ github.com/pierrec/lz4/v4 v4.1.22 // indirect
112 github.com/pmezard/go-difflib v1.0.0 // indirect
114 github.com/pmezard/go-difflib v1.0.0 // indirect
113 github.com/rivo/uniseg v0.4.7
115 github.com/rivo/uniseg v0.4.7
114 github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
116 github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
@@ -119,6 +121,8 @@ require (
119 github.com/tidwall/gjson v1.18.0 // indirect
121 github.com/tidwall/gjson v1.18.0 // indirect
120 github.com/tidwall/match v1.1.1 // indirect
122 github.com/tidwall/match v1.1.1 // indirect
121 github.com/tidwall/pretty v1.2.1 // indirect
123 github.com/tidwall/pretty v1.2.1 // indirect
124+ github.com/u-root/u-root v0.14.1-0.20250807200646-5e7721023dc7 // indirect
125+ github.com/u-root/uio v0.0.0-20240224005618-d2acac8f3701 // indirect
122 github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
126 github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
123 github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
127 github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
124 github.com/yosida95/uritemplate/v3 v3.0.2 // indirect
128 github.com/yosida95/uritemplate/v3 v3.0.2 // indirect
@@ -148,4 +152,5 @@ require (
148 google.golang.org/protobuf v1.36.6 // indirect
152 google.golang.org/protobuf v1.36.6 // indirect
149 gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
153 gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
150 gopkg.in/yaml.v3 v3.0.1 // indirect
154 gopkg.in/yaml.v3 v3.0.1 // indirect
155+ mvdan.cc/sh/moreinterp v0.0.0-20250807215248-5a1a658912aa
151)
156)
go.sum+12−2
@@ -165,8 +165,12 @@ github.com/invopop/jsonschema v0.13.0/go.mod h1:ffZ5Km5SWWRAIN6wbDXItl95euhFz2uO
165github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
165github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
166github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
166github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
167github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
167github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
168+github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo=
169+github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ=
168github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4=
170github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4=
169github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
171github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
172+github.com/klauspost/pgzip v1.2.6 h1:8RXeL5crjEUFnR2/Sn6GJNWtSQ3Dk8pq4CL3jvdDyjU=
173+github.com/klauspost/pgzip v1.2.6/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs=
170github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
174github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
171github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
175github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
172github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
176github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
@@ -214,6 +218,8 @@ github.com/nxadm/tail v1.4.11 h1:8feyoE3OzPrcshW5/MJ4sGESc5cqmGkGCWlco4l0bqY=
214github.com/nxadm/tail v1.4.11/go.mod h1:OTaG3NK980DZzxbRq6lEuzgU+mug70nY11sMd4JXXHc=
218github.com/nxadm/tail v1.4.11/go.mod h1:OTaG3NK980DZzxbRq6lEuzgU+mug70nY11sMd4JXXHc=
215github.com/openai/openai-go v1.11.1 h1:fTQ4Sr9eoRiWFAoHzXiZZpVi6KtLeoTMyGrcOCudjNU=
219github.com/openai/openai-go v1.11.1 h1:fTQ4Sr9eoRiWFAoHzXiZZpVi6KtLeoTMyGrcOCudjNU=
216github.com/openai/openai-go v1.11.1/go.mod h1:g461MYGXEXBVdV5SaR/5tNzNbSfwTBBefwc+LlDCK0Y=
220github.com/openai/openai-go v1.11.1/go.mod h1:g461MYGXEXBVdV5SaR/5tNzNbSfwTBBefwc+LlDCK0Y=
221+github.com/pierrec/lz4/v4 v4.1.22 h1:cKFw6uJDK+/gfw5BcDL0JL5aBsAFdsIT18eRtLj7VIU=
222+github.com/pierrec/lz4/v4 v4.1.22/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
217github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c h1:+mdjkGKdHQG3305AYmdv1U2eRNDiU2ErMBj1gwrq8eQ=
223github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c h1:+mdjkGKdHQG3305AYmdv1U2eRNDiU2ErMBj1gwrq8eQ=
218github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjLxUqIJNnCWiEdr3bn6IUYi15bNlnbCCU=
224github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjLxUqIJNnCWiEdr3bn6IUYi15bNlnbCCU=
219github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
225github.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=
272github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
278github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
273github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY=
279github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY=
274github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=
280github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=
275−github.com/u-root/u-root v0.14.1-0.20250724181933-b01901710169 h1:f4cp2yGKkMuGpCwAyNEjzcw8szgVXmemK/wfOu4l5gc=
281+github.com/u-root/u-root v0.14.1-0.20250807200646-5e7721023dc7 h1:ax+jBy7xFhh+Ka0IGLmH5mft+YDuqvzEjSgWuAP0nsM=
276−github.com/u-root/u-root v0.14.1-0.20250724181933-b01901710169/go.mod h1:/0Qr7qJeDwWxoKku2xKQ4Szc+SwBE3g9VE8jNiamsmc=
282+github.com/u-root/u-root v0.14.1-0.20250807200646-5e7721023dc7/go.mod h1:/0Qr7qJeDwWxoKku2xKQ4Szc+SwBE3g9VE8jNiamsmc=
283+github.com/u-root/uio v0.0.0-20240224005618-d2acac8f3701 h1:pyC9PaHYZFgEKFdlp3G8RaCKgVpHZnecvArXvPXcFkM=
284+github.com/u-root/uio v0.0.0-20240224005618-d2acac8f3701/go.mod h1:P3a5rG4X7tI17Nn3aOIAYr5HbIMukwXG0urG0WuL8OA=
277github.com/wk8/go-ordered-map/v2 v2.1.8 h1:5h/BUHu93oj4gIdvHHHGsScSTMijfx5PeYkE/fJgbpc=
285github.com/wk8/go-ordered-map/v2 v2.1.8 h1:5h/BUHu93oj4gIdvHHHGsScSTMijfx5PeYkE/fJgbpc=
278github.com/wk8/go-ordered-map/v2 v2.1.8/go.mod h1:5nJHM5DyteebpVlHnWMV0rPz6Zp7+xBAnxjb1X5vnTw=
286github.com/wk8/go-ordered-map/v2 v2.1.8/go.mod h1:5nJHM5DyteebpVlHnWMV0rPz6Zp7+xBAnxjb1X5vnTw=
279github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
287github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
@@ -412,5 +420,7 @@ modernc.org/memory v1.9.1 h1:V/Z1solwAVmMW1yttq3nDdZPJqV1rM05Ccq6KMSZ34g=
412modernc.org/memory v1.9.1/go.mod h1:/JP4VbVC+K5sU2wZi9bHoq2MAkCnrt2r98UGeSK7Mjw=
420modernc.org/memory v1.9.1/go.mod h1:/JP4VbVC+K5sU2wZi9bHoq2MAkCnrt2r98UGeSK7Mjw=
413modernc.org/sqlite v1.36.2 h1:vjcSazuoFve9Wm0IVNHgmJECoOXLZM1KfMXbcX2axHA=
421modernc.org/sqlite v1.36.2 h1:vjcSazuoFve9Wm0IVNHgmJECoOXLZM1KfMXbcX2axHA=
414modernc.org/sqlite v1.36.2/go.mod h1:ADySlx7K4FdY5MaJcEv86hTJ0PjedAloTUuif0YS3ws=
422modernc.org/sqlite v1.36.2/go.mod h1:ADySlx7K4FdY5MaJcEv86hTJ0PjedAloTUuif0YS3ws=
423+mvdan.cc/sh/moreinterp v0.0.0-20250807215248-5a1a658912aa h1:sRmA9AmA5+9CbK6a7N52q9W9jAeoBy1EJ7cncm+SLxw=
424+mvdan.cc/sh/moreinterp v0.0.0-20250807215248-5a1a658912aa/go.mod h1:Of9PCedbLDYT8b3EyiYG64rNnx5nOp27OLCVdDrjJyo=
415mvdan.cc/sh/v3 v3.12.1-0.20250726150758-e256f53bade8 h1:yOKqXg3uKDx7VxqnrKRUzB+InP3whTBi7jeggyFkfX0=
425mvdan.cc/sh/v3 v3.12.1-0.20250726150758-e256f53bade8 h1:yOKqXg3uKDx7VxqnrKRUzB+InP3whTBi7jeggyFkfX0=
416mvdan.cc/sh/v3 v3.12.1-0.20250726150758-e256f53bade8/go.mod h1:Se6Cj17eYSn+sNooLZiEUnNNmNxg0imoYlTu4CyaGyg=
426mvdan.cc/sh/v3 v3.12.1-0.20250726150758-e256f53bade8/go.mod h1:Se6Cj17eYSn+sNooLZiEUnNNmNxg0imoYlTu4CyaGyg=
internal/shell/coreutils.godeleted−59
@@ -1,59 +0,0 @@
1−package shell
2−
3−import (
4− "context"
5−
6− "github.com/u-root/u-root/pkg/core"
7− "github.com/u-root/u-root/pkg/core/cat"
8− "github.com/u-root/u-root/pkg/core/chmod"
9− "github.com/u-root/u-root/pkg/core/cp"
10− "github.com/u-root/u-root/pkg/core/find"
11− "github.com/u-root/u-root/pkg/core/ls"
12− "github.com/u-root/u-root/pkg/core/mkdir"
13− "github.com/u-root/u-root/pkg/core/mv"
14− "github.com/u-root/u-root/pkg/core/rm"
15− "github.com/u-root/u-root/pkg/core/touch"
16− "github.com/u-root/u-root/pkg/core/xargs"
17− "mvdan.cc/sh/v3/interp"
18−)
19−
20−var coreUtils = map[string]func() core.Command{
21− "cat": func() core.Command { return cat.New() },
22− "chmod": func() core.Command { return chmod.New() },
23− "cp": func() core.Command { return cp.New() },
24− "find": func() core.Command { return find.New() },
25− "ls": func() core.Command { return ls.New() },
26− "mkdir": func() core.Command { return mkdir.New() },
27− "mv": func() core.Command { return mv.New() },
28− "rm": func() core.Command { return rm.New() },
29− "touch": func() core.Command { return touch.New() },
30− "xargs": func() core.Command { return xargs.New() },
31−}
32−
33−func (s *Shell) coreUtilsHandler() func(next interp.ExecHandlerFunc) interp.ExecHandlerFunc {
34− return func(next interp.ExecHandlerFunc) interp.ExecHandlerFunc {
35− return func(ctx context.Context, args []string) error {
36− if len(args) == 0 {
37− return next(ctx, args)
38− }
39−
40− program, programArgs := args[0], args[1:]
41−
42− newCoreUtil, ok := coreUtils[program]
43− if !ok {
44− return next(ctx, args)
45− }
46−
47− c := interp.HandlerCtx(ctx)
48−
49− cmd := newCoreUtil()
50− cmd.SetIO(c.Stdin, c.Stdout, c.Stderr)
51− cmd.SetWorkingDir(c.Dir)
52− cmd.SetLookupEnv(func(key string) (string, bool) {
53− v := c.Env.Get(key)
54− return v.Str, v.Set
55− })
56− return cmd.RunContext(ctx, programArgs...)
57− }
58− }
59−}
internal/shell/shell.go+2−1
@@ -19,6 +19,7 @@ import (
19 "strings"
19 "strings"
20 "sync"
20 "sync"
21
21
22+ "mvdan.cc/sh/moreinterp/coreutils"
22 "mvdan.cc/sh/v3/expand"
23 "mvdan.cc/sh/v3/expand"
23 "mvdan.cc/sh/v3/interp"
24 "mvdan.cc/sh/v3/interp"
24 "mvdan.cc/sh/v3/syntax"
25 "mvdan.cc/sh/v3/syntax"
@@ -221,7 +222,7 @@ func (s *Shell) execPOSIX(ctx context.Context, command string) (string, string,
221 interp.Interactive(false),
222 interp.Interactive(false),
222 interp.Env(expand.ListEnviron(s.env...)),
223 interp.Env(expand.ListEnviron(s.env...)),
223 interp.Dir(s.cwd),
224 interp.Dir(s.cwd),
224− interp.ExecHandlers(s.blockHandler(), s.coreUtilsHandler()),
225+ interp.ExecHandlers(s.blockHandler(), coreutils.ExecHandler),
225 )
226 )
226 if err != nil {
227 if err != nil {
227 return "", "", fmt.Errorf("could not run command: %w", err)
228 return "", "", fmt.Errorf("could not run command: %w", err)