Skip to content

Commit

Permalink
Merge pull request #10397 from hashicorp/f-cli-file-arg
Browse files Browse the repository at this point in the history
cli: filename arg for `volume init` and `quote init`
  • Loading branch information
Mahmood Ali committed Apr 19, 2021
2 parents 79325fb + 35c25e8 commit 2051f67
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 69 deletions.
12 changes: 8 additions & 4 deletions command/quota_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ type QuotaInitCommand struct {

func (c *QuotaInitCommand) Help() string {
helpText := `
Usage: nomad quota init
Usage: nomad quota init <filename>
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:
Expand Down Expand Up @@ -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: <filename>")
c.Ui.Error(commandErrorText(c))
return 1
}
Expand All @@ -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)
Expand Down
116 changes: 55 additions & 61 deletions command/quota_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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))
}
12 changes: 8 additions & 4 deletions command/volume_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ type VolumeInitCommand struct {

func (c *VolumeInitCommand) Help() string {
helpText := `
Usage: nomad volume init
Usage: nomad volume init <filename>
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:
Expand Down Expand Up @@ -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: <filename>")
c.Ui.Error(commandErrorText(c))
return 1
}
Expand All @@ -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)
Expand Down

0 comments on commit 2051f67

Please sign in to comment.