Skip to content

Commit

Permalink
Use CmdRunner instead of Command interface
Browse files Browse the repository at this point in the history
Signed-off-by: Tobias Brumhard <tobias.brumhard@mail.schwarz>
  • Loading branch information
brumhard committed Oct 30, 2021
1 parent ed68423 commit 38fd02f
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 88 deletions.
79 changes: 0 additions & 79 deletions pkg/exec/command.go

This file was deleted.

17 changes: 14 additions & 3 deletions pkg/exec/command_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package exec

import (
"fmt"
"os/exec"
"strings"

"github.com/pkg/errors"
Expand All @@ -14,10 +15,16 @@ type CommandGroup struct {
// func run before any of the commands is executed
PreRun func() error
// Commands to run
Commands []Command
Commands []*exec.Cmd
// TargetDir to run in
TargetDir string
}

func (cg *CommandGroup) Run() error {
return cg.RunWith(NewExecCmdRunner())
}

func (cg *CommandGroup) RunWith(runner CmdRunner) error {
if len(cg.Commands) == 0 {
return nil
}
Expand All @@ -26,15 +33,19 @@ func (cg *CommandGroup) Run() error {
if err := cg.PreRun(); err != nil {
var skipsCmds []string
for _, cmd := range cg.Commands {
skipsCmds = append(skipsCmds, fmt.Sprintf("`%s`", strings.Join(cmd.Args(), " ")))
skipsCmds = append(skipsCmds, fmt.Sprintf("`%s`", strings.Join(cmd.Args, " ")))
}

return errors.Wrapf(err, "skipping %s", strings.Join(skipsCmds, ", "))
}
}

for _, cmd := range cg.Commands {
if _, err := cmd.Run(); err != nil {
if cg.TargetDir != "" {
cmd.Dir = cg.TargetDir
}

if _, err := runner.Run(cmd); err != nil {
return err
}
}
Expand Down
63 changes: 63 additions & 0 deletions pkg/exec/runner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package exec

import (
"bytes"
"fmt"
"os/exec"
"strings"
)

var (
_ CmdRunner = (*execCmdRunner)(nil)
_ CmdRunner = CmdRunnerFunc(nil)
)

// CmdRunner is an interface to safely abstract exec.Cmd calls.
type CmdRunner interface {
Run(cmd *exec.Cmd) (string, error)
}

type CmdRunnerFunc func(cmd *exec.Cmd) (string, error)

func (f CmdRunnerFunc) Run(cmd *exec.Cmd) (string, error) {
return f(cmd)
}

type execCmdRunner struct{}

func (r *execCmdRunner) Run(cmd *exec.Cmd) (string, error) {
stdout, stderr := &bytes.Buffer{}, &bytes.Buffer{}
cmd.Stdout, cmd.Stderr = stdout, stderr

if err := cmd.Run(); err != nil {
return "", &ErrWithStderr{
Wrapped: err,
Args: cmd.Args,
StdErr: stderr.Bytes(),
}
}

return stdout.String(), nil
}

func NewExecCmdRunner() CmdRunner {
return &execCmdRunner{}
}

type ErrWithStderr struct {
Wrapped error
StdErr []byte
Args []string
}

func (e *ErrWithStderr) Error() string {
if len(e.StdErr) > 0 {
return fmt.Sprintf("failed running `%s`, %q: %s", strings.Join(e.Args, " "), e.StdErr, e.Wrapped.Error())
}

return fmt.Sprintf("failed running `%s`, make sure %s is available: %s", strings.Join(e.Args, " "), e.Args[0], e.Wrapped.Error())
}

func (e *ErrWithStderr) Unwrap() error {
return e.Wrapped
}
15 changes: 9 additions & 6 deletions pkg/gotemplate/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io/fs"
"os"
"os/exec"
"path"
"reflect"
"strconv"
Expand Down Expand Up @@ -238,16 +239,18 @@ func (gt *GT) InitNewProject(opts *NewRepositoryOptions) (err error) {
func (gt *GT) initRepo(targetDir, moduleName string) {
commandGroups := []ownexec.CommandGroup{
{
Commands: []ownexec.Command{
ownexec.NewExecCmd([]string{"git", "init"}, ownexec.WithTargetDir(targetDir)),
Commands: []*exec.Cmd{
exec.Command("git", "init"),
},
TargetDir: targetDir,
},
{
PreRun: checkGoVersion,
Commands: []ownexec.Command{
ownexec.NewExecCmd([]string{"go", "mod", "init", moduleName}, ownexec.WithTargetDir(targetDir)),
ownexec.NewExecCmd([]string{"go", "mod", "tidy"}, ownexec.WithTargetDir(targetDir)),
// PreRun: checkGoVersion,
Commands: []*exec.Cmd{
exec.Command("go", "mod", "init", moduleName),
exec.Command("go", "mod", "tidy"),
},
TargetDir: targetDir,
},
}

Expand Down

0 comments on commit 38fd02f

Please sign in to comment.