TUN-4502: Make `cloudflared tunnel route` subcommands described consistently

This commit is contained in:
Nuno Diegues 2021-06-07 09:20:11 +01:00
parent 98a0844f56
commit f88732277a
2 changed files with 45 additions and 33 deletions

View File

@ -10,9 +10,10 @@
## 2021.5.8 ## 2021.5.8
### New Features ### New Features
- When creating a DNS record to point a hostname at a tunnel, you can now use --overwrite-dns to overwrite any existing - When creating a DNS record to point a hostname at a tunnel, you can now use --overwrite-dns to overwrite any existing
DNS records with that hostname. This works when using the CLI to provision DNS, and when starting an adhoc tunnel, e.g. DNS records with that hostname. This works both when using the CLI to provision DNS, as well as when starting an adhoc
- `cloudflared route --overwrite-dns dns foo foo.example.com` named tunnel, e.g.:
- `cloudflared tunnel --overwrite-dns --name foo --hostname foo.example.com` - `cloudflared tunnel route dns --overwrite-dns foo-tunnel foo.example.com`
- `cloudflared tunnel --overwrite-dns --name foo-tunnel --hostname foo.example.com`
## 2021.5.7 ## 2021.5.7
### New Features ### New Features

View File

@ -655,7 +655,6 @@ func cleanupCommand(c *cli.Context) error {
func buildRouteCommand() *cli.Command { func buildRouteCommand() *cli.Command {
return &cli.Command{ return &cli.Command{
Name: "route", Name: "route",
Action: cliutil.ConfiguredAction(routeCommand),
Usage: "Define which traffic routed from Cloudflare edge to this tunnel: requests to a DNS hostname, to a Cloudflare Load Balancer, or traffic originating from Cloudflare WARP clients", Usage: "Define which traffic routed from Cloudflare edge to this tunnel: requests to a DNS hostname, to a Cloudflare Load Balancer, or traffic originating from Cloudflare WARP clients",
UsageText: "cloudflared tunnel [tunnel command options] route [subcommand options] [dns TUNNEL HOSTNAME]|[lb TUNNEL HOSTNAME LB-POOL]|[ip NETWORK TUNNEL]", UsageText: "cloudflared tunnel [tunnel command options] route [subcommand options] [dns TUNNEL HOSTNAME]|[lb TUNNEL HOSTNAME LB-POOL]|[ip NETWORK TUNNEL]",
Description: `The route command defines how Cloudflare will proxy requests to this tunnel. Description: `The route command defines how Cloudflare will proxy requests to this tunnel.
@ -675,16 +674,30 @@ Further information about managing Cloudflare WARP traffic to your tunnel is ava
`, `,
CustomHelpTemplate: commandHelpTemplate(), CustomHelpTemplate: commandHelpTemplate(),
Subcommands: []*cli.Command{ Subcommands: []*cli.Command{
{
Name: "dns",
Action: cliutil.ConfiguredAction(routeDnsCommand),
Usage: "Route a hostname by creating a DNS CNAME record to a tunnel",
UsageText: "cloudflared tunnel route dns [TUNNEL] [HOSTNAME]",
Description: `Creates a DNS CNAME record hostname that points to the tunnel.`,
Flags: []cli.Flag{overwriteDNSFlag},
},
{
Name: "lb",
Action: cliutil.ConfiguredAction(routeLbCommand),
Usage: "Use this tunnel as a load balancer origin, creating pool and load balancer if necessary",
UsageText: "cloudflared tunnel route dns [TUNNEL] [HOSTNAME] [LB-POOL]",
Description: `Creates Load Balancer with an origin pool that points to the tunnel.`,
},
buildRouteIPSubcommand(), buildRouteIPSubcommand(),
}, },
Flags: []cli.Flag{overwriteDNSFlag},
} }
} }
func dnsRouteFromArg(c *cli.Context, overwriteExisting bool) (tunnelstore.Route, error) { func dnsRouteFromArg(c *cli.Context, overwriteExisting bool) (tunnelstore.Route, error) {
const ( const (
userHostnameIndex = 2 userHostnameIndex = 1
expectedNArgs = 3 expectedNArgs = 2
) )
if c.NArg() != expectedNArgs { if c.NArg() != expectedNArgs {
return nil, cliutil.UsageError("Expected %d arguments, got %d", expectedNArgs, c.NArg()) return nil, cliutil.UsageError("Expected %d arguments, got %d", expectedNArgs, c.NArg())
@ -700,9 +713,9 @@ func dnsRouteFromArg(c *cli.Context, overwriteExisting bool) (tunnelstore.Route,
func lbRouteFromArg(c *cli.Context) (tunnelstore.Route, error) { func lbRouteFromArg(c *cli.Context) (tunnelstore.Route, error) {
const ( const (
lbNameIndex = 2 lbNameIndex = 1
lbPoolIndex = 3 lbPoolIndex = 2
expectedNArgs = 4 expectedNArgs = 3
) )
if c.NArg() != expectedNArgs { if c.NArg() != expectedNArgs {
return nil, cliutil.UsageError("Expected %d arguments, got %d", expectedNArgs, c.NArg()) return nil, cliutil.UsageError("Expected %d arguments, got %d", expectedNArgs, c.NArg())
@ -744,41 +757,39 @@ func validateHostname(s string, allowWildcardSubdomain bool) bool {
return err == nil && validateName(puny, allowWildcardSubdomain) return err == nil && validateName(puny, allowWildcardSubdomain)
} }
func routeCommand(c *cli.Context) error { func routeDnsCommand(c *cli.Context) error {
if c.NArg() < 2 { if c.NArg() != 2 {
return cliutil.UsageError(`"cloudflared tunnel route" requires the first argument to be the route type(dns or lb), followed by the ID or name of the tunnel`) return cliutil.UsageError(`This command expects the format "cloudflared tunnel route dns <tunnel name/id> <hostname>"`)
} }
return routeCommand(c, "dns")
}
func routeLbCommand(c *cli.Context) error {
if c.NArg() != 3 {
return cliutil.UsageError(`This command expects the format "cloudflared tunnel route lb <tunnel name/id> <hostname> <load balancer pool>"`)
}
return routeCommand(c, "lb")
}
func routeCommand(c *cli.Context, routeType string) error {
sc, err := newSubcommandContext(c) sc, err := newSubcommandContext(c)
if err != nil { if err != nil {
return err return err
} }
const tunnelIDIndex = 1 tunnelID, err := sc.findID(c.Args().Get(0))
if err != nil {
routeType := c.Args().First() return err
}
var route tunnelstore.Route var route tunnelstore.Route
var tunnelID uuid.UUID
switch routeType { switch routeType {
case "dns": case "dns":
tunnelID, err = sc.findID(c.Args().Get(tunnelIDIndex))
if err != nil {
return err
}
route, err = dnsRouteFromArg(c, c.Bool(overwriteDNSFlagName)) route, err = dnsRouteFromArg(c, c.Bool(overwriteDNSFlagName))
if err != nil {
return err
}
case "lb": case "lb":
tunnelID, err = sc.findID(c.Args().Get(tunnelIDIndex))
if err != nil {
return err
}
route, err = lbRouteFromArg(c) route, err = lbRouteFromArg(c)
if err != nil { }
return err if err != nil {
} return err
default:
return cliutil.UsageError("%s is not a recognized route type. Supported route types are dns and lb", routeType)
} }
res, err := sc.route(tunnelID, route) res, err := sc.route(tunnelID, route)