From b1af40f20fc82cb228cb0dce071eebc110e842b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Gonz=C3=A1lez=20Lopes?= Date: Fri, 3 Jan 2025 13:16:42 +0100 Subject: [PATCH] Move everything to one command based on feedback MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Daniel González Lopes --- cmd/cloud.go | 6 +-- cmd/cloud_new.go | 97 ------------------------------------------------ cmd/new.go | 11 +++++- cmd/new_test.go | 25 +++++++++++++ 4 files changed, 35 insertions(+), 104 deletions(-) delete mode 100644 cmd/cloud_new.go diff --git a/cmd/cloud.go b/cmd/cloud.go index 91bc3ece08f..9c189606c04 100644 --- a/cmd/cloud.go +++ b/cmd/cloud.go @@ -371,10 +371,7 @@ func getCmdCloud(gs *state.GlobalState) *cobra.Command { $ {{.}} cloud run script.js # Run a k6 archive in the Grafana Cloud k6 - $ {{.}} cloud run archive.tar - - # Create a new cloud-ready k6 script - $ {{.}} cloud new script.js`[1:]) + $ {{.}} cloud run archive.tar`[1:]) cloudCmd := &cobra.Command{ Use: "cloud", @@ -397,7 +394,6 @@ service. Be sure to run the "k6 cloud login" command prior to authenticate with cloudCmd.AddCommand(getCmdCloudRun(c)) cloudCmd.AddCommand(getCmdCloudLogin(gs)) cloudCmd.AddCommand(getCmdCloudUpload(c)) - cloudCmd.AddCommand(getCmdCloudNew(c)) cloudCmd.Flags().SortFlags = false cloudCmd.Flags().AddFlagSet(c.flagSet()) diff --git a/cmd/cloud_new.go b/cmd/cloud_new.go deleted file mode 100644 index 0447661e493..00000000000 --- a/cmd/cloud_new.go +++ /dev/null @@ -1,97 +0,0 @@ -package cmd - -import ( - "fmt" - - templates "go.k6.io/k6/cmd/newtemplates" - "go.k6.io/k6/cmd/state" - "go.k6.io/k6/lib/fsext" - - "github.com/spf13/cobra" - "github.com/spf13/pflag" -) - -const defaultNewCloudScriptName = "script.js" - -type cmdCloudNew struct { - gs *state.GlobalState - overwriteFiles bool - templateType string - projectID string -} - -func (c *cmdCloudNew) flagSet() *pflag.FlagSet { - flags := pflag.NewFlagSet("", pflag.ContinueOnError) - flags.SortFlags = false - flags.BoolVarP(&c.overwriteFiles, "force", "f", false, "overwrite existing files") - flags.StringVar(&c.templateType, "template", "minimal", "template type (choices: minimal, protocol, browser)") - flags.StringVar(&c.projectID, "project-id", "", "specify the Grafana Cloud ProjectID for the test") - return flags -} - -func (c *cmdCloudNew) run(cmd *cobra.Command, args []string) error { - target := defaultNewCloudScriptName - if len(args) > 0 { - target = args[0] - } - - fileExists, err := fsext.Exists(c.gs.FS, target) - if err != nil { - return err - } - if fileExists && !c.overwriteFiles { - return fmt.Errorf("%s already exists. Use the `--force` flag to overwrite", target) - } - - fd, err := c.gs.FS.Create(target) - if err != nil { - return err - } - defer fd.Close() - - tmpl, err := templates.GetTemplate(c.templateType) - if err != nil { - return err - } - - argsStruct := templates.TemplateArgs{ - ScriptName: target, - EnableCloud: true, - ProjectID: c.projectID, - } - - if err := templates.ExecuteTemplate(fd, tmpl, argsStruct); err != nil { - return err - } - - fmt.Fprintf(c.gs.Stdout, "New cloud script created: %s (%s template).\n", target, c.templateType) - return nil -} - -func getCmdCloudNew(cloudCmd *cmdCloud) *cobra.Command { - c := &cmdCloudNew{gs: cloudCmd.gs} - - exampleText := getExampleText(cloudCmd.gs, ` - # Create a minimal cloud-ready script - $ {{.}} cloud new --template minimal - - # Create a protocol-based cloud script with a specific ProjectID - $ {{.}} cloud new --template protocol --project-id my-project-id - - # Overwrite an existing file with a browser-based cloud script - $ {{.}} cloud new -f --template browser test.js --project-id my-project-id`[1:]) - - initCmd := &cobra.Command{ - Use: "new [file]", - Short: "Create a new k6 script", - Long: `Create a new cloud-ready k6 script using one of the predefined templates. - -By default, the script will be named script.js unless a different name is specified.`, - Example: exampleText, - Args: cobra.MaximumNArgs(1), - RunE: c.run, - } - - initCmd.Flags().AddFlagSet(c.flagSet()) - return initCmd -} diff --git a/cmd/new.go b/cmd/new.go index 531b20d782a..fb27e3bd890 100644 --- a/cmd/new.go +++ b/cmd/new.go @@ -16,6 +16,7 @@ type newScriptCmd struct { gs *state.GlobalState overwriteFiles bool templateType string + projectID string } func (c *newScriptCmd) flagSet() *pflag.FlagSet { @@ -23,6 +24,7 @@ func (c *newScriptCmd) flagSet() *pflag.FlagSet { flags.SortFlags = false flags.BoolVarP(&c.overwriteFiles, "force", "f", false, "overwrite existing files") flags.StringVar(&c.templateType, "template", "minimal", "template type (choices: minimal, protocol, browser)") + flags.StringVar(&c.projectID, "project-id", "", "specify the Grafana Cloud project ID for the test") return flags } @@ -52,7 +54,9 @@ func (c *newScriptCmd) run(cmd *cobra.Command, args []string) error { } argsStruct := templates.TemplateArgs{ - ScriptName: target, + ScriptName: target, + EnableCloud: c.projectID != "", + ProjectID: c.projectID, } if err := templates.ExecuteTemplate(fd, tmpl, argsStruct); err != nil { @@ -71,7 +75,10 @@ func getCmdNewScript(gs *state.GlobalState) *cobra.Command { $ {{.}} new --template minimal # Overwrite an existing file with a protocol-based script - $ {{.}} new -f --template protocol test.js`[1:]) + $ {{.}} new -f --template protocol test.js + + # Create a cloud-ready script with a specific project ID + $ {{.}} new --project-id 12315`[1:]) initCmd := &cobra.Command{ Use: "new [file]", diff --git a/cmd/new_test.go b/cmd/new_test.go index 5f955a0b423..2a0e6fd05fd 100644 --- a/cmd/new_test.go +++ b/cmd/new_test.go @@ -93,3 +93,28 @@ func TestNewScriptCmd_FileExists_Overwrite(t *testing.T) { assert.Contains(t, string(data), "export const options = {") assert.Contains(t, string(data), "export default function() {") } + +func TestNewScriptCmd_InvalidTemplateType(t *testing.T) { + t.Parallel() + + ts := tests.NewGlobalTestState(t) + ts.CmdArgs = []string{"k6", "new", "--template", "invalid-template"} + + ts.ExpectedExitCode = -1 + + newRootCommand(ts.GlobalState).execute() + assert.Contains(t, ts.Stderr.String(), "invalid template type") +} +func TestNewScriptCmd_ProjectID(t *testing.T) { + t.Parallel() + + ts := tests.NewGlobalTestState(t) + ts.CmdArgs = []string{"k6", "new", "--project-id", "1422"} + + newRootCommand(ts.GlobalState).execute() + + data, err := fsext.ReadFile(ts.FS, defaultNewScriptName) + require.NoError(t, err) + + assert.Contains(t, string(data), "projectID: 1422") +}