Skip to content

Commit

Permalink
Merge pull request #34 from k1LoW/colorize
Browse files Browse the repository at this point in the history
Colorize
  • Loading branch information
k1LoW authored Mar 20, 2022
2 parents a45fad3 + 6457c57 commit 669e487
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 57 deletions.
15 changes: 11 additions & 4 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"fmt"
"os"

"github.com/fatih/color"
"github.com/k1LoW/runn"
"github.com/spf13/cobra"
)
Expand All @@ -42,6 +43,8 @@ var runCmd = &cobra.Command{
Long: `run books.`,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
green := color.New(color.FgGreen).SprintFunc()
red := color.New(color.FgRed).SprintFunc()
total := 0
failed := 0
books := []runn.Option{}
Expand All @@ -57,18 +60,18 @@ var runCmd = &cobra.Command{
desc := runn.GetDesc(b)
o, err := runn.New(b, runn.Debug(debug))
if err != nil {
fmt.Printf("%s ... %v\n", desc, err)
fmt.Printf("%s ... %v\n", desc, red(err))
failed += 1
if failFast {
return err
}
continue
}
if err := o.Run(ctx); err != nil {
fmt.Printf("%s ... %v\n", desc, err)
fmt.Printf("%s ... %v\n", desc, red(err))
failed += 1
} else {
fmt.Printf("%s ... ok\n", desc)
fmt.Printf("%s ... %s\n", desc, green("ok"))
}
}
fmt.Println("")
Expand All @@ -83,7 +86,11 @@ var runCmd = &cobra.Command{
} else {
fs = fmt.Sprintf("%d failures", failed)
}
_, _ = fmt.Fprintf(os.Stdout, "%s, %s\n", ts, fs)
if failed > 0 {
_, _ = fmt.Fprintf(os.Stdout, red("%s, %s\n"), ts, fs)
} else {
_, _ = fmt.Fprintf(os.Stdout, green("%s, %s\n"), ts, fs)
}
if failed > 0 {
os.Exit(1)
}
Expand Down
12 changes: 5 additions & 7 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ func (rnr *dbRunner) Run(ctx context.Context, q *dbQuery) error {
stmts := separateStmt(q.stmt)
out := map[string]interface{}{}
for _, stmt := range stmts {
if rnr.operator.debug {
_, _ = fmt.Fprintf(os.Stderr, "-----START QUERY-----\n%s\n-----END QUERY-----\n", stmt)
}
rnr.operator.Debugf("-----START QUERY-----\n%s\n-----END QUERY-----\n", stmt)
err := func() error {
if !strings.HasPrefix(strings.ToUpper(stmt), "SELECT") {
// exec
Expand Down Expand Up @@ -114,7 +112,7 @@ func (rnr *dbRunner) Run(ctx context.Context, q *dbQuery) error {
return err
}
if rnr.operator.debug {
_, _ = fmt.Fprintln(os.Stderr, "-----START ROWS-----")
rnr.operator.Debugln("-----START ROWS-----")
table := tablewriter.NewWriter(os.Stderr)
table.SetHeader(columns)
table.SetAutoFormatHeaders(false)
Expand All @@ -129,11 +127,11 @@ func (rnr *dbRunner) Run(ctx context.Context, q *dbQuery) error {
table.Render()
c := len(rows)
if c == 1 {
_, _ = fmt.Fprintf(os.Stderr, "(%d row)\n", len(rows))
rnr.operator.Debugf("(%d row)\n", len(rows))
} else {
_, _ = fmt.Fprintf(os.Stderr, "(%d rows)\n", len(rows))
rnr.operator.Debugf("(%d rows)\n", len(rows))
}
_, _ = fmt.Fprintln(os.Stderr, "-----END ROWS-----")
rnr.operator.Debugln("-----END ROWS-----")
}
out = map[string]interface{}{
"rows": rows,
Expand Down
16 changes: 4 additions & 12 deletions exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package runn
import (
"bytes"
"context"
"fmt"
"os"
"strings"

"github.com/cli/safeexec"
Expand All @@ -31,27 +29,21 @@ func newExecRunner(o *operator) (*execRunner, error) {
func (rnr *execRunner) Run(ctx context.Context, c *execCommand) error {
stdout := new(bytes.Buffer)
stderr := new(bytes.Buffer)
if rnr.operator.debug {
_, _ = fmt.Fprintf(os.Stderr, "-----START COMMAND-----\n%s\n-----END COMMAND-----\n", c.command)
}
rnr.operator.Debugf("-----START COMMAND-----\n%s\n-----END COMMAND-----\n", c.command)
sh, err := safeexec.LookPath("sh")
if err != nil {
return err
}
cmd := exec.CommandContext(ctx, sh, "-c", c.command)
if strings.Trim(c.stdin, " \n") != "" {
cmd.Stdin = strings.NewReader(c.stdin)
if rnr.operator.debug {
_, _ = fmt.Fprintf(os.Stderr, "-----START STDIN-----\n%s\n-----END STDIN-----\n", c.stdin)
}
rnr.operator.Debugf("-----START STDIN-----\n%s\n-----END STDIN-----\n", c.stdin)
}
cmd.Stdout = stdout
cmd.Stderr = stderr
_ = cmd.Run()
if rnr.operator.debug {
_, _ = fmt.Fprintf(os.Stderr, "-----START STDOUT-----\n%s\n-----END STDOUT-----\n", stdout.String())
_, _ = fmt.Fprintf(os.Stderr, "-----START STDERR-----\n%s\n-----END STDERR-----\n", stderr.String())
}
rnr.operator.Debugf("-----START STDOUT-----\n%s\n-----END STDOUT-----\n", stdout.String())
rnr.operator.Debugf("-----START STDERR-----\n%s\n-----END STDERR-----\n", stderr.String())
rnr.operator.record(map[string]interface{}{
"stdout": stdout.String(),
"stderr": stderr.String(),
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/antonmedv/expr v1.9.0
github.com/bmatcuk/doublestar/v4 v4.0.2
github.com/cli/safeexec v1.0.0
github.com/fatih/color v1.13.0
github.com/go-sql-driver/mysql v1.6.0
github.com/goccy/go-json v0.9.5
github.com/goccy/go-yaml v1.9.5
Expand All @@ -23,7 +24,6 @@ require (
)

require (
github.com/fatih/color v1.13.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
Expand Down
7 changes: 3 additions & 4 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"net/http/httptest"
"net/http/httputil"
"net/url"
"os"
"path"
"strings"
"time"
Expand Down Expand Up @@ -138,7 +137,7 @@ func (rnr *httpRunner) Run(ctx context.Context, r *httpRequest) error {
}
if rnr.operator.debug {
b, _ := httputil.DumpRequest(req, true)
_, _ = fmt.Fprintf(os.Stderr, "-----START HTTP REQUEST-----\n%s\n-----END HTTP REQUEST-----\n", string(b))
rnr.operator.Debugf("-----START HTTP REQUEST-----\n%s\n-----END HTTP REQUEST-----\n", string(b))
}
res, err = rnr.client.Do(req)
if err != nil {
Expand All @@ -155,7 +154,7 @@ func (rnr *httpRunner) Run(ctx context.Context, r *httpRequest) error {
}
if rnr.operator.debug {
b, _ := httputil.DumpRequest(req, true)
_, _ = fmt.Fprintf(os.Stderr, "-----START HTTP REQUEST-----\n%s\n-----END HTTP REQUEST-----\n", string(b))
rnr.operator.Debugf("-----START HTTP REQUEST-----\n%s\n-----END HTTP REQUEST-----\n", string(b))
}
w := httptest.NewRecorder()
rnr.handler.ServeHTTP(w, req)
Expand All @@ -167,7 +166,7 @@ func (rnr *httpRunner) Run(ctx context.Context, r *httpRequest) error {

if rnr.operator.debug {
b, _ := httputil.DumpResponse(res, true)
_, _ = fmt.Fprintf(os.Stderr, "-----START HTTP RESPONSE-----\n%s\n-----END HTTP RESPONSE-----\n", string(b))
rnr.operator.Debugf("-----START HTTP RESPONSE-----\n%s\n-----END HTTP RESPONSE-----\n", string(b))
}

resBody, err := io.ReadAll(res.Body)
Expand Down
50 changes: 27 additions & 23 deletions operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package runn
import (
"context"
"fmt"
"io"
"os"
"path/filepath"
"regexp"
Expand All @@ -12,6 +13,7 @@ import (
"time"

"github.com/antonmedv/expr"
"github.com/fatih/color"
"github.com/goccy/go-yaml"
"github.com/k1LoW/expand"
)
Expand All @@ -21,6 +23,7 @@ var numberRe = regexp.MustCompile(`^[+-]?\d+(?:\.\d+)?$`)

type step struct {
key string
runnerKey string
httpRunner *httpRunner
httpRequest map[string]interface{}
dbRunner *dbRunner
Expand Down Expand Up @@ -77,6 +80,7 @@ type operator struct {
root string
t *testing.T
failFast bool
out io.Writer
}

func (o *operator) record(v map[string]interface{}) {
Expand Down Expand Up @@ -116,6 +120,7 @@ func New(opts ...Option) (*operator, error) {
interval: bk.interval,
t: bk.t,
failFast: bk.failFast,
out: os.Stderr,
}

if bk.path != "" {
Expand Down Expand Up @@ -188,6 +193,7 @@ func (o *operator) AppendStep(key string, s map[string]interface{}) error {
}
step := &step{key: key, debug: o.debug}
for k, v := range s {
step.runnerKey = k
if k == testRunnerKey {
tr, err := newTestRunner(o)
if err != nil {
Expand Down Expand Up @@ -304,15 +310,17 @@ func (o *operator) Run(ctx context.Context) error {
}

func (o *operator) run(ctx context.Context) error {
cyan := color.New(color.FgCyan).SprintFunc()
for i, s := range o.steps {
if i != 0 {
time.Sleep(o.interval)
}
if i != 0 {
o.Debugln("")
}
o.Debugf(cyan("Run '%s' on %s\n"), s.runnerKey, o.stepName(i))
switch {
case s.httpRunner != nil && s.httpRequest != nil:
if o.debug {
_, _ = fmt.Fprintf(os.Stderr, "Run '%s' on %s\n", s.httpRunner.name, o.stepName(i))
}
e, err := o.expand(s.httpRequest)
if err != nil {
return err
Expand All @@ -329,9 +337,6 @@ func (o *operator) run(ctx context.Context) error {
return fmt.Errorf("http request failed on %s: %v", o.stepName(i), err)
}
case s.dbRunner != nil && s.dbQuery != nil:
if o.debug {
_, _ = fmt.Fprintf(os.Stderr, "Run '%s' on %s\n", s.dbRunner.name, o.stepName(i))
}
e, err := o.expand(s.dbQuery)
if err != nil {
return err
Expand All @@ -348,9 +353,6 @@ func (o *operator) run(ctx context.Context) error {
return fmt.Errorf("db query failed on %s: %v", o.stepName(i), err)
}
case s.execRunner != nil && s.execCommand != nil:
if o.debug {
_, _ = fmt.Fprintf(os.Stderr, "Run '%s' on %s\n", execRunnerKey, o.stepName(i))
}
e, err := o.expand(s.execCommand)
if err != nil {
return err
Expand All @@ -367,30 +369,18 @@ func (o *operator) run(ctx context.Context) error {
return fmt.Errorf("exec command failed on %s: %v", o.stepName(i), err)
}
case s.testRunner != nil && s.testCond != "":
if o.debug {
_, _ = fmt.Fprintf(os.Stderr, "Run '%s' on %s\n", testRunnerKey, o.stepName(i))
}
if err := s.testRunner.Run(ctx, s.testCond); err != nil {
return fmt.Errorf("test failed on %s: %v", o.stepName(i), err)
}
case s.dumpRunner != nil && s.dumpCond != "":
if o.debug {
_, _ = fmt.Fprintf(os.Stderr, "Run '%s' on %s\n", dumpRunnerKey, o.stepName(i))
}
if err := s.dumpRunner.Run(ctx, s.dumpCond); err != nil {
return fmt.Errorf("dump failed on %s: %v", o.stepName(i), err)
}
case s.bindRunner != nil && s.bindCond != nil:
if o.debug {
_, _ = fmt.Fprintf(os.Stderr, "Run '%s' on %s\n", bindRunnerKey, o.stepName(i))
}
if err := s.bindRunner.Run(ctx, s.bindCond); err != nil {
return fmt.Errorf("bind failed on %s: %v", o.stepName(i), err)
}
case s.includeRunner != nil && s.includePath != "":
if o.debug {
_, _ = fmt.Fprintf(os.Stderr, "Run '%s' on %s\n", includeRunnerKey, o.stepName(i))
}
if err := s.includeRunner.Run(ctx, s.includePath); err != nil {
return fmt.Errorf("include failed on %s: %v", o.stepName(i), err)
}
Expand All @@ -403,9 +393,9 @@ func (o *operator) run(ctx context.Context) error {

func (o *operator) stepName(i int) string {
if o.useMaps {
return fmt.Sprintf("%s.steps.%s", o.desc, o.steps[i].key)
return fmt.Sprintf("'%s'.steps.%s", o.desc, o.steps[i].key)
}
return fmt.Sprintf("%s.steps[%d]", o.desc, i)
return fmt.Sprintf("'%s'.steps[%d]", o.desc, i)
}

func (o *operator) expand(in interface{}) (interface{}, error) {
Expand Down Expand Up @@ -458,6 +448,20 @@ func (o *operator) expand(in interface{}) (interface{}, error) {
return out, nil
}

func (o *operator) Debugln(a string) {
if !o.debug {
return
}
_, _ = fmt.Fprintln(o.out, a)
}

func (o *operator) Debugf(format string, a ...interface{}) {
if !o.debug {
return
}
_, _ = fmt.Fprintf(o.out, format, a...)
}

type operators struct {
ops []*operator
t *testing.T
Expand Down
9 changes: 3 additions & 6 deletions test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"fmt"
"os"
"strings"

"github.com/antonmedv/expr"
Expand All @@ -30,11 +29,9 @@ var opReplacer = strings.NewReplacer("==", rep, "!=", rep, "<", rep, ">", rep, "
func (rnr *testRunner) Run(ctx context.Context, cond string) error {
store := rnr.operator.store.toMap()
t := buildTree(cond, store)
if rnr.operator.debug {
_, _ = fmt.Fprintln(os.Stderr, "-----START TEST CONDITION-----")
_, _ = fmt.Fprint(os.Stderr, t)
_, _ = fmt.Fprintln(os.Stderr, "-----END TEST CONDITION-----")
}
rnr.operator.Debugln("-----START TEST CONDITION-----")
rnr.operator.Debugf("%s", t)
rnr.operator.Debugln("-----END TEST CONDITION-----")
tf, err := expr.Eval(fmt.Sprintf("(%s) == true", cond), store)
if err != nil {
return err
Expand Down

0 comments on commit 669e487

Please sign in to comment.