ConnectAndWait connects to the socket passed via well-known env var, if present, and attempts to read from it until it receives an EOF, at which point cb is called.
(cb func())
| 140 | // if present, and attempts to read from it until it receives an EOF, at which |
| 141 | // point cb is called. |
| 142 | func ConnectAndWait(cb func()) { |
| 143 | socketAddr, ok := os.LookupEnv(EnvKey) |
| 144 | if !ok { |
| 145 | // if a plugin compiled against a more recent version of docker/cli |
| 146 | // is executed by an older CLI binary, ignore missing environment |
| 147 | // variable and behave as usual |
| 148 | return |
| 149 | } |
| 150 | addr, err := net.ResolveUnixAddr("unix", socketAddr) |
| 151 | if err != nil { |
| 152 | return |
| 153 | } |
| 154 | conn, err := net.DialUnix("unix", nil, addr) |
| 155 | if err != nil { |
| 156 | return |
| 157 | } |
| 158 | |
| 159 | go func() { |
| 160 | b := make([]byte, 1) |
| 161 | for { |
| 162 | _, err := conn.Read(b) |
| 163 | if errors.Is(err, io.EOF) { |
| 164 | cb() |
| 165 | return |
| 166 | } |
| 167 | } |
| 168 | }() |
| 169 | } |
searching dependent graphs…