diff --git a/command/quota_init.go b/command/quota_init.go index a78a356c773b..89e3b93c54a4 100644 --- a/command/quota_init.go +++ b/command/quota_init.go @@ -27,10 +27,11 @@ type QuotaInitCommand struct { func (c *QuotaInitCommand) Help() string { helpText := ` -Usage: nomad quota init +Usage: nomad quota init Creates an example quota specification file that can be used as a starting - point to customize further. + point to customize further. If no filename is given, the default of "spec.hcl" + or "spec.json" will be used. Init Options: @@ -68,8 +69,8 @@ func (c *QuotaInitCommand) Run(args []string) int { // Check that we get no arguments args = flags.Args() - if l := len(args); l != 0 { - c.Ui.Error("This command takes no arguments") + if l := len(args); l > 1 { + c.Ui.Error("This command takes no arguments or one: ") c.Ui.Error(commandErrorText(c)) return 1 } @@ -80,6 +81,9 @@ func (c *QuotaInitCommand) Run(args []string) int { fileName = DefaultJsonQuotaInitName fileContent = defaultJsonQuotaSpec } + if len(args) == 1 { + fileName = args[0] + } // Check if the file already exists _, err := os.Stat(fileName) diff --git a/command/quota_init_test.go b/command/quota_init_test.go index 315b30c97763..889d66a877db 100644 --- a/command/quota_init_test.go +++ b/command/quota_init_test.go @@ -3,10 +3,10 @@ package command import ( "io/ioutil" "os" - "strings" "testing" "github.com/mitchellh/cli" + "github.com/stretchr/testify/require" ) func TestQuotaInitCommand_Implements(t *testing.T) { @@ -20,50 +20,47 @@ func TestQuotaInitCommand_Run_HCL(t *testing.T) { cmd := &QuotaInitCommand{Meta: Meta{Ui: ui}} // Fails on misuse - if code := cmd.Run([]string{"some", "bad", "args"}); code != 1 { - t.Fatalf("expect exit code 1, got: %d", code) - } - if out := ui.ErrorWriter.String(); !strings.Contains(out, commandErrorText(cmd)) { - t.Fatalf("expect help output, got: %s", out) - } + code := cmd.Run([]string{"some", "bad", "args"}) + require.Equal(t, 1, code) + require.Contains(t, ui.ErrorWriter.String(), commandErrorText(cmd)) ui.ErrorWriter.Reset() // Ensure we change the cwd back origDir, err := os.Getwd() - if err != nil { - t.Fatalf("err: %s", err) - } + require.NoError(t, err) defer os.Chdir(origDir) // Create a temp dir and change into it dir, err := ioutil.TempDir("", "nomad") - if err != nil { - t.Fatalf("err: %s", err) - } + require.NoError(t, err) defer os.RemoveAll(dir) - if err := os.Chdir(dir); err != nil { - t.Fatalf("err: %s", err) - } + + err = os.Chdir(dir) + require.NoError(t, err) // Works if the file doesn't exist - if code := cmd.Run([]string{}); code != 0 { - t.Fatalf("expect exit code 0, got: %d", code) - } + code = cmd.Run([]string{}) + require.Empty(t, ui.ErrorWriter.String()) + require.Zero(t, code) + content, err := ioutil.ReadFile(DefaultHclQuotaInitName) - if err != nil { - t.Fatalf("err: %s", err) - } - if string(content) != defaultHclQuotaSpec { - t.Fatalf("unexpected file content\n\n%s", string(content)) - } + require.NoError(t, err) + require.Equal(t, defaultHclQuotaSpec, string(content)) // Fails if the file exists - if code := cmd.Run([]string{}); code != 1 { - t.Fatalf("expect exit code 1, got: %d", code) - } - if out := ui.ErrorWriter.String(); !strings.Contains(out, "exists") { - t.Fatalf("expect file exists error, got: %s", out) - } + code = cmd.Run([]string{}) + require.Contains(t, ui.ErrorWriter.String(), "exists") + require.Equal(t, 1, code) + ui.ErrorWriter.Reset() + + // Works if file is passed + code = cmd.Run([]string{"mytest.hcl"}) + require.Empty(t, ui.ErrorWriter.String()) + require.Zero(t, code) + + content, err = ioutil.ReadFile("mytest.hcl") + require.NoError(t, err) + require.Equal(t, defaultHclQuotaSpec, string(content)) } func TestQuotaInitCommand_Run_JSON(t *testing.T) { @@ -72,48 +69,45 @@ func TestQuotaInitCommand_Run_JSON(t *testing.T) { cmd := &QuotaInitCommand{Meta: Meta{Ui: ui}} // Fails on misuse - if code := cmd.Run([]string{"some", "bad", "args"}); code != 1 { - t.Fatalf("expect exit code 1, got: %d", code) - } - if out := ui.ErrorWriter.String(); !strings.Contains(out, commandErrorText(cmd)) { - t.Fatalf("expect help output, got: %s", out) - } + code := cmd.Run([]string{"some", "bad", "args"}) + require.Equal(t, 1, code) + require.Contains(t, ui.ErrorWriter.String(), commandErrorText(cmd)) ui.ErrorWriter.Reset() // Ensure we change the cwd back origDir, err := os.Getwd() - if err != nil { - t.Fatalf("err: %s", err) - } + require.NoError(t, err) defer os.Chdir(origDir) // Create a temp dir and change into it dir, err := ioutil.TempDir("", "nomad") - if err != nil { - t.Fatalf("err: %s", err) - } + require.NoError(t, err) defer os.RemoveAll(dir) - if err := os.Chdir(dir); err != nil { - t.Fatalf("err: %s", err) - } + + err = os.Chdir(dir) + require.NoError(t, err) // Works if the file doesn't exist - if code := cmd.Run([]string{"-json"}); code != 0 { - t.Fatalf("expect exit code 0, got: %d", code) - } + code = cmd.Run([]string{"-json"}) + require.Empty(t, ui.ErrorWriter.String()) + require.Zero(t, code) + content, err := ioutil.ReadFile(DefaultJsonQuotaInitName) - if err != nil { - t.Fatalf("err: %s", err) - } - if string(content) != defaultJsonQuotaSpec { - t.Fatalf("unexpected file content\n\n%s", string(content)) - } + require.NoError(t, err) + require.Equal(t, defaultJsonQuotaSpec, string(content)) // Fails if the file exists - if code := cmd.Run([]string{"-json"}); code != 1 { - t.Fatalf("expect exit code 1, got: %d", code) - } - if out := ui.ErrorWriter.String(); !strings.Contains(out, "exists") { - t.Fatalf("expect file exists error, got: %s", out) - } + code = cmd.Run([]string{"-json"}) + require.Contains(t, ui.ErrorWriter.String(), "exists") + require.Equal(t, 1, code) + ui.ErrorWriter.Reset() + + // Works if file is passed + code = cmd.Run([]string{"-json", "mytest.json"}) + require.Empty(t, ui.ErrorWriter.String()) + require.Zero(t, code) + + content, err = ioutil.ReadFile("mytest.json") + require.NoError(t, err) + require.Equal(t, defaultJsonQuotaSpec, string(content)) } diff --git a/command/volume_init.go b/command/volume_init.go index 94f55a036f7b..c1714f7bf345 100644 --- a/command/volume_init.go +++ b/command/volume_init.go @@ -27,10 +27,11 @@ type VolumeInitCommand struct { func (c *VolumeInitCommand) Help() string { helpText := ` -Usage: nomad volume init +Usage: nomad volume init Creates an example volume specification file that can be used as a starting - point to customize further. + point to customize further. If no filename is give, the default "volume.json" + or "volume.hcl" will be used. Init Options: @@ -68,8 +69,8 @@ func (c *VolumeInitCommand) Run(args []string) int { // Check that we get no arguments args = flags.Args() - if l := len(args); l != 0 { - c.Ui.Error("This command takes no arguments") + if l := len(args); l > 1 { + c.Ui.Error("This command takes no arguments or one: ") c.Ui.Error(commandErrorText(c)) return 1 } @@ -80,6 +81,9 @@ func (c *VolumeInitCommand) Run(args []string) int { fileName = DefaultJsonVolumeInitName fileContent = defaultJsonVolumeSpec } + if len(args) == 1 { + fileName = args[0] + } // Check if the file already exists _, err := os.Stat(fileName)