Skip to content

Commit b203878

Browse files
committed
feat: correctly handle yaml strings
Both > and | are handled I suggest to only use | with \ at the end of the line, so the command will be easier to read and copy paste for the user. But > will be handled and newlines will be removed. Signed-off-by: Yves Brissaud <[email protected]>
1 parent a22f536 commit b203878

File tree

4 files changed

+12
-7
lines changed

4 files changed

+12
-7
lines changed

internal/pizza/slice.go

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package pizza
22

33
func Map[I, R interface{}](objs []I, fn func(obj I) R) []R {
4+
if len(objs) == 0 {
5+
return nil
6+
}
47
s := []R{}
58
for _, obj := range objs {
69
s = append(s, fn(obj))

internal/runx/run.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func Run(ctx context.Context, out io.Writer, rk *runkit.RunKit, lc *runkit.Local
5959
%s
6060
6161
---
62-
`, runnable.Command)
62+
`, "\n```\n"+runnable.Command+"\n```\n")
6363

6464
var flags []string
6565
if !runConfig.NoConfirm && !lc.AcceptTheRisk {

runkit/read.go

-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"encoding/base64"
77
"fmt"
88
"io"
9-
"strings"
109

1110
"github.com/google/go-containerregistry/pkg/name"
1211
v1 "github.com/google/go-containerregistry/pkg/v1"
@@ -175,9 +174,6 @@ func decodeConfig(rk *RunKit, src string, runxConfig []byte) error {
175174
}
176175
var actions []Action
177176
for _, a := range config.Actions {
178-
// TODO: fix reading of multiline YAML strings
179-
a.Command = strings.ReplaceAll(a.Command, "\n", " ")
180-
181177
if a.Dockerfile != "" {
182178
if c, ok := rk.Files[a.Dockerfile]; ok {
183179
a.DockerfileContent = c

runkit/run.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"mvdan.cc/sh/v3/interp"
1515
"mvdan.cc/sh/v3/syntax"
1616

17+
"github.com/eunomie/docker-runx/internal/pizza"
1718
"github.com/eunomie/docker-runx/internal/tui"
1819
)
1920

@@ -186,7 +187,7 @@ func (r *Runnable) CheckFlags() ([]string, error) {
186187
if r.Action.Type != ActionTypeRun {
187188
return nil, nil
188189
}
189-
tokens, err := shlex.Split(r.args)
190+
tokens, err := splitArgs(r.args)
190191
if err != nil {
191192
return nil, err
192193
}
@@ -197,7 +198,7 @@ func (r *Runnable) CheckFlags() ([]string, error) {
197198
}
198199
if f.NArg() > 0 {
199200
args, _, _ := strings.Cut(r.args, f.Arg(0))
200-
tokens, err = shlex.Split(args)
201+
tokens, err = splitArgs(args)
201202
if err != nil {
202203
return nil, err
203204
}
@@ -217,6 +218,11 @@ func (r *Runnable) CheckFlags() ([]string, error) {
217218
return flagsSet, nil
218219
}
219220

221+
func splitArgs(args string) ([]string, error) {
222+
tokens, err := shlex.Split(args)
223+
return pizza.Map(tokens, strings.TrimSpace), err
224+
}
225+
220226
func (r *Runnable) Run(ctx context.Context) error {
221227
if r.Command == "" {
222228
return fmt.Errorf("command not set")

0 commit comments

Comments
 (0)