Skip to content

Commit

Permalink
Move everything to one command based on feedback
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel González Lopes <[email protected]>
  • Loading branch information
dgzlopes committed Jan 3, 2025
1 parent fea3e7f commit b1af40f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 104 deletions.
6 changes: 1 addition & 5 deletions cmd/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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())
Expand Down
97 changes: 0 additions & 97 deletions cmd/cloud_new.go

This file was deleted.

11 changes: 9 additions & 2 deletions cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ type newScriptCmd struct {
gs *state.GlobalState
overwriteFiles bool
templateType string
projectID string
}

func (c *newScriptCmd) 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 project ID for the test")
return flags
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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]",
Expand Down
25 changes: 25 additions & 0 deletions cmd/new_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

0 comments on commit b1af40f

Please sign in to comment.