|
1 | 1 | package docker |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "errors" |
4 | 5 | "fmt" |
| 6 | + "net/url" |
5 | 7 | "os" |
6 | 8 | "runtime" |
| 9 | + "strings" |
7 | 10 |
|
8 | 11 | "github.com/moby/moby/client" |
9 | 12 | ) |
10 | 13 |
|
11 | 14 | func NewClient() (*client.Client, error) { |
12 | 15 |
|
13 | | - if runtime.GOOS == "windows" { |
14 | | - value := os.Getenv("DOCKER_HOST") |
15 | | - if value == "" { |
16 | | - return nil, fmt.Errorf(`The environment variable DOCKER_HOST is not defined. |
17 | | -It must have a value similar to 'tcp://<host>:<port>'.`) |
| 16 | + host, ok := os.LookupEnv("DOCKER_HOST") |
| 17 | + if ok { |
| 18 | + err := ValidateHost(host) |
| 19 | + if err != nil { |
| 20 | + return nil, fmt.Errorf("DOCKER_HOST has an invalid value '%s': %w", host, err) |
18 | 21 | } |
| 22 | + } else { |
| 23 | + host = client.DefaultDockerHost |
19 | 24 | } |
20 | 25 |
|
21 | | - return client.New(client.WithHostFromEnv()) |
| 26 | + return NewClientForHost(host) |
| 27 | +} |
| 28 | + |
| 29 | +func NewClientForHost(host string) (*client.Client, error) { |
| 30 | + return client.New(client.WithHost(host)) |
| 31 | +} |
| 32 | + |
| 33 | +var protocolsForUnix = []string{"tcp", "ssh", "unix"} |
| 34 | +var protocolsForWindows = []string{"tcp", "ssh", "npipe"} |
| 35 | + |
| 36 | +func ValidateHost(value string) error { |
| 37 | + |
| 38 | + if len(strings.TrimSpace(value)) == 0 { |
| 39 | + return errors.New("value is blank") |
| 40 | + } |
| 41 | + |
| 42 | + scheme, remaining, ok := strings.Cut(value, "://") |
| 43 | + if !ok || scheme == "" { |
| 44 | + return errors.New("protocol is missing in the URL") |
| 45 | + } |
| 46 | + |
| 47 | + switch scheme { |
| 48 | + case "tcp": |
| 49 | + if err := validateTcpHost(value); err != nil { |
| 50 | + return err |
| 51 | + } |
| 52 | + case "unix": |
| 53 | + if err := validateUnixHost(remaining); err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + case "ssh", "npipe": |
| 57 | + default: |
| 58 | + return fmt.Errorf( |
| 59 | + "unsupported protocol '%s'; "+ |
| 60 | + "the protocol must be one of [%s] for Unix and [%s] for Windows; ", |
| 61 | + scheme, |
| 62 | + strings.Join(protocolsForUnix, ", "), |
| 63 | + strings.Join(protocolsForWindows, ", ")) |
| 64 | + } |
| 65 | + |
| 66 | + return nil |
22 | 67 | } |
23 | 68 |
|
24 | 69 | func WrapError(err error) error { |
25 | 70 | if client.IsErrConnectionFailed(err) { |
26 | | - return fmt.Errorf(`failed to connect to the Docker daemon: %w\n |
27 | | -Hint: If the daemon is installed on a WSL distribution, start the instance first. |
28 | | -`, err) |
| 71 | + return fmt.Errorf( |
| 72 | + `failed to connect to the Docker daemon: %w |
| 73 | +Hint: If the daemon is installed on a WSL distribution, start the distribution first.`, |
| 74 | + err) |
29 | 75 | } |
30 | 76 | return err |
31 | 77 | } |
| 78 | + |
| 79 | +func validateTcpHost(value string) error { |
| 80 | + parsed, err := url.Parse(value) |
| 81 | + if err != nil { |
| 82 | + return fmt.Errorf("not a URL: %w", err) |
| 83 | + } |
| 84 | + if parsed.Hostname() == "" { |
| 85 | + return errors.New("hostname is missing") |
| 86 | + } |
| 87 | + if parsed.Port() == "" { |
| 88 | + return errors.New("port number is missing") |
| 89 | + } |
| 90 | + return nil |
| 91 | +} |
| 92 | + |
| 93 | +func validateUnixHost(path string) error { |
| 94 | + if runtime.GOOS == "windows" { |
| 95 | + return errors.New("'unix' protocol is not supported in Windows") |
| 96 | + } |
| 97 | + if len(strings.TrimSpace(path)) == 0 { |
| 98 | + return errors.New("path is blank") |
| 99 | + } |
| 100 | + if !strings.HasPrefix(path, "/") { |
| 101 | + return fmt.Errorf("path is not absolute: '%s'", path) |
| 102 | + } |
| 103 | + if _, err := os.Stat(path); err != nil { |
| 104 | + return fmt.Errorf("file does not exist at '%s'", path) |
| 105 | + } |
| 106 | + return nil |
| 107 | +} |
0 commit comments