Skip to content

Commit

Permalink
Apply Pull-Request suggestions
Browse files Browse the repository at this point in the history
Co-authored-by: Joan López de la Franca Beltran <[email protected]>
  • Loading branch information
oleiade and joanlopez committed Jul 18, 2024
1 parent c325f05 commit ddce8cb
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 51 deletions.
34 changes: 11 additions & 23 deletions cmd/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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,
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions cmd/cloud_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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")
}

Expand Down
27 changes: 16 additions & 11 deletions cmd/cloud_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 <YOUR_API_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,
}
Expand Down Expand Up @@ -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",
)
}
Expand Down
14 changes: 0 additions & 14 deletions cmd/tests/cmd_cloud_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
//}

0 comments on commit ddce8cb

Please sign in to comment.