|
| 1 | +package enaptercli |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "net/url" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/urfave/cli/v2" |
| 10 | +) |
| 11 | + |
| 12 | +type cmdDevicesInspect struct { |
| 13 | + cmdDevices |
| 14 | + deviceID string |
| 15 | + expand []string |
| 16 | +} |
| 17 | + |
| 18 | +func buildCmdDevicesInspect() *cli.Command { |
| 19 | + cmd := &cmdDevicesInspect{} |
| 20 | + return &cli.Command{ |
| 21 | + Name: "inspect", |
| 22 | + Usage: "Inspect a devices", |
| 23 | + CustomHelpTemplate: cmd.HelpTemplate(), |
| 24 | + Flags: cmd.Flags(), |
| 25 | + Before: cmd.Before, |
| 26 | + Action: func(cliCtx *cli.Context) error { |
| 27 | + return cmd.do(cliCtx.Context) |
| 28 | + }, |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +func (c *cmdDevicesInspect) Flags() []cli.Flag { |
| 33 | + flags := c.cmdDevices.Flags() |
| 34 | + return append(flags, &cli.StringFlag{ |
| 35 | + Name: "device-id", |
| 36 | + Aliases: []string{"d"}, |
| 37 | + Usage: "device ID", |
| 38 | + Destination: &c.deviceID, |
| 39 | + Required: true, |
| 40 | + }, &cli.MultiStringFlag{ |
| 41 | + Target: &cli.StringSliceFlag{ |
| 42 | + Name: "expand", |
| 43 | + Usage: "coma separated list of expanded device info", |
| 44 | + }, |
| 45 | + Destination: &c.expand, |
| 46 | + }) |
| 47 | +} |
| 48 | + |
| 49 | +func (c *cmdDevicesInspect) Before(cliCtx *cli.Context) error { |
| 50 | + if err := c.cmdDevices.Before(cliCtx); err != nil { |
| 51 | + return err |
| 52 | + } |
| 53 | + return c.validateExpandFlag(cliCtx) |
| 54 | +} |
| 55 | + |
| 56 | +func (c *cmdDevicesInspect) do(ctx context.Context) error { |
| 57 | + query := url.Values{} |
| 58 | + if len(c.expand) != 0 { |
| 59 | + query.Set("expand", strings.Join(c.expand, ",")) |
| 60 | + } |
| 61 | + return c.doHTTPRequest(ctx, doHTTPRequestParams{ |
| 62 | + Method: http.MethodGet, |
| 63 | + Path: "/" + c.deviceID, |
| 64 | + Query: query, |
| 65 | + }) |
| 66 | +} |
0 commit comments