Projects STRLCPY cdebug Commits b47569c6
🤬
  • ■ ■ ■ ■ ■
    cmd/exec/exec.go
    skipped 10 lines
    11 11   
    12 12   "github.com/docker/docker/api/types"
    13 13   "github.com/docker/docker/api/types/container"
    14  - dockerclient "github.com/docker/docker/client"
    15 14   "github.com/docker/docker/pkg/stdcopy"
    16 15   "github.com/sirupsen/logrus"
    17 16   "github.com/spf13/cobra"
    18 17   
    19 18   "github.com/iximiuz/cdebug/pkg/cliutil"
     19 + "github.com/iximiuz/cdebug/pkg/docker"
    20 20   "github.com/iximiuz/cdebug/pkg/tty"
    21  - "github.com/iximiuz/cdebug/pkg/util"
     21 + "github.com/iximiuz/cdebug/pkg/uuid"
    22 22  )
    23 23   
    24 24  const (
    skipped 110 lines
    135 135   return err
    136 136   }
    137 137   
    138  - client, err := dockerclient.NewClientWithOpts(
    139  - dockerclient.FromEnv,
    140  - dockerclient.WithAPIVersionNegotiation(),
    141  - )
     138 + client, err := docker.NewClient(cli.AuxStream())
    142 139   if err != nil {
    143 140   return err
    144 141   }
    skipped 7 lines
    152 149   return errors.New("target container found but it's not running")
    153 150   }
    154 151   
    155  - if err := pullImage(ctx, cli, client, opts.image); err != nil {
    156  - return err
     152 + if err := client.ImagePullEx(ctx, opts.image, types.ImagePullOptions{}); err != nil {
     153 + return fmt.Errorf("cannot pull debugger image %q: %w", opts.image, err)
    157 154   }
    158 155   
    159  - runID := util.ShortID()
     156 + runID := uuid.ShortID()
    160 157   nsMode := "container:" + target.ID
    161 158   resp, err := client.ContainerCreate(
    162 159   ctx,
    skipped 70 lines
    233 230   return nil
    234 231  }
    235 232   
    236  -func pullImage(
    237  - ctx context.Context,
    238  - cli cliutil.CLI,
    239  - client *dockerclient.Client,
    240  - image string,
    241  -) error {
    242  - resp, err := client.ImagePull(ctx, image, types.ImagePullOptions{})
    243  - if err != nil {
    244  - return fmt.Errorf("cannot pull debugger image %q: %w", image, err)
    245  - }
    246  - defer resp.Close()
    247  - 
    248  - _, err = io.Copy(cli.OutputStream(), resp)
    249  - return err
    250  -}
    251  - 
    252 233  func attachDebugger(
    253 234   ctx context.Context,
    254 235   cli cliutil.CLI,
    255  - client *dockerclient.Client,
     236 + client *docker.Client,
    256 237   opts *options,
    257 238   contID string,
    258 239  ) (func(), error) {
    skipped 126 lines
  • ■ ■ ■ ■ ■ ■
    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
  • ■ ■ ■ ■
    e2e/portforward/docker_test.go
    skipped 27 lines
    28 28   defer func() { removeContainer(t, targetID).Assert(t, icmd.Success) }()
    29 29   
    30 30   // Initiate port forwarding.
    31  - cmd := icmd.Command("cdebug", "port-forward", "-o", "json", targetID, "80")
     31 + cmd := icmd.Command("cdebug", "port-forward", "-q", "-o", "json", targetID, "80")
    32 32   res := icmd.StartCmd(cmd)
    33 33   assert.NilError(t, res.Error)
    34 34   defer func() { icmd.WaitOnCmd(cmd.Timeout, res).Assert(t, icmd.Success) }()
    skipped 33 lines
  • ■ ■ ■ ■ ■ ■
    main.go
    skipped 57 lines
    58 58   
    59 59   if err := cmd.Execute(); err != nil {
    60 60   if sterr, ok := err.(cliutil.StatusError); ok {
    61  - cli.Grumble("cdebug: %s\n", sterr)
     61 + cli.PrintErr("cdebug: %s\n", sterr)
    62 62   os.Exit(sterr.Code())
    63 63   }
    64 64   
    65 65   // Hopefully, only usage errors.
    66  - logrus.WithError(err).Debug("Exit error")
     66 + logrus.Debug("Exit error: %s", err)
    67 67   os.Exit(1)
    68 68   }
    69 69  }
    skipped 1 lines
    71 71  func setLogLevel(cli cliutil.CLI, logLevel string) {
    72 72   lvl, err := logrus.ParseLevel(logLevel)
    73 73   if err != nil {
    74  - cli.Grumble("Unable to parse log level: %s\n", logLevel)
     74 + cli.PrintErr("Unable to parse log level: %s\n", logLevel)
    75 75   os.Exit(1)
    76 76   }
    77 77   logrus.SetLevel(lvl)
    skipped 2 lines
  • ■ ■ ■ ■ ■ ■
    pkg/cliutil/cliutil.go
    skipped 10 lines
    11 11  type Streams interface {
    12 12   InputStream() *streams.In
    13 13   OutputStream() *streams.Out
     14 + AuxStream() *streams.Out // ErrorStream unless quiet else io.Discard
    14 15   ErrorStream() io.Writer
    15 16  }
    16 17   
    skipped 3 lines
    20 21   SetQuiet(bool)
    21 22   
    22 23   // Regular print to stdout.
    23  - Say(string, ...any)
     24 + PrintOut(string, ...any)
    24 25   
    25 26   // Regular print to stderr.
    26  - Grumble(string, ...any)
     27 + PrintErr(string, ...any)
    27 28   
    28  - // Optional print to stderr (unless quiet).
    29  - Wisper(string, ...any)
     29 + // Print to stderr unless quiet else - discard.
     30 + PrintAux(string, ...any)
    30 31  }
    31 32   
    32 33  type cli struct {
    33 34   inputStream *streams.In
    34 35   outputStream *streams.Out
     36 + auxStream *streams.Out
    35 37   errorStream io.Writer
    36  - 
    37  - quiet bool
    38 38  }
    39 39   
    40 40  var _ CLI = &cli{}
    skipped 2 lines
    43 43   return &cli{
    44 44   inputStream: streams.NewIn(cin),
    45 45   outputStream: streams.NewOut(cout),
     46 + auxStream: streams.NewOut(cerr),
    46 47   errorStream: cerr,
    47 48   }
    48 49  }
    skipped 4 lines
    53 54   
    54 55  func (c *cli) OutputStream() *streams.Out {
    55 56   return c.outputStream
     57 +}
     58 + 
     59 +func (c *cli) AuxStream() *streams.Out {
     60 + return c.auxStream
    56 61  }
    57 62   
    58 63  func (c *cli) ErrorStream() io.Writer {
    skipped 1 lines
    60 65  }
    61 66   
    62 67  func (c *cli) SetQuiet(v bool) {
    63  - c.quiet = v
     68 + if v {
     69 + c.auxStream = streams.NewOut(io.Discard)
     70 + } else {
     71 + c.auxStream = streams.NewOut(c.errorStream)
     72 + }
    64 73  }
    65 74   
    66  -func (c *cli) Say(format string, a ...any) {
     75 +func (c *cli) PrintOut(format string, a ...any) {
    67 76   fmt.Fprintf(c.OutputStream(), format, a...)
    68 77  }
    69 78   
    70  -func (c *cli) Grumble(format string, a ...any) {
     79 +func (c *cli) PrintErr(format string, a ...any) {
    71 80   fmt.Fprintf(c.ErrorStream(), format, a...)
    72 81  }
    73 82   
    74  -func (c *cli) Wisper(format string, a ...any) {
    75  - if !c.quiet {
    76  - fmt.Fprintf(c.ErrorStream(), format, a...)
    77  - }
     83 +func (c *cli) PrintAux(format string, a ...any) {
     84 + fmt.Fprintf(c.AuxStream(), format, a...)
    78 85  }
    79 86   
    80 87  type StatusError struct {
    skipped 29 lines
  • ■ ■ ■ ■ ■ ■
    pkg/docker/client.go
     1 +package docker
     2 + 
     3 +import (
     4 + "context"
     5 + "fmt"
     6 + 
     7 + "github.com/docker/cli/cli/streams"
     8 + "github.com/docker/docker/api/types"
     9 + "github.com/docker/docker/client"
     10 + "github.com/docker/docker/pkg/jsonmessage"
     11 +)
     12 + 
     13 +type Client struct {
     14 + client.CommonAPIClient
     15 + aux *streams.Out
     16 +}
     17 + 
     18 +var _ client.CommonAPIClient = &Client{}
     19 + 
     20 +func NewClient(aux *streams.Out) (*Client, error) {
     21 + inner, err := client.NewClientWithOpts(
     22 + client.FromEnv,
     23 + client.WithAPIVersionNegotiation(),
     24 + )
     25 + if err != nil {
     26 + return nil, fmt.Errorf("cannot initialize Docker client: %w", err)
     27 + }
     28 + 
     29 + return &Client{
     30 + CommonAPIClient: inner,
     31 + aux: aux,
     32 + }, nil
     33 +}
     34 + 
     35 +func (c *Client) ImagePullEx(
     36 + ctx context.Context,
     37 + image string,
     38 + options types.ImagePullOptions,
     39 +) error {
     40 + resp, err := c.CommonAPIClient.ImagePull(ctx, image, options)
     41 + if err != nil {
     42 + return err
     43 + }
     44 + defer resp.Close()
     45 + 
     46 + return jsonmessage.DisplayJSONMessagesToStream(resp, c.aux, nil)
     47 +}
     48 + 
  • ■ ■ ■ ■
    pkg/util/util.go pkg/uuid/uuid.go
    1  -package util
     1 +package uuid
    2 2   
    3 3  import (
    4 4   "strings"
    skipped 8 lines
Please wait...
Page is in error, reload to recover