Skip to content

Commit

Permalink
cmd: small refactoring with existing func
Browse files Browse the repository at this point in the history
  • Loading branch information
yorugac committed Dec 15, 2021
1 parent bedba0f commit 6e68f3d
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 35 deletions.
12 changes: 2 additions & 10 deletions cmd/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,12 @@ import (
"github.com/spf13/pflag"

"go.k6.io/k6/lib/metrics"
"go.k6.io/k6/loader"
)

var archiveOut = "archive.tar"

func getArchiveCmd(logger *logrus.Logger) *cobra.Command {
// archiveCmd represents the pause command
// archiveCmd represents the archive command
archiveCmd := &cobra.Command{
Use: "archive",
Short: "Create an archive",
Expand All @@ -50,14 +49,7 @@ An archive is a fully self-contained test run, and can be executed identically e
k6 run myarchive.tar`[1:],
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
// Runner.
pwd, err := os.Getwd()
if err != nil {
return err
}
filename := args[0]
filesystems := loader.CreateFilesystems()
src, err := loader.ReadSource(logger, filename, pwd, filesystems, os.Stdin)
src, filesystems, err := readSource(args[0], logger)
if err != nil {
return err
}
Expand Down
9 changes: 1 addition & 8 deletions cmd/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ import (
"go.k6.io/k6/lib"
"go.k6.io/k6/lib/consts"
"go.k6.io/k6/lib/metrics"
"go.k6.io/k6/loader"
"go.k6.io/k6/ui/pb"
)

Expand Down Expand Up @@ -90,14 +89,8 @@ This will execute the test on the k6 cloud service. Use "k6 login cloud" to auth
printBar(progressBar)

// Runner
pwd, err := os.Getwd()
if err != nil {
return err
}

filename := args[0]
filesystems := loader.CreateFilesystems()
src, err := loader.ReadSource(logger, filename, pwd, filesystems, os.Stdin)
src, filesystems, err := readSource(filename, logger)
if err != nil {
return err
}
Expand Down
2 changes: 2 additions & 0 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ func exactArgsWithMsg(n int, msg string) cobra.PositionalArgs {
}
}

// readSource is a small wrapper around loader.ReadSource returning
// result of the load and filesystems map
func readSource(filename string, logger *logrus.Logger) (*loader.SourceData, map[string]afero.Fs, error) {
pwd, err := os.Getwd()
if err != nil {
Expand Down
1 change: 0 additions & 1 deletion cmd/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ func getInspectCmd(logger *logrus.Logger) *cobra.Command {
func addExecRequirements(b *js.Bundle,
builtinMetrics *metrics.BuiltinMetrics, registry *metrics.Registry,
logger *logrus.Logger) (interface{}, error) {

// TODO: after #1048 issue, consider rewriting this without a Runner:
// just creating ExecutionPlan directly from validated options

Expand Down
18 changes: 2 additions & 16 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
package cmd

import (
"archive/tar"
"bytes"
"context"
"encoding/json"
Expand Down Expand Up @@ -99,13 +98,7 @@ a commandline interface for interacting with it.`,
logger.Debug("Initializing the runner...")

// Create the Runner.
pwd, err := os.Getwd()
if err != nil {
return err
}
filename := args[0]
filesystems := loader.CreateFilesystems()
src, err := loader.ReadSource(logger, filename, pwd, filesystems, os.Stdin)
src, filesystems, err := readSource(args[0], logger)
if err != nil {
return err
}
Expand Down Expand Up @@ -226,7 +219,7 @@ a commandline interface for interacting with it.`,
defer engine.StopOutputs()

printExecutionDescription(
"local", filename, "", conf, execScheduler.GetState().ExecutionTuple,
"local", args[0], "", conf, execScheduler.GetState().ExecutionTuple,
executionPlan, outputs, noColor || !stdoutTTY)

// Trap Interrupts, SIGINTs and SIGTERMs.
Expand Down Expand Up @@ -415,13 +408,6 @@ func newRunner(
return runner, err
}

func detectType(data []byte) string {
if _, err := tar.NewReader(bytes.NewReader(data)).Next(); err == nil {
return typeArchive
}
return typeJS
}

func handleSummaryResult(fs afero.Fs, stdOut, stdErr io.Writer, result map[string]io.Reader) error {
var errs []error

Expand Down

0 comments on commit 6e68f3d

Please sign in to comment.