Skip to content

Commit

Permalink
Remove splitting executed commands
Browse files Browse the repository at this point in the history
  • Loading branch information
adambabik committed Jul 20, 2022
1 parent bbd139d commit 53fda0f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 29 deletions.
14 changes: 14 additions & 0 deletions internal/cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package cmd

import (
"os"
"path/filepath"

"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/stateful/rdme/internal/parser"
)

var (
Expand Down Expand Up @@ -31,3 +36,12 @@ func Root() *cobra.Command {

return &cmd
}

func newParser() (*parser.Parser, error) {
source, err := os.ReadFile(filepath.Join(chdir, fileName))
if err != nil {
return nil, errors.Wrap(err, "fail to read README file")
}

return parser.New(source), nil
}
32 changes: 3 additions & 29 deletions internal/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@ import (
"os"
"os/exec"
"os/signal"
"path/filepath"
"strings"
"syscall"

"github.com/google/shlex"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/stateful/rdme/internal/parser"
)

func runCmd() *cobra.Command {
Expand Down Expand Up @@ -41,19 +38,14 @@ func runCmd() *cobra.Command {
return filtered, cobra.ShellCompDirectiveNoFileComp | cobra.ShellCompDirectiveNoSpace
},
RunE: func(cmd *cobra.Command, args []string) error {
// TODO: like can omit because Args should do the validation.
if len(args) != 1 {
return cmd.Help()
}

p, err := newParser()
if err != nil {
return errors.Wrap(err, "fail to read README file")
}

snippets := p.Snippets()

snippet, found := p.Snippets().Lookup(args[0])
snippet, found := snippets.Lookup(args[0])
if !found {
return errors.Errorf("command %q not found; known command names: %s", args[0], snippets.Names())
}
Expand All @@ -76,10 +68,6 @@ func runCmd() *cobra.Command {
stdin := cmd.InOrStdin()
stdout, stderr := cmd.OutOrStdout(), cmd.ErrOrStderr()

if len(snippet.Cmds()) == 1 {
return execSingle(ctx, sh, snippet.FirstCmd(), stdin, stdout, stderr)
}

for _, cmd := range snippet.Cmds() {
if err := execSingle(ctx, sh, cmd, stdin, stdout, stderr); err != nil {
return err
Expand All @@ -94,25 +82,11 @@ func runCmd() *cobra.Command {
}

func execSingle(ctx context.Context, sh, cmd string, stdin io.Reader, stdout, stderr io.Writer) error {
fragments, err := shlex.Split(cmd)
if err != nil {
return errors.Wrapf(err, "failed to parse command %q", cmd)
}

c := exec.CommandContext(ctx, sh, []string{"-c", strings.Join(fragments, " ")}...)
c := exec.CommandContext(ctx, sh, []string{"-c", cmd}...)
c.Dir = chdir
c.Stderr = stderr
c.Stdout = stdout
c.Stdin = stdin

return errors.Wrapf(c.Run(), "failed to run command %s", cmd)
}

func newParser() (*parser.Parser, error) {
source, err := os.ReadFile(filepath.Join(chdir, fileName))
if err != nil {
return nil, errors.Wrap(err, "fail to read README file")
}

return parser.New(source), nil
return errors.Wrapf(c.Run(), "failed to run command %q", cmd)
}
12 changes: 12 additions & 0 deletions internal/parser/parse_snippets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,16 @@ go run main.go
snippets4 := p4.Snippets()
assert.Len(t, snippets4, 1)
assert.EqualValues(t, []string{"run"}, snippets4.Names())

p5 := New([]byte(`
Feedback can be patched:
` + "```sh { name=patch-feedback }" + `
$ curl -X PATCH -H "Content-Type: application/json" localhost:8080/feedback/a02b6b5f-46c4-40ff-8160-ff7d55b8ca6f/ -d '{"message": "Modified!"}'
{"id":"a02b6b5f-46c4-40ff-8160-ff7d55b8ca6f"}
` + "```"))
snippets5 := p5.Snippets()
assert.Len(t, snippets5, 1)
assert.EqualValues(t, []string{"patch-feedback"}, snippets5.Names())
p5Snippet, _ := snippets5.Lookup("patch-feedback")
assert.Equal(t, []string{`curl -X PATCH -H "Content-Type: application/json" localhost:8080/feedback/a02b6b5f-46c4-40ff-8160-ff7d55b8ca6f/ -d '{"message": "Modified!"}'`}, p5Snippet.Cmds())
}

0 comments on commit 53fda0f

Please sign in to comment.