2021-04-28 08:15:45 -06:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2021-05-08 05:58:51 -06:00
|
|
|
"strings"
|
2021-04-28 08:15:45 -06:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2021-04-29 16:23:26 -06:00
|
|
|
var RoutesCmd = &cobra.Command{
|
|
|
|
Use: "routes",
|
|
|
|
Short: "Manage the routes of Headscale",
|
|
|
|
}
|
|
|
|
|
2021-04-28 08:15:45 -06:00
|
|
|
var ListRoutesCmd = &cobra.Command{
|
2021-04-29 16:23:26 -06:00
|
|
|
Use: "list NODE",
|
2021-04-28 08:15:45 -06:00
|
|
|
Short: "List the routes exposed by this node",
|
|
|
|
Args: func(cmd *cobra.Command, args []string) error {
|
2021-04-29 16:23:26 -06:00
|
|
|
if len(args) < 1 {
|
2021-04-28 08:15:45 -06:00
|
|
|
return fmt.Errorf("Missing parameters")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2021-04-29 16:23:26 -06:00
|
|
|
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")
|
2021-04-29 16:23:26 -06:00
|
|
|
|
2021-04-28 08:15:45 -06:00
|
|
|
h, err := getHeadscaleApp()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error initializing: %s", err)
|
|
|
|
}
|
2021-04-29 16:23:26 -06:00
|
|
|
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
|
|
|
|
2021-04-28 08:15:45 -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)
|
2021-04-28 08:15:45 -06:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var EnableRouteCmd = &cobra.Command{
|
2021-04-29 16:23:26 -06:00
|
|
|
Use: "enable node-name route",
|
2021-04-28 08:15:45 -06:00
|
|
|
Short: "Allows exposing a route declared by this node to the rest of the nodes",
|
|
|
|
Args: func(cmd *cobra.Command, args []string) error {
|
2021-04-29 16:23:26 -06:00
|
|
|
if len(args) < 2 {
|
2021-04-28 08:15:45 -06:00
|
|
|
return fmt.Errorf("Missing parameters")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2021-04-29 16:23:26 -06:00
|
|
|
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")
|
2021-04-29 16:23:26 -06:00
|
|
|
|
2021-04-28 08:15:45 -06:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-04-28 08:15:45 -06:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
2021-05-08 05:58:51 -06:00
|
|
|
fmt.Printf("Enabled route %s\n", route)
|
2021-04-28 08:15:45 -06:00
|
|
|
},
|
|
|
|
}
|