Skip to content

Commit

Permalink
Merge pull request #243 from k1LoW/runner
Browse files Browse the repository at this point in the history
Support `--runner req:https://example.com/api/v1` option for CLI
  • Loading branch information
k1LoW authored Oct 28, 2022
2 parents c380c87 + 0a35e2a commit 6c4ae41
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 151 deletions.
15 changes: 8 additions & 7 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ var listCmd = &cobra.Command{
table.SetBorder(false)

pathp := strings.Join(args, string(filepath.ListSeparator))
opts, err := collectOpts()
opts, err := flags.ToOpts()
if err != nil {
return err
}
Expand All @@ -73,10 +73,11 @@ var listCmd = &cobra.Command{

func init() {
rootCmd.AddCommand(listCmd)
listCmd.Flags().BoolVarP(&skipIncluded, "skip-included", "", false, `skip running the included step by itself`)
listCmd.Flags().StringSliceVarP(&vars, "var", "", []string{}, `set var to runbook ("key:value")`)
listCmd.Flags().StringSliceVarP(&overlays, "overlay", "", []string{}, "overlay values on the runbook")
listCmd.Flags().StringSliceVarP(&underlays, "underlay", "", []string{}, "lay values under the runbook")
listCmd.Flags().IntVarP(&sample, "sample", "", 0, "run the specified number of runbooks at random")
listCmd.Flags().StringVarP(&shuffle, "shuffle", "", "off", `randomize the order of running runbooks ("on","off",N)`)
listCmd.Flags().BoolVarP(&flags.SkipIncluded, "skip-included", "", false, `skip running the included step by itself`)
listCmd.Flags().StringSliceVarP(&flags.Vars, "var", "", []string{}, `set var to runbook ("key:value")`)
listCmd.Flags().StringSliceVarP(&flags.Runners, "runner", "", []string{}, `set runner to runbook ("key:dsn")`)
listCmd.Flags().StringSliceVarP(&flags.Overlays, "overlay", "", []string{}, "overlay values on the runbook")
listCmd.Flags().StringSliceVarP(&flags.Underlays, "underlay", "", []string{}, "lay values under the runbook")
listCmd.Flags().IntVarP(&flags.Sample, "sample", "", 0, "run the specified number of runbooks at random")
listCmd.Flags().StringVarP(&flags.Shuffle, "shuffle", "", "off", `randomize the order of running runbooks ("on","off",N)`)
}
27 changes: 13 additions & 14 deletions cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ var newCmd = &cobra.Command{
al = [][]string{args}
}
ctx := context.Background()
rb := runn.NewRunbook(desc)
if out != "" {
p := filepath.Clean(out)
rb := runn.NewRunbook(flags.Desc)
if flags.Out != "" {
p := filepath.Clean(flags.Out)
if _, err := os.Stat(p); err == nil {
f, err := os.Open(p)
if err != nil {
Expand All @@ -70,8 +70,8 @@ var newCmd = &cobra.Command{
if err != nil {
return err
}
if desc != "" {
rb.Desc = desc
if flags.Desc != "" {
rb.Desc = flags.Desc
}
}
}
Expand All @@ -80,10 +80,10 @@ var newCmd = &cobra.Command{
return err
}
}
if out == "" {
if flags.Out == "" {
o = os.Stdout
} else {
o, err = os.Create(filepath.Clean(out))
o, err = os.Create(filepath.Clean(flags.Out))
if err != nil {
return err
}
Expand All @@ -103,7 +103,7 @@ var newCmd = &cobra.Command{
return nil
}

if andRun {
if flags.AndRun {
if err := runAndCapture(ctx, o, fn); err != nil {
return err
}
Expand All @@ -119,10 +119,10 @@ var newCmd = &cobra.Command{

func init() {
rootCmd.AddCommand(newCmd)
newCmd.Flags().StringVarP(&desc, "desc", "", "", "description of runbook")
newCmd.Flags().StringVarP(&out, "out", "", "", "target path of runbook")
newCmd.Flags().BoolVarP(&andRun, "and-run", "", false, "run created runbook and capture the response for test")
newCmd.Flags().BoolVarP(&grpcNoTLS, "grpc-no-tls", "", false, "disable TLS use in all gRPC runners")
newCmd.Flags().StringVarP(&flags.Desc, "desc", "", "", "description of runbook")
newCmd.Flags().StringVarP(&flags.Out, "out", "", "", "target path of runbook")
newCmd.Flags().BoolVarP(&flags.AndRun, "and-run", "", false, "run created runbook and capture the response for test")
newCmd.Flags().BoolVarP(&flags.GRPCNoTLS, "grpc-no-tls", "", false, "disable TLS use in all gRPC runners")
}

func runAndCapture(ctx context.Context, o *os.File, fn func(*os.File) error) error {
Expand Down Expand Up @@ -150,9 +150,8 @@ func runAndCapture(ctx context.Context, o *os.File, fn func(*os.File) error) err
opts := []runn.Option{
runn.Book(tf.Name()),
runn.Capture(capture.Runbook(td, capture.RunbookLoadDesc(true))),
runn.GRPCNoTLS(grpcNoTLS),
runn.GRPCNoTLS(flags.GRPCNoTLS),
}

oo, err := runn.New(opts...)
if err != nil {
return err
Expand Down
149 changes: 132 additions & 17 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,24 @@ THE SOFTWARE.
package cmd

import (
"errors"
"fmt"
"os"
"regexp"
"runtime"
"sort"
"strconv"
"strings"
"time"

"github.com/k1LoW/runn"
"github.com/k1LoW/runn/capture"
"github.com/k1LoW/runn/version"
"github.com/spf13/cast"
"github.com/spf13/cobra"
)

var (
debug bool
failFast bool
skipTest bool
skipIncluded bool
grpcNoTLS bool
captureDir string
vars []string
overlays []string
underlays []string
sample int
shuffle string
parallel string
desc string
out string
andRun bool
)
var flags = &Flags{}

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Expand All @@ -61,3 +56,123 @@ func Execute() {
os.Exit(1)
}
}

var intRe = regexp.MustCompile(`^\-?[0-9]+$`)
var floatRe = regexp.MustCompile(`^\-?[0-9.]+$`)

type Flags struct {
Debug bool
FailFast bool
SkipTest bool
SkipIncluded bool
GRPCNoTLS bool
CaptureDir string
Vars []string
Runners []string
Overlays []string
Underlays []string
Sample int
Shuffle string
Parallel string
Desc string
Out string
AndRun bool
}

func (f *Flags) ToOpts() ([]runn.Option, error) {
const (
on = "on"
off = "off"
keyValueSep = ":"
keysSep = "."
)
opts := []runn.Option{
runn.Debug(f.Debug),
runn.SkipTest(f.SkipTest),
runn.SkipIncluded(f.SkipIncluded),
runn.GRPCNoTLS(f.GRPCNoTLS),
runn.Capture(runn.NewCmdOut(os.Stdout)),
}
if f.Sample > 0 {
opts = append(opts, runn.RunSample(f.Sample))
}
if f.Shuffle != "" {
switch {
case f.Shuffle == on:
opts = append(opts, runn.RunShuffle(true, time.Now().UnixNano()))
case f.Shuffle == off:
default:
seed, err := strconv.ParseInt(f.Shuffle, 10, 64)
if err != nil {
return nil, errors.New(`should be "on", "off" or number for seed: --shuffle`)
}
opts = append(opts, runn.RunShuffle(true, seed))
}
}
if f.Parallel != "" {
switch {
case f.Parallel == on:
opts = append(opts, runn.RunParallel(true, int64(runtime.GOMAXPROCS(0))))
case f.Parallel == off:
default:
max, err := strconv.ParseInt(f.Parallel, 10, 64)
if err != nil {
return nil, errors.New(`should be "on", "off" or number for seed: --parallel`)
}
opts = append(opts, runn.RunParallel(true, max))
}
}

for _, v := range f.Vars {
splitted := strings.Split(v, keyValueSep)
if len(splitted) < 2 {
return nil, fmt.Errorf("invalid var: %s", v)
}
vk := strings.Split(splitted[0], keysSep)
vv := strings.Join(splitted[1:], keyValueSep)
switch {
case intRe.MatchString(vv):
vvv, err := cast.ToIntE(vv)
if err == nil {
opts = append(opts, runn.Var(vk, vvv))
continue
}
case floatRe.MatchString(vv):
vvv, err := cast.ToFloat64E(vv)
if err == nil {
opts = append(opts, runn.Var(vk, vvv))
continue
}
}
opts = append(opts, runn.Var(vk, vv))
}
for _, v := range f.Runners {
splitted := strings.Split(v, keyValueSep)
if len(splitted) < 2 {
return nil, fmt.Errorf("invalid var: %s", v)
}
vk := splitted[0]
vv := strings.Join(splitted[1:], keyValueSep)
opts = append(opts, runn.Runner(vk, vv))
}
for _, o := range f.Overlays {
opts = append(opts, runn.Overlay(o))
}
sort.SliceStable(f.Underlays, func(i, j int) bool {
return i > j
})
for _, u := range f.Underlays {
opts = append(opts, runn.Underlay(u))
}
if f.CaptureDir != "" {
fi, err := os.Stat(f.CaptureDir)
if err != nil {
return nil, err
}
if !fi.IsDir() {
return nil, fmt.Errorf("%s is not directory", f.CaptureDir)
}
opts = append(opts, runn.Capture(capture.Runbook(f.CaptureDir)))
}
return opts, nil
}
Loading

0 comments on commit 6c4ae41

Please sign in to comment.