X Tutup
Skip to content

Commit eb39f09

Browse files
committed
Move code where it's used
Signed-off-by: David Gageot <david@gageot.net>
1 parent 8bbfc6e commit eb39f09

File tree

7 files changed

+75
-103
lines changed

7 files changed

+75
-103
lines changed

commands/active.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@ package commands
33
import (
44
"errors"
55
"fmt"
6-
"os"
7-
"strings"
86

97
"github.com/docker/machine/libmachine/host"
108
"github.com/docker/machine/libmachine/persist"
11-
"github.com/docker/machine/libmachine/state"
129
)
1310

1411
var (
@@ -50,17 +47,3 @@ func getActiveHost(store persist.Store) (*host.Host, error) {
5047

5148
return nil, errors.New("Active host not found")
5249
}
53-
54-
// IsActive provides a single function for determining if a host is active
55-
// based on both the url and if the host is stopped.
56-
func isActive(h *host.Host, currentState state.State, url string) (bool, error) {
57-
dockerHost := os.Getenv("DOCKER_HOST")
58-
59-
// TODO: hard-coding the swarm port is a travesty...
60-
deSwarmedHost := strings.Replace(dockerHost, ":3376", ":2376", 1)
61-
if dockerHost == url || deSwarmedHost == url {
62-
return currentState == state.Running, nil
63-
}
64-
65-
return false, nil
66-
}

commands/active_test.go

Lines changed: 0 additions & 45 deletions
This file was deleted.

commands/commands.go

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"os"
77
"path/filepath"
8-
"runtime"
98
"strings"
109

1110
"github.com/docker/machine/cli"
@@ -447,22 +446,21 @@ func runActionWithContext(actionName string, c CommandLine) error {
447446
// sigh.
448447
func getCertPathInfoFromContext(c CommandLine) cert.PathInfo {
449448
caCertPath := c.GlobalString("tls-ca-cert")
450-
caKeyPath := c.GlobalString("tls-ca-key")
451-
clientCertPath := c.GlobalString("tls-client-cert")
452-
clientKeyPath := c.GlobalString("tls-client-key")
453-
454449
if caCertPath == "" {
455450
caCertPath = filepath.Join(mcndirs.GetMachineCertDir(), "ca.pem")
456451
}
457452

453+
caKeyPath := c.GlobalString("tls-ca-key")
458454
if caKeyPath == "" {
459455
caKeyPath = filepath.Join(mcndirs.GetMachineCertDir(), "ca-key.pem")
460456
}
461457

458+
clientCertPath := c.GlobalString("tls-client-cert")
462459
if clientCertPath == "" {
463460
clientCertPath = filepath.Join(mcndirs.GetMachineCertDir(), "cert.pem")
464461
}
465462

463+
clientKeyPath := c.GlobalString("tls-client-key")
466464
if clientKeyPath == "" {
467465
clientKeyPath = filepath.Join(mcndirs.GetMachineCertDir(), "key.pem")
468466
}
@@ -474,20 +472,3 @@ func getCertPathInfoFromContext(c CommandLine) cert.PathInfo {
474472
ClientKeyPath: clientKeyPath,
475473
}
476474
}
477-
478-
func detectShell() (string, error) {
479-
// attempt to get the SHELL env var
480-
shell := filepath.Base(os.Getenv("SHELL"))
481-
482-
log.Debugf("shell: %s", shell)
483-
if shell == "" {
484-
// check for windows env and not bash (i.e. msysgit, etc)
485-
if runtime.GOOS == "windows" {
486-
log.Printf("On Windows, please specify either 'cmd' or 'powershell' with the --shell flag.\n\n")
487-
}
488-
489-
return "", ErrUnknownShell
490-
}
491-
492-
return shell, nil
493-
}

commands/commands_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package commands
22

33
import (
44
"errors"
5+
"os"
56
"strings"
67
"testing"
78

@@ -12,6 +13,18 @@ import (
1213
"github.com/stretchr/testify/assert"
1314
)
1415

16+
var (
17+
stdout *os.File
18+
)
19+
20+
func init() {
21+
stdout = os.Stdout
22+
}
23+
24+
func cleanup() {
25+
os.Stdout = stdout
26+
}
27+
1528
func TestRunActionForeachMachine(t *testing.T) {
1629
// Assume a bunch of machines in randomly started or
1730
// stopped states.

commands/env.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"os"
77
"path/filepath"
8+
"runtime"
89
"strings"
910
"text/template"
1011

@@ -208,3 +209,20 @@ func generateUsageHint(userShell string, args []string) string {
208209

209210
return fmt.Sprintf("%s Run this command to configure your shell: \n%s %s\n", comment, comment, cmd)
210211
}
212+
213+
func detectShell() (string, error) {
214+
// attempt to get the SHELL env var
215+
shell := filepath.Base(os.Getenv("SHELL"))
216+
217+
log.Debugf("shell: %s", shell)
218+
if shell == "" {
219+
// check for windows env and not bash (i.e. msysgit, etc)
220+
if runtime.GOOS == "windows" {
221+
log.Printf("On Windows, please specify either 'cmd' or 'powershell' with the --shell flag.\n\n")
222+
}
223+
224+
return "", ErrUnknownShell
225+
}
226+
227+
return shell, nil
228+
}

commands/ls.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ func attemptGetHostState(h *host.Host, stateQueryChan chan<- HostListItem) {
261261
close(stateCh)
262262
close(urlCh)
263263

264-
active, err := isActive(h, currentState, url)
264+
active, err := isActive(currentState, url)
265265
if err != nil {
266266
log.Errorf("error determining if host is active for host %s: %s",
267267
h.Name, err)
@@ -329,3 +329,17 @@ func sortHostListItemsByName(items []HostListItem) {
329329
items[i] = m[v]
330330
}
331331
}
332+
333+
// IsActive provides a single function for determining if a host is active
334+
// based on both the url and if the host is stopped.
335+
func isActive(currentState state.State, url string) (bool, error) {
336+
if currentState != state.Running {
337+
return false, nil
338+
}
339+
340+
// TODO: hard-coding the swarm port is a travesty...
341+
dockerHost := os.Getenv("DOCKER_HOST")
342+
deSwarmedHost := strings.Replace(dockerHost, ":3376", ":2376", 1)
343+
344+
return dockerHost == url || deSwarmedHost == url, nil
345+
}

commands/ls_test.go

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,6 @@ import (
1313
"github.com/stretchr/testify/assert"
1414
)
1515

16-
var (
17-
hostTestStorePath string
18-
stdout *os.File
19-
)
20-
21-
func init() {
22-
stdout = os.Stdout
23-
}
24-
25-
func cleanup() {
26-
os.Stdout = stdout
27-
os.RemoveAll(hostTestStorePath)
28-
}
29-
3016
func TestParseFiltersErrorsGivenInvalidFilter(t *testing.T) {
3117
_, err := parseFilters([]string{"foo=bar"})
3218
assert.EqualError(t, err, "Unsupported filter key 'foo'")
@@ -306,8 +292,6 @@ func captureStdout() (chan string, *os.File) {
306292
}
307293

308294
func TestGetHostListItems(t *testing.T) {
309-
defer cleanup()
310-
311295
hostListItemsChan := make(chan HostListItem)
312296

313297
hosts := []*host.Host{
@@ -381,8 +365,6 @@ func TestGetHostListItems(t *testing.T) {
381365
func TestGetHostListItemsEnvDockerHostUnset(t *testing.T) {
382366
orgDockerHost := os.Getenv("DOCKER_HOST")
383367
defer func() {
384-
cleanup()
385-
386368
// revert DOCKER_HOST
387369
os.Setenv("DOCKER_HOST", orgDockerHost)
388370
}()
@@ -465,3 +447,29 @@ func TestGetHostListItemsEnvDockerHostUnset(t *testing.T) {
465447
}
466448
}
467449
}
450+
451+
func TestIsActive(t *testing.T) {
452+
cases := []struct {
453+
dockerHost string
454+
state state.State
455+
expected bool
456+
}{
457+
{"", state.Running, false},
458+
{"tcp://5.6.7.8:2376", state.Running, false},
459+
{"tcp://1.2.3.4:2376", state.Stopped, false},
460+
{"tcp://1.2.3.4:2376", state.Running, true},
461+
{"tcp://1.2.3.4:3376", state.Running, true},
462+
}
463+
464+
for _, c := range cases {
465+
os.Unsetenv("DOCKER_HOST")
466+
if c.dockerHost != "" {
467+
os.Setenv("DOCKER_HOST", c.dockerHost)
468+
}
469+
470+
actual, err := isActive(c.state, "tcp://1.2.3.4:2376")
471+
472+
assert.Equal(t, c.expected, actual, "IsActive(%s, \"%s\") should return %v, but didn't", c.state, c.dockerHost, c.expected)
473+
assert.NoError(t, err)
474+
}
475+
}

0 commit comments

Comments
 (0)
X Tutup