Skip to content

Commit

Permalink
Rename k6 init to k6 new
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewslotin committed Nov 6, 2023
1 parent 5c11c29 commit 6b91c91
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
26 changes: 13 additions & 13 deletions cmd/init.go → cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
const defaultNewScriptName = "script.js"

//nolint:gochecknoglobals
var defaultNewScriptTemplate = template.Must(template.New("init").Parse(`import http from 'k6/http';
var defaultNewScriptTemplate = template.Must(template.New("new").Parse(`import http from 'k6/http';
import { sleep } from 'k6';
export const options = {
Expand Down Expand Up @@ -82,21 +82,21 @@ type initScriptTemplateArgs struct {
ScriptName string
}

// initCmd represents the `k6 init` command
type initCmd struct {
// newScriptCmd represents the `k6 new` command
type newScriptCmd struct {
gs *state.GlobalState
overwriteFiles bool
}

func (c *initCmd) flagSet() *pflag.FlagSet {
func (c *newScriptCmd) flagSet() *pflag.FlagSet {
flags := pflag.NewFlagSet("", pflag.ContinueOnError)
flags.SortFlags = false
flags.BoolVarP(&c.overwriteFiles, "force", "f", false, "Overwrite existing files")

return flags
}

func (c *initCmd) run(cmd *cobra.Command, args []string) error { //nolint:revive
func (c *newScriptCmd) run(cmd *cobra.Command, args []string) error { //nolint:revive
target := defaultNewScriptName
if len(args) > 0 {
target = args[0]
Expand Down Expand Up @@ -136,23 +136,23 @@ func (c *initCmd) run(cmd *cobra.Command, args []string) error { //nolint:revive
return nil
}

func getCmdInit(gs *state.GlobalState) *cobra.Command {
c := &initCmd{gs: gs}
func getCmdNewScript(gs *state.GlobalState) *cobra.Command {
c := &newScriptCmd{gs: gs}

exampleText := getExampleText(gs, `
# Create a minimal k6 script in the current directory. By default, k6 creates script.js.
{{.}} init
{{.}} new
# Create a minimal k6 script in the current directory and store it in test.js
{{.}} init test.js
{{.}} new test.js
# Overwrite existing test.js with a minimal k6 script
{{.}} init -f test.js`[1:])
{{.}} new -f test.js`[1:])

initCmd := &cobra.Command{
Use: "init",
Short: "Initialize a new k6 script",
Long: `Initialize a new k6 script.
Use: "new",
Short: "Create and initialize a new k6 script",
Long: `Create and initialize a new k6 script.
This command will create a minimal k6 script in the current directory and
store it in the file specified by the first argument. If no argument is
Expand Down
12 changes: 6 additions & 6 deletions cmd/init_test.go → cmd/new_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"go.k6.io/k6/lib/fsext"
)

func TestInitScript(t *testing.T) {
func TestNewScriptCmd(t *testing.T) {
t.Parallel()

testCases := []struct {
Expand Down Expand Up @@ -44,7 +44,7 @@ func TestInitScript(t *testing.T) {
t.Parallel()

ts := tests.NewGlobalTestState(t)
ts.CmdArgs = []string{"k6", "init"}
ts.CmdArgs = []string{"k6", "new"}
if testCase.scriptNameArg != "" {
ts.CmdArgs = append(ts.CmdArgs, testCase.scriptNameArg)
}
Expand All @@ -62,13 +62,13 @@ func TestInitScript(t *testing.T) {
}
}

func TestInitScript_FileExists_NoOverwrite(t *testing.T) {
func TestNewScriptCmd_FileExists_NoOverwrite(t *testing.T) {
t.Parallel()

ts := tests.NewGlobalTestState(t)
require.NoError(t, fsext.WriteFile(ts.FS, defaultNewScriptName, []byte("untouched"), 0o644))

ts.CmdArgs = []string{"k6", "init"}
ts.CmdArgs = []string{"k6", "new"}
ts.ExpectedExitCode = -1

newRootCommand(ts.GlobalState).execute()
Expand All @@ -79,13 +79,13 @@ func TestInitScript_FileExists_NoOverwrite(t *testing.T) {
assert.Contains(t, string(data), "untouched")
}

func TestInitScript_FileExists_Overwrite(t *testing.T) {
func TestNewScriptCmd_FileExists_Overwrite(t *testing.T) {
t.Parallel()

ts := tests.NewGlobalTestState(t)
require.NoError(t, fsext.WriteFile(ts.FS, defaultNewScriptName, []byte("untouched"), 0o644))

ts.CmdArgs = []string{"k6", "init", "-f"}
ts.CmdArgs = []string{"k6", "new", "-f"}

newRootCommand(ts.GlobalState).execute()

Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func newRootCommand(gs *state.GlobalState) *rootCommand {
rootCmd.SetIn(gs.Stdin)

subCommands := []func(*state.GlobalState) *cobra.Command{
getCmdArchive, getCmdCloud, getCmdInit, getCmdInspect,
getCmdArchive, getCmdCloud, getCmdNewScript, getCmdInspect,
getCmdLogin, getCmdPause, getCmdResume, getCmdScale, getCmdRun,
getCmdStats, getCmdStatus, getCmdVersion,
}
Expand Down

0 comments on commit 6b91c91

Please sign in to comment.