Skip to content

Commit 2b2236d

Browse files
committed
feat: list and inspect sites
1 parent d334735 commit 2b2236d

8 files changed

Lines changed: 167 additions & 0 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package enaptercli
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/url"
7+
8+
"github.com/urfave/cli/v2"
9+
)
10+
11+
type cmdSite struct {
12+
cmdBase
13+
}
14+
15+
func buildCmdSites() *cli.Command {
16+
return &cli.Command{
17+
Name: "site",
18+
Usage: "Manage sites",
19+
Subcommands: []*cli.Command{
20+
buildCmdSitesList(),
21+
buildCmdSiteInspect(),
22+
},
23+
}
24+
}
25+
26+
func (c *cmdSite) doHTTPRequest(ctx context.Context, p doHTTPRequestParams) error {
27+
path, err := url.JoinPath("/sites", p.Path)
28+
if err != nil {
29+
return fmt.Errorf("join path: %w", err)
30+
}
31+
p.Path = path
32+
return c.cmdBase.doHTTPRequest(ctx, p)
33+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package enaptercli
2+
3+
import (
4+
"context"
5+
"net/http"
6+
7+
"github.com/urfave/cli/v2"
8+
)
9+
10+
type cmdSiteInspect struct {
11+
cmdSite
12+
siteID string
13+
}
14+
15+
func buildCmdSiteInspect() *cli.Command {
16+
cmd := &cmdSiteInspect{}
17+
return &cli.Command{
18+
Name: "inspect",
19+
Usage: "Inspect a site",
20+
CustomHelpTemplate: cmd.HelpTemplate(),
21+
Flags: cmd.Flags(),
22+
Before: cmd.Before,
23+
Action: func(cliCtx *cli.Context) error {
24+
return cmd.do(cliCtx.Context)
25+
},
26+
}
27+
}
28+
29+
func (c *cmdSiteInspect) Flags() []cli.Flag {
30+
flags := c.cmdSite.Flags()
31+
return append(flags, &cli.StringFlag{
32+
Name: "site-id",
33+
Usage: "site ID",
34+
Destination: &c.siteID,
35+
Required: true,
36+
})
37+
}
38+
39+
func (c *cmdSiteInspect) do(ctx context.Context) error {
40+
return c.doHTTPRequest(ctx, doHTTPRequestParams{
41+
Method: http.MethodGet,
42+
Path: "/" + c.siteID,
43+
})
44+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package enaptercli
2+
3+
import (
4+
"context"
5+
"net/http"
6+
"net/url"
7+
"strconv"
8+
9+
"github.com/urfave/cli/v2"
10+
)
11+
12+
type cmdSitesList struct {
13+
cmdSite
14+
offset int
15+
limit int
16+
}
17+
18+
func buildCmdSitesList() *cli.Command {
19+
cmd := &cmdSitesList{}
20+
return &cli.Command{
21+
Name: "list",
22+
Usage: "List user sites",
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 *cmdSitesList) do(ctx context.Context) error {
33+
query := url.Values{}
34+
if c.offset != 0 {
35+
query.Set("offset", strconv.Itoa(c.offset))
36+
}
37+
if c.limit != 0 {
38+
query.Set("limit", strconv.Itoa(c.limit))
39+
}
40+
41+
return c.cmdBase.doHTTPRequest(ctx, doHTTPRequestParams{
42+
Method: http.MethodGet,
43+
Path: "/users/me/sites",
44+
Query: query,
45+
})
46+
}

internal/app/enaptercli/execute.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ func NewApp() *cli.App {
2121
"The token can be obtained in your Enapter Cloud account settings."
2222

2323
app.Commands = []*cli.Command{
24+
buildCmdSites(),
2425
buildCmdDevices(),
2526
buildCmdBlueprints(),
2627
buildCmdProvisioning(),

internal/app/enaptercli/testdata/helps/enapter

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ DESCRIPTION:
88
Enapter CLI requires access token for authentication. The token can be obtained in your Enapter Cloud account settings.
99

1010
COMMANDS:
11+
site Manage sites
1112
device Manage devices
1213
blueprint Manage blueprints
1314
provisioning Create devices of different types
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
NAME:
2+
enaptercli.test site - Manage sites
3+
4+
USAGE:
5+
enaptercli.test site command [command options]
6+
7+
COMMANDS:
8+
list List user sites
9+
inspect Inspect a site
10+
help, h Shows a list of commands or help for one command
11+
12+
OPTIONS:
13+
--help, -h show help
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
NAME:
2+
enaptercli.test site inspect - Inspect a site
3+
4+
USAGE:
5+
enaptercli.test site inspect [command options]
6+
7+
OPTIONS:
8+
--verbose log extra details about operation (default: false)
9+
--site-id value site ID
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+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
NAME:
2+
enaptercli.test site list - List user sites
3+
4+
USAGE:
5+
enaptercli.test site list [command options]
6+
7+
OPTIONS:
8+
--verbose log extra details about operation (default: false)
9+
--help, -h show help
10+
11+
ENVIRONMENT VARIABLES:
12+
ENAPTER3_API_TOKEN Enapter API access token
13+
ENAPTER3_API_HOST Enapter API base URL (https://api.enapter.com by default)
14+

0 commit comments

Comments
 (0)