Skip to content

Commit

Permalink
Flatten some long functions in cmd/ and enable splitting them apart
Browse files Browse the repository at this point in the history
  • Loading branch information
na-- committed Mar 8, 2022
1 parent 207ef83 commit f82b364
Show file tree
Hide file tree
Showing 15 changed files with 588 additions and 543 deletions.
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 := loadTest(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 := loadTest(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

0 comments on commit f82b364

Please sign in to comment.