Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove splitting executed commands #7

Merged
merged 1 commit into from
Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ repos:
- id: go-revive-repo-mod
- id: go-staticcheck-mod
- id: go-sec-repo-mod
args: ["-exclude=G204,G104", "-exclude-generated"]
args: ["-exclude=G204,G304", "-exclude-generated"]
4 changes: 1 addition & 3 deletions internal/cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ func listCmd() *cobra.Command {
table.EndRow()
}

table.Render()

return nil
return errors.Wrap(table.Render(), "failed to render")
},
}
return &cmd
Expand Down
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())
}