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

Flatten some long functions in cmd/ and enable splitting them apart #2418

Merged
merged 1 commit into from
Mar 18, 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
96 changes: 54 additions & 42 deletions cmd/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,58 @@ import (
"github.com/spf13/pflag"
)

func getArchiveCmd(gs *globalState) *cobra.Command {
archiveOut := "archive.tar"
// archiveCmd represents the archive command
// cmdArchive handles the `k6 archive` sub-command
type cmdArchive struct {
gs *globalState

archiveOut string
}

func (c *cmdArchive) run(cmd *cobra.Command, args []string) error {
test, err := loadAndConfigureTest(c.gs, cmd, args, getPartialConfig)
if err != nil {
return err
}

// It's important to NOT set the derived options back to the runner
// here, only the consolidated ones. Otherwise, if the script used
// an execution shortcut option (e.g. `iterations` or `duration`),
// we will have multiple conflicting execution options since the
// derivation will set `scenarios` as well.
err = test.initRunner.SetOptions(test.consolidatedConfig.Options)
if err != nil {
return err
}

// Archive.
arc := test.initRunner.MakeArchive()
f, err := c.gs.fs.Create(c.archiveOut)
if err != nil {
return err
}

err = arc.Write(f)
if cerr := f.Close(); err == nil && cerr != nil {
err = cerr
}
return err
}

func (c *cmdArchive) flagSet() *pflag.FlagSet {
flags := pflag.NewFlagSet("", pflag.ContinueOnError)
flags.SortFlags = false
flags.AddFlagSet(optionFlagSet())
flags.AddFlagSet(runtimeOptionFlagSet(false))
flags.StringVarP(&c.archiveOut, "archive-out", "O", c.archiveOut, "archive output filename")
return flags
}

func getCmdArchive(gs *globalState) *cobra.Command {
c := &cmdArchive{
gs: gs,
archiveOut: "archive.tar",
}

archiveCmd := &cobra.Command{
Use: "archive",
Short: "Create an archive",
Expand All @@ -41,48 +90,11 @@ An archive is a fully self-contained test run, and can be executed identically e
# Run the resulting archive.
k6 run myarchive.tar`[1:],
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
test, err := loadAndConfigureTest(gs, cmd, args, getPartialConfig)
if err != nil {
return err
}

// It's important to NOT set the derived options back to the runner
// here, only the consolidated ones. Otherwise, if the script used
// an execution shortcut option (e.g. `iterations` or `duration`),
// we will have multiple conflicting execution options since the
// derivation will set `scenarios` as well.
err = test.initRunner.SetOptions(test.consolidatedConfig.Options)
if err != nil {
return err
}

// Archive.
arc := test.initRunner.MakeArchive()
f, err := gs.fs.Create(archiveOut)
if err != nil {
return err
}

err = arc.Write(f)
if cerr := f.Close(); err == nil && cerr != nil {
err = cerr
}
return err
},
RunE: c.run,
}

archiveCmd.Flags().SortFlags = false
archiveCmd.Flags().AddFlagSet(archiveCmdFlagSet(&archiveOut))
archiveCmd.Flags().AddFlagSet(c.flagSet())

return archiveCmd
}

func archiveCmdFlagSet(archiveOut *string) *pflag.FlagSet {
flags := pflag.NewFlagSet("", pflag.ContinueOnError)
flags.SortFlags = false
flags.AddFlagSet(optionFlagSet())
flags.AddFlagSet(runtimeOptionFlagSet(false))
flags.StringVarP(archiveOut, "archive-out", "O", *archiveOut, "archive output filename")
return flags
}
Loading