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

Move cloud run and login functionalities under cloud subcommands #3813

Merged
merged 11 commits into from
Jul 24, 2024
Prev Previous commit
Next Next commit
Register run and login subcommands of the cloud command
  • Loading branch information
oleiade committed Jul 23, 2024
commit 42f9de46642fe4f9ebafe7944fdc2de92d5ff803
60 changes: 55 additions & 5 deletions cmd/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type cmdCloud struct {
uploadOnly bool
}

//nolint:dupl // remove this statement once the migration from the `k6 cloud` to the `k6 cloud run` is complete.
oleiade marked this conversation as resolved.
Show resolved Hide resolved
func (c *cmdCloud) preRun(cmd *cobra.Command, _ []string) error {
// TODO: refactor (https://github.com/loadimpact/k6/issues/883)
//
Expand Down Expand Up @@ -117,7 +118,7 @@ func (c *cmdCloud) run(cmd *cobra.Command, args []string) error {
return err
}
if !cloudConfig.Token.Valid {
return errors.New("Not logged in, please use `k6 login cloud`.") //nolint:golint,revive,stylecheck
return errors.New("Not logged in, please use `k6 cloud login`") //nolint:golint,stylecheck
}

// Display config warning if needed
Expand Down Expand Up @@ -343,20 +344,69 @@ func getCmdCloud(gs *state.GlobalState) *cobra.Command {
}

exampleText := getExampleText(gs, `
{{.}} cloud script.js`[1:])
# Authenticate with Grafana k6 Cloud
$ {{.}} cloud login

# Run a k6 script in the Grafana k6 cloud
$ {{.}} cloud run script.js

# Run a k6 archive in the Grafana k6 cloud
$ {{.}} cloud run archive.tar

# [deprecated] Run a k6 script in the Grafana k6 cloud
$ {{.}} cloud script.js

# [deprecated] Run a k6 archive in the Grafana k6 cloud
$ {{.}} cloud archive.tar`[1:])

cloudCmd := &cobra.Command{
Use: "cloud",
Short: "Run a test on the cloud",
Long: `Run a test on the cloud.
Long: `[deprecation notice]
oleiade marked this conversation as resolved.
Show resolved Hide resolved
The k6 team is in the process of modifying and deprecating the cloud command behavior. In the future, the "cloud"
oleiade marked this conversation as resolved.
Show resolved Hide resolved
command will only display a help text, instead of running tests in the cloud.
To run tests in the cloud, users are now invited to migrate to the "k6 cloud run" command instead.

Run a test on the cloud.
oleiade marked this conversation as resolved.
Show resolved Hide resolved
oleiade marked this conversation as resolved.
Show resolved Hide resolved

This will execute the test on the k6 cloud service. Use "k6 login cloud" to authenticate.`,
oleiade marked this conversation as resolved.
Show resolved Hide resolved
Example: exampleText,
Args: exactArgsWithMsg(1, "arg should either be \"-\", if reading script from stdin, or a path to a script file"),
Args: exactCloudArgs(),
PreRunE: c.preRun,
RunE: c.run,
Example: exampleText,
}

// Register `k6 cloud` subcommands
cloudCmd.AddCommand(getCmdCloudRun(gs))
cloudCmd.AddCommand(getCmdCloudLogin(gs))

cloudCmd.Flags().SortFlags = false
cloudCmd.Flags().AddFlagSet(c.flagSet())

return cloudCmd
}

func exactCloudArgs() cobra.PositionalArgs {
return func(_ *cobra.Command, args []string) error {
if len(args) < 1 || len(args) > 2 {
oleiade marked this conversation as resolved.
Show resolved Hide resolved
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"
oleiade marked this conversation as resolved.
Show resolved Hide resolved
)

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])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

return nil
}
}