2021-04-28 16:15:45 +02:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2021-05-08 13:58:51 +02:00
|
|
|
"strings"
|
2021-04-28 16:15:45 +02:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2021-07-25 15:08:40 +02:00
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(routesCmd)
|
|
|
|
routesCmd.PersistentFlags().StringP("namespace", "n", "", "Namespace")
|
2021-07-25 16:26:15 +02:00
|
|
|
err := routesCmd.MarkPersistentFlagRequired("namespace")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf(err.Error())
|
|
|
|
}
|
2021-07-25 15:08:40 +02:00
|
|
|
routesCmd.AddCommand(listRoutesCmd)
|
|
|
|
routesCmd.AddCommand(enableRouteCmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
var routesCmd = &cobra.Command{
|
2021-04-30 00:23:26 +02:00
|
|
|
Use: "routes",
|
|
|
|
Short: "Manage the routes of Headscale",
|
|
|
|
}
|
|
|
|
|
2021-07-25 15:08:40 +02:00
|
|
|
var listRoutesCmd = &cobra.Command{
|
2021-04-30 00:23:26 +02:00
|
|
|
Use: "list NODE",
|
2021-04-28 16:15:45 +02:00
|
|
|
Short: "List the routes exposed by this node",
|
|
|
|
Args: func(cmd *cobra.Command, args []string) error {
|
2021-04-30 00:23:26 +02:00
|
|
|
if len(args) < 1 {
|
2021-04-28 16:15:45 +02:00
|
|
|
return fmt.Errorf("Missing parameters")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2021-04-30 00:23:26 +02:00
|
|
|
n, err := cmd.Flags().GetString("namespace")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error getting namespace: %s", err)
|
|
|
|
}
|
2021-05-08 13:58:51 +02:00
|
|
|
o, _ := cmd.Flags().GetString("output")
|
2021-04-30 00:23:26 +02:00
|
|
|
|
2021-04-28 16:15:45 +02:00
|
|
|
h, err := getHeadscaleApp()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error initializing: %s", err)
|
|
|
|
}
|
2021-04-30 00:23:26 +02:00
|
|
|
routes, err := h.GetNodeRoutes(n, args[0])
|
2021-05-08 13:58:51 +02:00
|
|
|
|
|
|
|
if strings.HasPrefix(o, "json") {
|
2021-05-08 17:06:36 +02:00
|
|
|
JsonOutput(routes, err, o)
|
2021-05-08 13:58:51 +02:00
|
|
|
return
|
|
|
|
}
|
2021-05-08 17:06:36 +02:00
|
|
|
|
2021-04-28 16:15:45 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
2021-05-08 13:58:51 +02:00
|
|
|
|
2021-04-28 16:55:29 +02:00
|
|
|
fmt.Println(routes)
|
2021-04-28 16:15:45 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-07-25 15:08:40 +02:00
|
|
|
var enableRouteCmd = &cobra.Command{
|
2021-04-30 00:23:26 +02:00
|
|
|
Use: "enable node-name route",
|
2021-04-28 16:15:45 +02: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-30 00:23:26 +02:00
|
|
|
if len(args) < 2 {
|
2021-04-28 16:15:45 +02:00
|
|
|
return fmt.Errorf("Missing parameters")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2021-04-30 00:23:26 +02:00
|
|
|
n, err := cmd.Flags().GetString("namespace")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error getting namespace: %s", err)
|
|
|
|
}
|
2021-05-08 13:58:51 +02:00
|
|
|
o, _ := cmd.Flags().GetString("output")
|
2021-04-30 00:23:26 +02:00
|
|
|
|
2021-04-28 16:15:45 +02:00
|
|
|
h, err := getHeadscaleApp()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error initializing: %s", err)
|
|
|
|
}
|
2021-05-08 13:58:51 +02:00
|
|
|
route, err := h.EnableNodeRoute(n, args[0], args[1])
|
|
|
|
if strings.HasPrefix(o, "json") {
|
2021-05-08 17:06:36 +02:00
|
|
|
JsonOutput(route, err, o)
|
2021-05-08 13:58:51 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-28 16:15:45 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
2021-05-08 13:58:51 +02:00
|
|
|
fmt.Printf("Enabled route %s\n", route)
|
2021-04-28 16:15:45 +02:00
|
|
|
},
|
|
|
|
}
|