■ ■ ■ ■ ■ ■
cmd/portforward/portforward.go
| skipped 3 lines |
4 | 4 | | "context" |
5 | 5 | | "errors" |
6 | 6 | | "fmt" |
7 | | - | "io" |
8 | 7 | | "net" |
9 | 8 | | "os" |
10 | 9 | | "os/signal" |
| skipped 2 lines |
13 | 12 | | |
14 | 13 | | "github.com/docker/docker/api/types" |
15 | 14 | | "github.com/docker/docker/api/types/container" |
16 | | - | dockerclient "github.com/docker/docker/client" |
17 | 15 | | "github.com/docker/go-connections/nat" |
18 | 16 | | "github.com/sirupsen/logrus" |
19 | 17 | | "github.com/spf13/cobra" |
20 | 18 | | |
21 | 19 | | "github.com/iximiuz/cdebug/pkg/cliutil" |
| 20 | + | "github.com/iximiuz/cdebug/pkg/docker" |
22 | 21 | | "github.com/iximiuz/cdebug/pkg/jsonutil" |
23 | | - | "github.com/iximiuz/cdebug/pkg/util" |
| 22 | + | "github.com/iximiuz/cdebug/pkg/uuid" |
24 | 23 | | ) |
25 | 24 | | |
26 | 25 | | // TODO: |
| skipped 68 lines |
95 | 94 | | } |
96 | 95 | | |
97 | 96 | | func runPortForward(ctx context.Context, cli cliutil.CLI, opts *options) error { |
98 | | - | client, err := dockerclient.NewClientWithOpts( |
99 | | - | dockerclient.FromEnv, |
100 | | - | dockerclient.WithAPIVersionNegotiation(), |
101 | | - | ) |
| 97 | + | client, err := docker.NewClient(cli.AuxStream()) |
102 | 98 | | if err != nil { |
103 | | - | return fmt.Errorf("cannot initialize Docker client: %w", err) |
| 99 | + | return err |
104 | 100 | | } |
105 | 101 | | |
106 | 102 | | target, err := client.ContainerInspect(ctx, opts.target) |
107 | 103 | | if err != nil { |
108 | | - | return fmt.Errorf("cannot inspect target container: %w", err) |
| 104 | + | return err |
109 | 105 | | } |
110 | 106 | | |
111 | | - | if err := pullImage(ctx, cli, client, helperImage); err != nil { |
112 | | - | return err |
| 107 | + | // TODO: Check that target has at least 1 IP! |
| 108 | + | |
| 109 | + | if err := client.ImagePullEx(ctx, helperImage, types.ImagePullOptions{}); err != nil { |
| 110 | + | return fmt.Errorf("cannot pull port-forwarder helper image %q: %w", helperImage, err) |
113 | 111 | | } |
114 | 112 | | |
115 | 113 | | forwardings, err := parseForwardings(target, opts.forwardings) |
| skipped 24 lines |
140 | 138 | | }, |
141 | 139 | | nil, |
142 | 140 | | nil, |
143 | | - | "port-forwarder-"+util.ShortID(), |
| 141 | + | "port-forwarder-"+uuid.ShortID(), |
144 | 142 | | ) |
145 | 143 | | if err != nil { |
146 | 144 | | return fmt.Errorf("cannot create port-forwarder container: %w", err) |
| skipped 16 lines |
163 | 161 | | case outFormatText: |
164 | 162 | | local := net.JoinHostPort(binding.HostIP, binding.HostPort) |
165 | 163 | | remote := targetIP + ":" + string(remotePort) |
166 | | - | cli.Say("Forwarding %s to %s's %s\n", local, target.Name[1:], remote) |
| 164 | + | cli.PrintOut("Forwarding %s to %s's %s\n", local, target.Name[1:], remote) |
167 | 165 | | case outFormatJSON: |
168 | | - | cli.Say(jsonutil.Dump(map[string]string{ |
| 166 | + | cli.PrintOut(jsonutil.Dump(map[string]string{ |
169 | 167 | | "localHost": binding.HostIP, |
170 | 168 | | "localPort": binding.HostPort, |
171 | 169 | | "remoteHost": targetIP, |
| skipped 11 lines |
183 | 181 | | |
184 | 182 | | go func() { |
185 | 183 | | for _ = range sigCh { |
186 | | - | cli.Wisper("Exiting...") |
| 184 | + | cli.PrintAux("Exiting...") |
187 | 185 | | |
188 | 186 | | if err := client.ContainerKill(ctx, resp.ID, "KILL"); err != nil { |
189 | 187 | | logrus.Debugf("Cannot kill forwarder container: %s", err) |
| skipped 7 lines |
197 | 195 | | select { |
198 | 196 | | case err := <-forwarderErrCh: |
199 | 197 | | if err != nil { |
200 | | - | return fmt.Errorf("waiting port-forwarder container failed: %w", err) |
| 198 | + | return fmt.Errorf("waiting for port-forwarder container failed: %w", err) |
201 | 199 | | } |
202 | 200 | | case <-forwarderStatusCh: |
203 | 201 | | } |
204 | 202 | | |
205 | 203 | | return nil |
206 | | - | } |
207 | | - | |
208 | | - | func pullImage( |
209 | | - | ctx context.Context, |
210 | | - | cli cliutil.CLI, |
211 | | - | client *dockerclient.Client, |
212 | | - | image string, |
213 | | - | ) error { |
214 | | - | resp, err := client.ImagePull(ctx, image, types.ImagePullOptions{}) |
215 | | - | if err != nil { |
216 | | - | return fmt.Errorf("cannot pull port-forwarder helper image %q: %w", image, err) |
217 | | - | } |
218 | | - | defer resp.Close() |
219 | | - | |
220 | | - | _, err = io.Copy(cli.OutputStream(), resp) |
221 | | - | return err |
222 | 204 | | } |
223 | 205 | | |
224 | 206 | | type forwarding struct { |
| skipped 78 lines |