Files
Abiola IbrahimandGitHub 3d8dd0e234 chore: code refactor (#1072)
* chore: code refactor

Signed-off-by: Abiola Ibrahim <git@abiosoft.com>

* docs: update instructions

Signed-off-by: Abiola Ibrahim <git@abiosoft.com>

* chore: update refactor

Signed-off-by: Abiola Ibrahim <git@abiosoft.com>

---------

Signed-off-by: Abiola Ibrahim <git@abiosoft.com>
2024-07-21 11:01:35 +01:00

45 lines
1.1 KiB
Go

package docker
import (
"path/filepath"
"github.com/abiosoft/colima/config"
)
var configDir = func() string { return config.CurrentProfile().ConfigDir() }
// HostSocketFile returns the path to the docker socket on host.
func HostSocketFile() string { return filepath.Join(configDir(), "docker.sock") }
func LegacyDefaultHostSocketFile() string {
return filepath.Join(filepath.Dir(configDir()), "docker.sock")
}
func (d dockerRuntime) contextCreated() bool {
return d.host.RunQuiet("docker", "context", "inspect", config.CurrentProfile().ID) == nil
}
func (d dockerRuntime) setupContext() error {
if d.contextCreated() {
return nil
}
profile := config.CurrentProfile()
return d.host.Run("docker", "context", "create", profile.ID,
"--description", profile.DisplayName,
"--docker", "host=unix://"+HostSocketFile(),
)
}
func (d dockerRuntime) useContext() error {
return d.host.Run("docker", "context", "use", config.CurrentProfile().ID)
}
func (d dockerRuntime) teardownContext() error {
if !d.contextCreated() {
return nil
}
return d.host.Run("docker", "context", "rm", "--force", config.CurrentProfile().ID)
}