headscale/cmd/headscale/cli/routes.go

85 lines
1.7 KiB
Go
Raw Normal View History

package cli
import (
"fmt"
"log"
2021-05-08 05:58:51 -06:00
"strings"
"github.com/spf13/cobra"
)
var RoutesCmd = &cobra.Command{
Use: "routes",
Short: "Manage the routes of Headscale",
}
var ListRoutesCmd = &cobra.Command{
Use: "list NODE",
Short: "List the routes exposed by this node",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return fmt.Errorf("Missing parameters")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
n, err := cmd.Flags().GetString("namespace")
if err != nil {
log.Fatalf("Error getting namespace: %s", err)
}
2021-05-08 05:58:51 -06:00
o, _ := cmd.Flags().GetString("output")
h, err := getHeadscaleApp()
if err != nil {
log.Fatalf("Error initializing: %s", err)
}
routes, err := h.GetNodeRoutes(n, args[0])
2021-05-08 05:58:51 -06:00
if strings.HasPrefix(o, "json") {
2021-05-08 09:06:36 -06:00
JsonOutput(routes, err, o)
2021-05-08 05:58:51 -06:00
return
}
2021-05-08 09:06:36 -06:00
if err != nil {
fmt.Println(err)
return
}
2021-05-08 05:58:51 -06:00
2021-04-28 08:55:29 -06:00
fmt.Println(routes)
},
}
var EnableRouteCmd = &cobra.Command{
Use: "enable node-name route",
Short: "Allows exposing a route declared by this node to the rest of the nodes",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 2 {
return fmt.Errorf("Missing parameters")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
n, err := cmd.Flags().GetString("namespace")
if err != nil {
log.Fatalf("Error getting namespace: %s", err)
}
2021-05-08 05:58:51 -06:00
o, _ := cmd.Flags().GetString("output")
h, err := getHeadscaleApp()
if err != nil {
log.Fatalf("Error initializing: %s", err)
}
2021-05-08 05:58:51 -06:00
route, err := h.EnableNodeRoute(n, args[0], args[1])
if strings.HasPrefix(o, "json") {
2021-05-08 09:06:36 -06:00
JsonOutput(route, err, o)
2021-05-08 05:58:51 -06:00
return
}
if err != nil {
fmt.Println(err)
return
}
2021-05-08 05:58:51 -06:00
fmt.Printf("Enabled route %s\n", route)
},
}