Skip to content

Commit 5000139

Browse files
Add ability to imply 'default' VM in commands
Signed-off-by: Nathan LeClaire <nathan.leclaire@gmail.com>
1 parent 0bbf6f1 commit 5000139

File tree

18 files changed

+308
-86
lines changed

18 files changed

+308
-86
lines changed

commands/commands.go

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ import (
1818
"github.com/docker/machine/libmachine/ssh"
1919
)
2020

21+
const (
22+
defaultMachineName = "default"
23+
)
24+
2125
var (
26+
ErrHostLoad = errors.New("All specified hosts had errors loading their configuration.")
27+
ErrNoDefault = fmt.Errorf("Error: No machine name(s) specified and no %q machine exists.", defaultMachineName)
2228
ErrNoMachineSpecified = errors.New("Error: Expected to get one or more machine names as arguments")
2329
ErrExpectedOneMachine = errors.New("Error: Expected one machine name as an argument")
2430
ErrTooManyArguments = errors.New("Error: Too many arguments given")
@@ -67,8 +73,45 @@ func (c *contextCommandLine) Application() *cli.App {
6773
return c.App
6874
}
6975

76+
// targetHost returns a specific host name if one is indicated by the first CLI
77+
// arg, or the default host name if no host is specified.
78+
func targetHost(c CommandLine, api libmachine.API) (string, error) {
79+
if len(c.Args()) == 0 {
80+
defaultExists, err := api.Exists(defaultMachineName)
81+
if err != nil {
82+
return "", fmt.Errorf("Error checking if host %q exists: %s", defaultMachineName, err)
83+
}
84+
85+
if defaultExists {
86+
return defaultMachineName, nil
87+
}
88+
89+
return "", ErrNoDefault
90+
}
91+
92+
return c.Args()[0], nil
93+
}
94+
7095
func runAction(actionName string, c CommandLine, api libmachine.API) error {
71-
hosts, hostsInError := persist.LoadHosts(api, c.Args())
96+
var (
97+
hostsToLoad []string
98+
)
99+
100+
// If user did not specify a machine name explicitly, use the 'default'
101+
// machine if it exists. This allows short form commands such as
102+
// 'docker-machine stop' for convenience.
103+
if len(c.Args()) == 0 {
104+
target, err := targetHost(c, api)
105+
if err != nil {
106+
return err
107+
}
108+
109+
hostsToLoad = []string{target}
110+
} else {
111+
hostsToLoad = c.Args()
112+
}
113+
114+
hosts, hostsInError := persist.LoadHosts(api, hostsToLoad)
72115

73116
if len(hostsInError) > 0 {
74117
errs := []error{}
@@ -79,7 +122,7 @@ func runAction(actionName string, c CommandLine, api libmachine.API) error {
79122
}
80123

81124
if len(hosts) == 0 {
82-
return ErrNoMachineSpecified
125+
return ErrHostLoad
83126
}
84127

85128
if errs := runActionForeachMachine(actionName, hosts); len(errs) > 0 {

commands/config.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ func cmdConfig(c CommandLine, api libmachine.API) error {
1414
// being run (it is intended to be run in a subshell)
1515
log.SetOutWriter(os.Stderr)
1616

17-
if len(c.Args()) != 1 {
18-
return ErrExpectedOneMachine
17+
target, err := targetHost(c, api)
18+
if err != nil {
19+
return err
1920
}
2021

21-
host, err := api.Load(c.Args().First())
22+
host, err := api.Load(target)
2223
if err != nil {
2324
return err
2425
}

commands/env.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ const (
2020
)
2121

2222
var (
23-
errImproperEnvArgs = errors.New("Error: Expected one machine name")
2423
errImproperUnsetEnvArgs = errors.New("Error: Expected no machine name when the -u flag is present")
2524
defaultUsageHinter UsageHintGenerator
2625
)
@@ -68,11 +67,16 @@ func cmdEnv(c CommandLine, api libmachine.API) error {
6867
}
6968

7069
func shellCfgSet(c CommandLine, api libmachine.API) (*ShellConfig, error) {
71-
if len(c.Args()) != 1 {
72-
return nil, errImproperEnvArgs
70+
if len(c.Args()) > 1 {
71+
return nil, ErrExpectedOneMachine
7372
}
7473

75-
host, err := api.Load(c.Args().First())
74+
target, err := targetHost(c, api)
75+
if err != nil {
76+
return nil, err
77+
}
78+
79+
host, err := api.Load(target)
7680
if err != nil {
7781
return nil, err
7882
}

commands/env_test.go

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,14 @@ func TestShellCfgSet(t *testing.T) {
111111
}{
112112
{
113113
description: "no host name specified",
114+
api: &libmachinetest.FakeAPI{
115+
Hosts: []*host.Host{},
116+
},
114117
commandLine: &commandstest.FakeCommandLine{
115118
CliArgs: nil,
116119
},
117120
expectedShellCfg: nil,
118-
expectedErr: errImproperEnvArgs,
121+
expectedErr: ErrNoDefault,
119122
},
120123
{
121124
description: "bash shell set happy path without any flags set",
@@ -153,6 +156,42 @@ func TestShellCfgSet(t *testing.T) {
153156
},
154157
expectedErr: nil,
155158
},
159+
{
160+
description: "bash shell set happy path with 'default' vm",
161+
commandLine: &commandstest.FakeCommandLine{
162+
CliArgs: []string{},
163+
LocalFlags: &commandstest.FakeFlagger{
164+
Data: map[string]interface{}{
165+
"shell": "bash",
166+
"swarm": false,
167+
"no-proxy": false,
168+
},
169+
},
170+
},
171+
api: &libmachinetest.FakeAPI{
172+
Hosts: []*host.Host{
173+
{
174+
Name: defaultMachineName,
175+
},
176+
},
177+
},
178+
connChecker: &FakeConnChecker{
179+
DockerHost: "tcp://1.2.3.4:2376",
180+
AuthOptions: nil,
181+
Err: nil,
182+
},
183+
expectedShellCfg: &ShellConfig{
184+
Prefix: "export ",
185+
Delimiter: "=\"",
186+
Suffix: "\"\n",
187+
DockerCertPath: filepath.Join(mcndirs.GetMachineDir(), defaultMachineName),
188+
DockerHost: "tcp://1.2.3.4:2376",
189+
DockerTLSVerify: "1",
190+
UsageHint: usageHint,
191+
MachineName: defaultMachineName,
192+
},
193+
expectedErr: nil,
194+
},
156195
{
157196
description: "fish shell set happy path",
158197
commandLine: &commandstest.FakeCommandLine{

commands/inspect.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,17 @@ var funcMap = template.FuncMap{
2121
}
2222

2323
func cmdInspect(c CommandLine, api libmachine.API) error {
24-
if len(c.Args()) == 0 {
24+
if len(c.Args()) > 1 {
2525
c.ShowHelp()
2626
return ErrExpectedOneMachine
2727
}
2828

29-
host, err := api.Load(c.Args().First())
29+
target, err := targetHost(c, api)
30+
if err != nil {
31+
return err
32+
}
33+
34+
host, err := api.Load(target)
3035
if err != nil {
3136
return err
3237
}

commands/ip_test.go

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/docker/machine/commands/commandstest"
77
"github.com/docker/machine/drivers/fakedriver"
8+
"github.com/docker/machine/libmachine"
89
"github.com/docker/machine/libmachine/host"
910
"github.com/docker/machine/libmachine/libmachinetest"
1011
"github.com/docker/machine/libmachine/state"
@@ -17,30 +18,62 @@ func TestCmdIPMissingMachineName(t *testing.T) {
1718

1819
err := cmdURL(commandLine, api)
1920

20-
assert.EqualError(t, err, "Error: Expected one machine name as an argument")
21+
assert.Equal(t, err, ErrNoDefault)
2122
}
2223

2324
func TestCmdIP(t *testing.T) {
24-
commandLine := &commandstest.FakeCommandLine{
25-
CliArgs: []string{"machine"},
26-
}
27-
api := &libmachinetest.FakeAPI{
28-
Hosts: []*host.Host{
29-
{
30-
Name: "machine",
31-
Driver: &fakedriver.Driver{
32-
MockState: state.Running,
33-
MockIP: "1.2.3.4",
25+
testCases := []struct {
26+
commandLine CommandLine
27+
api libmachine.API
28+
expectedErr error
29+
expectedOut string
30+
}{
31+
{
32+
commandLine: &commandstest.FakeCommandLine{
33+
CliArgs: []string{"machine"},
34+
},
35+
api: &libmachinetest.FakeAPI{
36+
Hosts: []*host.Host{
37+
{
38+
Name: "machine",
39+
Driver: &fakedriver.Driver{
40+
MockState: state.Running,
41+
MockIP: "1.2.3.4",
42+
},
43+
},
44+
},
45+
},
46+
expectedErr: nil,
47+
expectedOut: "1.2.3.4\n",
48+
},
49+
{
50+
commandLine: &commandstest.FakeCommandLine{
51+
CliArgs: []string{},
52+
},
53+
api: &libmachinetest.FakeAPI{
54+
Hosts: []*host.Host{
55+
{
56+
Name: "default",
57+
Driver: &fakedriver.Driver{
58+
MockState: state.Running,
59+
MockIP: "1.2.3.4",
60+
},
61+
},
3462
},
3563
},
64+
expectedErr: nil,
65+
expectedOut: "1.2.3.4\n",
3666
},
3767
}
3868

39-
stdoutGetter := commandstest.NewStdoutGetter()
40-
defer stdoutGetter.Stop()
69+
for _, tc := range testCases {
70+
stdoutGetter := commandstest.NewStdoutGetter()
4171

42-
err := cmdIP(commandLine, api)
72+
err := cmdIP(tc.commandLine, tc.api)
4373

44-
assert.NoError(t, err)
45-
assert.Equal(t, "1.2.3.4\n", stdoutGetter.Output())
74+
assert.Equal(t, tc.expectedErr, err)
75+
assert.Equal(t, tc.expectedOut, stdoutGetter.Output())
76+
77+
stdoutGetter.Stop()
78+
}
4679
}

commands/kill_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func TestCmdKillMissingMachineName(t *testing.T) {
1717

1818
err := cmdKill(commandLine, api)
1919

20-
assert.EqualError(t, err, "Error: Expected to get one or more machine names as arguments")
20+
assert.Equal(t, ErrNoDefault, err)
2121
}
2222

2323
func TestCmdKill(t *testing.T) {

commands/rm_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func TestCmdRmMissingMachineName(t *testing.T) {
1818

1919
err := cmdRm(commandLine, api)
2020

21-
assert.EqualError(t, err, "Error: Expected to get one or more machine names as arguments")
21+
assert.Equal(t, ErrNoMachineSpecified, err)
2222
assert.True(t, commandLine.HelpShown)
2323
}
2424

commands/ssh.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ func cmdSSH(c CommandLine, api libmachine.API) error {
2323
return nil
2424
}
2525

26-
name := firstArg
27-
if name == "" {
28-
return ErrExpectedOneMachine
26+
target, err := targetHost(c, api)
27+
if err != nil {
28+
return err
2929
}
3030

31-
host, err := api.Load(name)
31+
host, err := api.Load(target)
3232
if err != nil {
3333
return err
3434
}

commands/ssh_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ func TestCmdSSH(t *testing.T) {
5353
},
5454
{
5555
commandLine: &commandstest.FakeCommandLine{
56-
CliArgs: []string{""},
56+
CliArgs: []string{},
5757
},
5858
api: &libmachinetest.FakeAPI{},
59-
expectedErr: ErrExpectedOneMachine,
59+
expectedErr: ErrNoDefault,
6060
},
6161
{
6262
commandLine: &commandstest.FakeCommandLine{

0 commit comments

Comments
 (0)