Skip to content

Commit dc7391a

Browse files
committed
Remove github.com/docker/machine library dependency
1 parent 5428fcb commit dc7391a

File tree

4 files changed

+27
-18
lines changed

4 files changed

+27
-18
lines changed

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ require (
2929
github.com/docker/docker v27.5.0+incompatible
3030
github.com/docker/go-connections v0.5.0
3131
github.com/docker/go-units v0.5.0
32-
github.com/docker/machine v0.7.1-0.20170120224952-7b7a141da844
3332
github.com/evanphx/json-patch v4.12.0+incompatible
3433
github.com/fatih/color v1.18.0
3534
github.com/fullsailor/pkcs7 v0.0.0-20190404230743-d7302db945fa

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,6 @@ github.com/docker/go-metrics v0.0.1 h1:AgB/0SvBxihN0X8OR4SjsblXkbMvalQ8cjmtKQ2rQ
231231
github.com/docker/go-metrics v0.0.1/go.mod h1:cG1hvH2utMXtqgqqYE9plW6lDxS3/5ayHzueweSI3Vw=
232232
github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=
233233
github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
234-
github.com/docker/machine v0.7.1-0.20170120224952-7b7a141da844 h1:R/a6XXKtjxHvmrWt6Xfr92h0YimuscUU7pYMztnsb4w=
235-
github.com/docker/machine v0.7.1-0.20170120224952-7b7a141da844/go.mod h1:I8mPNDeK1uH+JTcUU7X0ZW8KiYz0jyAgNaeSJ1rCfDI=
236234
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
237235
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
238236
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=

helpers/docker/machine_command.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"sync"
1515
"time"
1616

17-
"github.com/docker/machine/commands/mcndirs"
1817
"github.com/sirupsen/logrus"
1918
)
2019

@@ -169,7 +168,7 @@ func (m *machineCommand) Remove(name string) error {
169168
}
170169

171170
func (m *machineCommand) List() (hostNames []string, err error) {
172-
dir, err := os.ReadDir(mcndirs.GetMachineDir())
171+
dir, err := os.ReadDir(getMachineDir())
173172
if err != nil {
174173
errExist := err
175174
// On Windows, ReadDir() on a regular file will satisfy ErrNotExist,
@@ -179,7 +178,7 @@ func (m *machineCommand) List() (hostNames []string, err error) {
179178
// exists or not with a Stat call.
180179
//nolint:goconst
181180
if runtime.GOOS == "windows" {
182-
_, errExist = os.Stat(mcndirs.GetMachineDir())
181+
_, errExist = os.Stat(getMachineDir())
183182
}
184183
if os.IsNotExist(errExist) {
185184
return nil, nil
@@ -231,7 +230,7 @@ func (m *machineCommand) Status(name string) (string, error) {
231230
}
232231

233232
func (m *machineCommand) Exist(name string) bool {
234-
configPath := filepath.Join(mcndirs.GetMachineDir(), name, "config.json")
233+
configPath := filepath.Join(getMachineDir(), name, "config.json")
235234
_, err := os.Stat(configPath)
236235
if err != nil {
237236
return false
@@ -310,6 +309,24 @@ func newDockerMachineCommandCtx(ctx context.Context, args ...string) *exec.Cmd {
310309
return cmd
311310
}
312311

312+
func getBaseDir() string {
313+
homeDir := os.Getenv("HOME")
314+
if runtime.GOOS == "windows" {
315+
homeDir = os.Getenv("USERPROFILE")
316+
}
317+
318+
baseDir := os.Getenv("MACHINE_STORAGE_PATH")
319+
if baseDir == "" {
320+
baseDir = filepath.Join(homeDir, ".docker", "machine")
321+
}
322+
323+
return baseDir
324+
}
325+
326+
func getMachineDir() string {
327+
return filepath.Join(getBaseDir(), "machines")
328+
}
329+
313330
func newDockerMachineCommand(args ...string) *exec.Cmd {
314331
return newDockerMachineCommandCtx(context.Background(), args...)
315332
}

helpers/docker/machine_command_test.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"runtime"
1212
"testing"
1313

14-
"github.com/docker/machine/commands/mcndirs"
1514
"github.com/stretchr/testify/assert"
1615
"github.com/stretchr/testify/require"
1716
)
@@ -23,17 +22,13 @@ func guardMachineOperationTest(t *testing.T, name string, callback func(t *testi
2322
err := os.MkdirAll(machineDir, 0755)
2423
require.NoError(t, err)
2524

26-
mcndirs.BaseDir = machineDir
27-
defer func() {
28-
mcndirs.BaseDir = ""
29-
}()
30-
25+
t.Setenv("MACHINE_STORAGE_PATH", machineDir)
3126
t.Run(name, callback)
3227
}
3328

3429
func TestList(t *testing.T) {
3530
guardMachineOperationTest(t, "no machines", func(t *testing.T) {
36-
err := os.MkdirAll(mcndirs.GetMachineDir(), 0755)
31+
err := os.MkdirAll(getMachineDir(), 0755)
3732
require.NoError(t, err)
3833

3934
mc := NewMachineCommand()
@@ -43,10 +38,10 @@ func TestList(t *testing.T) {
4338
})
4439

4540
guardMachineOperationTest(t, "one machine", func(t *testing.T) {
46-
err := os.MkdirAll(mcndirs.GetMachineDir(), 0755)
41+
err := os.MkdirAll(getMachineDir(), 0755)
4742
require.NoError(t, err)
4843

49-
machineDir := path.Join(mcndirs.GetMachineDir(), "machine-1")
44+
machineDir := path.Join(getMachineDir(), "machine-1")
5045
err = os.MkdirAll(machineDir, 0755)
5146
require.NoError(t, err)
5247

@@ -65,10 +60,10 @@ func TestList(t *testing.T) {
6560
})
6661

6762
guardMachineOperationTest(t, "machines directory is invalid", func(t *testing.T) {
68-
err := os.MkdirAll(mcndirs.GetBaseDir(), 0755)
63+
err := os.MkdirAll(getBaseDir(), 0755)
6964
require.NoError(t, err)
7065

71-
err = os.WriteFile(mcndirs.GetMachineDir(), []byte{}, 0o600)
66+
err = os.WriteFile(getMachineDir(), []byte{}, 0o600)
7267
require.NoError(t, err)
7368

7469
mc := NewMachineCommand()

0 commit comments

Comments
 (0)