Skip to content

Commit

Permalink
feat: correctly handle yaml strings
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
eunomie committed Oct 17, 2024
1 parent a22f536 commit b203878
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 7 deletions.
3 changes: 3 additions & 0 deletions internal/pizza/slice.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package pizza

func Map[I, R interface{}](objs []I, fn func(obj I) R) []R {
if len(objs) == 0 {
return nil
}
s := []R{}
for _, obj := range objs {
s = append(s, fn(obj))
Expand Down
2 changes: 1 addition & 1 deletion internal/runx/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func Run(ctx context.Context, out io.Writer, rk *runkit.RunKit, lc *runkit.Local
%s
---
`, runnable.Command)
`, "\n```\n"+runnable.Command+"\n```\n")

var flags []string
if !runConfig.NoConfirm && !lc.AcceptTheRisk {
Expand Down
4 changes: 0 additions & 4 deletions runkit/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/base64"
"fmt"
"io"
"strings"

"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
Expand Down Expand Up @@ -175,9 +174,6 @@ func decodeConfig(rk *RunKit, src string, runxConfig []byte) error {
}
var actions []Action
for _, a := range config.Actions {
// TODO: fix reading of multiline YAML strings
a.Command = strings.ReplaceAll(a.Command, "\n", " ")

if a.Dockerfile != "" {
if c, ok := rk.Files[a.Dockerfile]; ok {
a.DockerfileContent = c
Expand Down
10 changes: 8 additions & 2 deletions runkit/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"mvdan.cc/sh/v3/interp"
"mvdan.cc/sh/v3/syntax"

"github.com/eunomie/docker-runx/internal/pizza"
"github.com/eunomie/docker-runx/internal/tui"
)

Expand Down Expand Up @@ -186,7 +187,7 @@ func (r *Runnable) CheckFlags() ([]string, error) {
if r.Action.Type != ActionTypeRun {
return nil, nil
}
tokens, err := shlex.Split(r.args)
tokens, err := splitArgs(r.args)
if err != nil {
return nil, err
}
Expand All @@ -197,7 +198,7 @@ func (r *Runnable) CheckFlags() ([]string, error) {
}
if f.NArg() > 0 {
args, _, _ := strings.Cut(r.args, f.Arg(0))
tokens, err = shlex.Split(args)
tokens, err = splitArgs(args)
if err != nil {
return nil, err
}
Expand All @@ -217,6 +218,11 @@ func (r *Runnable) CheckFlags() ([]string, error) {
return flagsSet, nil
}

func splitArgs(args string) ([]string, error) {
tokens, err := shlex.Split(args)
return pizza.Map(tokens, strings.TrimSpace), err
}

func (r *Runnable) Run(ctx context.Context) error {
if r.Command == "" {
return fmt.Errorf("command not set")
Expand Down

0 comments on commit b203878

Please sign in to comment.