From ddce8cbb5d3ac9d414652c94e82c8457724770d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Crevon?= Date: Wed, 17 Jul 2024 09:20:43 +0200 Subject: [PATCH] Apply Pull-Request suggestions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Joan López de la Franca Beltran <5459617+joanlopez@users.noreply.github.com> --- cmd/cloud.go | 34 +++++++++++----------------------- cmd/cloud_login.go | 6 +++--- cmd/cloud_run.go | 27 ++++++++++++++++----------- cmd/tests/cmd_cloud_test.go | 14 -------------- 4 files changed, 30 insertions(+), 51 deletions(-) diff --git a/cmd/cloud.go b/cmd/cloud.go index 548f23f89a30..d87ca5770b12 100644 --- a/cmd/cloud.go +++ b/cmd/cloud.go @@ -344,19 +344,19 @@ func getCmdCloud(gs *state.GlobalState) *cobra.Command { } exampleText := getExampleText(gs, ` - # Authenticate with Grafana k6 Cloud + # Authenticate with Grafana Cloud k6 $ {{.}} cloud login - # Run a k6 script in the Grafana k6 cloud + # Run a k6 script in the Grafana Cloud k6 $ {{.}} cloud run script.js - # Run a k6 archive in the Grafana k6 cloud + # Run a k6 archive in the Grafana Cloud k6 $ {{.}} cloud run archive.tar - # [deprecated] Run a k6 script in the Grafana k6 cloud + # [deprecated] Run a k6 script in the Grafana Cloud k6 $ {{.}} cloud script.js - # [deprecated] Run a k6 archive in the Grafana k6 cloud + # [deprecated] Run a k6 archive in the Grafana Cloud k6 $ {{.}} cloud archive.tar`[1:]) cloudCmd := &cobra.Command{ @@ -369,7 +369,7 @@ To run tests in the cloud, users are now invited to migrate to the "k6 cloud run Run a test on the cloud. -This will execute the test on the k6 cloud service. Use "k6 login cloud" to authenticate.`, +This will execute the test on the k6 cloud service. Use "k6 cloud login" to authenticate.`, Args: exactCloudArgs(), PreRunE: c.preRun, RunE: c.run, @@ -388,23 +388,11 @@ This will execute the test on the k6 cloud service. Use "k6 login cloud" to auth func exactCloudArgs() cobra.PositionalArgs { return func(_ *cobra.Command, args []string) error { - if len(args) < 1 || len(args) > 2 { - return fmt.Errorf("accepts 1 or 2 arg(s), received %d", len(args)) - } - - var ( - isRunSubcommand = args[0] == "run" - isLoginSubcommand = args[0] == "login" - isScript = filepath.Ext(args[0]) == ".js" - isArchive = filepath.Ext(args[0]) == ".tar" - ) - - if len(args) == 1 && !isScript && !isArchive { - return fmt.Errorf("unexpected argument: %s", args[0]) - } - - if len(args) == 2 && !isRunSubcommand && !isLoginSubcommand { - return fmt.Errorf("unexpected argument: %s", args[0]) + if len(args) == 0 { + return fmt.Errorf( + "the k6 cloud command accepts 1 argument consisting in either in "+ + "a subcommand such as `run` or `cloud`, or the path to a script/archive, or "+ + "the `-` symbol, received: %d arguments instead", len(args)) } return nil diff --git a/cmd/cloud_login.go b/cmd/cloud_login.go index 3ca508e6d19e..e75734460fa6 100644 --- a/cmd/cloud_login.go +++ b/cmd/cloud_login.go @@ -41,10 +41,10 @@ func getCmdCloudLogin(gs *state.GlobalState) *cobra.Command { loginCloudCommand := &cobra.Command{ Use: cloudLoginCommandName, - Short: "Authenticate with Grafana k6 Cloud", + Short: "Authenticate with Grafana Cloud k6", Long: `Authenticate with Grafana Cloud k6. -This command will authenticate you with Grafana Cloud k6. Once authenticated, +This command will authenticate you with Grafana Cloud k6. Once authenticated you can start running tests in the cloud by using the "k6 cloud" command, or by executing a test locally and outputting samples to the cloud using the "k6 run -o cloud" command. @@ -144,7 +144,7 @@ func (c *cmdCloudLogin) run(cmd *cobra.Command, _ []string) error { if res.Token == "" { return errors.New("your account does not appear to have an active API token, please consult the " + - "Grafana k6 cloud documentation for instructions on how to generate " + + "Grafana Cloud k6 documentation for instructions on how to generate " + "one: https://grafana.com/docs/grafana-cloud/testing/k6/author-run/tokens-and-cli-authentication") } diff --git a/cmd/cloud_run.go b/cmd/cloud_run.go index 9fab789f2c5f..e987d4307586 100644 --- a/cmd/cloud_run.go +++ b/cmd/cloud_run.go @@ -24,7 +24,7 @@ import ( "go.k6.io/k6/ui/pb" ) -// cmdCloudRun handles the `k6 cloud` sub-command +// cmdCloudRun handles the `k6 cloud run` sub-command type cmdCloudRun struct { gs *state.GlobalState @@ -42,22 +42,27 @@ func getCmdCloudRun(gs *state.GlobalState) *cobra.Command { } exampleText := getExampleText(gs, ` - # Run a test in the Grafana k6 cloud + # Run a test script in Grafana Cloud k6 $ {{.}} cloud run script.js - # Run a test in the Grafana k6 cloud with a specific token - $ {{.}} cloud run --token script.js`[1:]) + # Run a test archive in Grafana Cloud k6 + $ {{.}} cloud run archive.tar + + # Read a test script or archive from stdin and run it in Grafana Cloud k6 + $ {{.}} cloud run - < script.js`[1:]) - // FIXME: when the command is "k6 cloud run" without an script/archive, we should display an error and the help cloudRunCmd := &cobra.Command{ Use: cloudRunCommandName, - Short: "Run a test in the Grafana k6 cloud", - Long: `Run a test in the Grafana k6 cloud. + Short: "Run a test in Grafana Cloud k6", + Long: `Run a test in Grafana Cloud k6. -This will execute the test in the Grafana k6 cloud service. Using this command requires to be authenticated -against the Grafana k6 cloud. Use the "k6 cloud login" command to authenticate.`, +This will execute the test in the Grafana Cloud k6 service. Using this command requires to be authenticated +against Grafana Cloud k6. Use the "k6 cloud login" command to authenticate.`, Example: exampleText, - Args: exactArgsWithMsg(1, "arg should either be \"-\", if reading script from stdin, or a path to a script file"), + Args: exactArgsWithMsg(1, + "the k6 cloud run command expects a single argument consisting in either a path to a script or "+ + "archive file, or the \"-\" symbol indicating the script or archive should be read from stdin", + ), PreRunE: c.preRun, RunE: c.run, } @@ -154,7 +159,7 @@ func (c *cmdCloudRun) run(cmd *cobra.Command, args []string) error { } if !cloudConfig.Token.Valid { return errors.New( //nolint:golint - "not logged in, please login to the Grafana k6 Cloud " + + "not logged in, please login to the Grafana Cloud k6 " + "using the `k6 cloud login` command", ) } diff --git a/cmd/tests/cmd_cloud_test.go b/cmd/tests/cmd_cloud_test.go index 1032906da560..a8e76e38006f 100644 --- a/cmd/tests/cmd_cloud_test.go +++ b/cmd/tests/cmd_cloud_test.go @@ -288,17 +288,3 @@ func TestCloudWithArchive(t *testing.T) { assert.Contains(t, stdout, `output: https://app.k6.io/runs/123`) assert.Contains(t, stdout, `test status: Finished`) } - -// FIXME: This test fails because our test setup leaves stdout empty -//func TestCloudArgs(t *testing.T) { -// t.Parallel() -// -// // ts := NewGlobalTestState(t) -// ts := getSimpleCloudTestState(t, nil, nil, nil, nil) -// ts.CmdArgs = []string{"k6", "cloud", "run"} -// ts.ExpectedExitCode = -1 -// cmd.ExecuteWithGlobalState(ts.GlobalState) -// -// stdout := ts.Stdout.String() -// assert.Contains(t, stdout, `accepts 1 or 2 arg(s), received 0`) -//}