Skip to content

Commit 093b120

Browse files
committed
feat: add device inspect and list commands
1 parent 0157302 commit 093b120

File tree

6 files changed

+161
-0
lines changed

6 files changed

+161
-0
lines changed

internal/app/enaptercli/cmd_device.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ func buildCmdDevices() *cli.Command {
2020
Name: "device",
2121
Usage: "Manage devices",
2222
Subcommands: []*cli.Command{
23+
buildCmdDevicesList(),
24+
buildCmdDevicesInspect(),
2325
buildCmdDevicesAssignBlueprint(),
2426
buildCmdDevicesLogs(),
2527
buildCmdDevicesLogsf(),
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 cmdDevicesList struct {
13+
cmdDevices
14+
expand []string
15+
}
16+
17+
func buildCmdDevicesList() *cli.Command {
18+
cmd := &cmdDevicesList{}
19+
return &cli.Command{
20+
Name: "list",
21+
Usage: "List user devices",
22+
CustomHelpTemplate: cmd.HelpTemplate(),
23+
Flags: cmd.Flags(),
24+
Before: cmd.Before,
25+
Action: func(cliCtx *cli.Context) error {
26+
return cmd.do(cliCtx.Context)
27+
},
28+
}
29+
}
30+
31+
func (c *cmdDevicesList) Flags() []cli.Flag {
32+
flags := c.cmdDevices.Flags()
33+
return append(flags, &cli.MultiStringFlag{
34+
Target: &cli.StringSliceFlag{
35+
Name: "expand",
36+
Usage: "coma separated list of expanded device info",
37+
},
38+
Destination: &c.expand,
39+
})
40+
}
41+
42+
func (c *cmdDevicesList) Before(cliCtx *cli.Context) error {
43+
if err := c.cmdDevices.Before(cliCtx); err != nil {
44+
return err
45+
}
46+
return c.validateExpandFlag(cliCtx)
47+
}
48+
49+
func (c *cmdDevicesList) do(ctx context.Context) error {
50+
query := url.Values{}
51+
if len(c.expand) != 0 {
52+
query.Set("expand", strings.Join(c.expand, ","))
53+
}
54+
55+
return c.doHTTPRequest(ctx, doHTTPRequestParams{
56+
Method: http.MethodGet,
57+
Path: "",
58+
Query: query,
59+
})
60+
}

internal/app/enaptercli/testdata/helps/enapter device

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ USAGE:
55
enaptercli.test device command [command options]
66

77
COMMANDS:
8+
list List user devices
9+
inspect Inspect a devices
810
assign-blueprint Assign blueprint to device
911
logs Show device logs
1012
logsf Follow device logs
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
NAME:
2+
enaptercli.test device inspect - Inspect a devices
3+
4+
USAGE:
5+
enaptercli.test device inspect [command options]
6+
7+
OPTIONS:
8+
--verbose log extra details about operation (default: false)
9+
--device-id value, -d value device ID
10+
--expand value [ --expand value ] coma separated list of expanded device info
11+
--help, -h show help
12+
13+
ENVIRONMENT VARIABLES:
14+
ENAPTER3_API_TOKEN Enapter API access token
15+
ENAPTER3_API_HOST Enapter API base URL (https://api.enapter.com by default)
16+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
NAME:
2+
enaptercli.test device list - List user devices
3+
4+
USAGE:
5+
enaptercli.test device list [command options]
6+
7+
OPTIONS:
8+
--verbose log extra details about operation (default: false)
9+
--expand value [ --expand value ] coma separated list of expanded device info
10+
--help, -h show help
11+
12+
ENVIRONMENT VARIABLES:
13+
ENAPTER3_API_TOKEN Enapter API access token
14+
ENAPTER3_API_HOST Enapter API base URL (https://api.enapter.com by default)
15+

0 commit comments

Comments
 (0)