Switch to named arguments for all nodes subcommands. Update docs

accordingly. Fix integration test failure.
This commit is contained in:
Ward Vandewege 2021-10-28 09:30:41 -04:00
parent 25c67cf2aa
commit f9187bdfc4
4 changed files with 37 additions and 37 deletions

2
api.go
View File

@ -43,7 +43,7 @@ func (h *Headscale) RegisterWebAPI(c *gin.Context) {
<p> <p>
<code> <code>
<b>headscale -n NAMESPACE nodes register %s</b> <b>headscale -n NAMESPACE nodes register -k %s</b>
</code> </code>
</p> </p>

View File

@ -25,8 +25,18 @@ func init() {
if err != nil { if err != nil {
log.Fatalf(err.Error()) log.Fatalf(err.Error())
} }
registerNodeCmd.Flags().StringP("key", "k", "", "Key")
err = registerNodeCmd.MarkFlagRequired("key")
if err != nil {
log.Fatalf(err.Error())
}
nodeCmd.AddCommand(registerNodeCmd) nodeCmd.AddCommand(registerNodeCmd)
deleteNodeCmd.Flags().IntP("identifier", "i", 0, "Node identifier (ID)")
err = deleteNodeCmd.MarkFlagRequired("identifier")
if err != nil {
log.Fatalf(err.Error())
}
nodeCmd.AddCommand(deleteNodeCmd) nodeCmd.AddCommand(deleteNodeCmd)
shareMachineCmd.Flags().StringP("namespace", "n", "", "Namespace") shareMachineCmd.Flags().StringP("namespace", "n", "", "Namespace")
@ -34,6 +44,11 @@ func init() {
if err != nil { if err != nil {
log.Fatalf(err.Error()) log.Fatalf(err.Error())
} }
shareMachineCmd.Flags().IntP("identifier", "i", 0, "Node identifier (ID)")
err = shareMachineCmd.MarkFlagRequired("identifier")
if err != nil {
log.Fatalf(err.Error())
}
nodeCmd.AddCommand(shareMachineCmd) nodeCmd.AddCommand(shareMachineCmd)
unshareMachineCmd.Flags().StringP("namespace", "n", "", "Namespace") unshareMachineCmd.Flags().StringP("namespace", "n", "", "Namespace")
@ -41,6 +56,11 @@ func init() {
if err != nil { if err != nil {
log.Fatalf(err.Error()) log.Fatalf(err.Error())
} }
unshareMachineCmd.Flags().IntP("identifier", "i", 0, "Node identifier (ID)")
err = unshareMachineCmd.MarkFlagRequired("identifier")
if err != nil {
log.Fatalf(err.Error())
}
nodeCmd.AddCommand(unshareMachineCmd) nodeCmd.AddCommand(unshareMachineCmd)
} }
@ -50,14 +70,8 @@ var nodeCmd = &cobra.Command{
} }
var registerNodeCmd = &cobra.Command{ var registerNodeCmd = &cobra.Command{
Use: "register machineID", Use: "register",
Short: "Registers a machine to your network", Short: "Registers a machine to your network",
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) { Run: func(cmd *cobra.Command, args []string) {
n, err := cmd.Flags().GetString("namespace") n, err := cmd.Flags().GetString("namespace")
if err != nil { if err != nil {
@ -69,7 +83,11 @@ var registerNodeCmd = &cobra.Command{
if err != nil { if err != nil {
log.Fatalf("Error initializing: %s", err) log.Fatalf("Error initializing: %s", err)
} }
m, err := h.RegisterMachine(args[0], n) machineIDStr, err := cmd.Flags().GetString("key")
if err != nil {
log.Fatalf("Error getting machine ID: %s", err)
}
m, err := h.RegisterMachine(machineIDStr, n)
if strings.HasPrefix(o, "json") { if strings.HasPrefix(o, "json") {
JsonOutput(m, err, o) JsonOutput(m, err, o)
return return
@ -157,21 +175,15 @@ var listNodesCmd = &cobra.Command{
} }
var deleteNodeCmd = &cobra.Command{ var deleteNodeCmd = &cobra.Command{
Use: "delete ID", Use: "delete",
Short: "Delete a node", Short: "Delete a 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) { Run: func(cmd *cobra.Command, args []string) {
output, _ := cmd.Flags().GetString("output") output, _ := cmd.Flags().GetString("output")
h, err := getHeadscaleApp() h, err := getHeadscaleApp()
if err != nil { if err != nil {
log.Fatalf("Error initializing: %s", err) log.Fatalf("Error initializing: %s", err)
} }
id, err := strconv.Atoi(args[0]) id, err := cmd.Flags().GetInt("identifier")
if err != nil { if err != nil {
log.Fatalf("Error converting ID to integer: %s", err) log.Fatalf("Error converting ID to integer: %s", err)
} }
@ -227,10 +239,10 @@ func sharingWorker(cmd *cobra.Command, args []string) (*headscale.Headscale, str
namespace, err := h.GetNamespace(namespaceStr) namespace, err := h.GetNamespace(namespaceStr)
if err != nil { if err != nil {
log.Fatalf("Error fetching namespace %s: %s", n, err) log.Fatalf("Error fetching namespace %s: %s", namespaceStr, err)
} }
id, err := strconv.Atoi(args[0]) id, err := cmd.Flags().GetInt("identifier")
if err != nil { if err != nil {
log.Fatalf("Error converting ID to integer: %s", err) log.Fatalf("Error converting ID to integer: %s", err)
} }
@ -243,14 +255,8 @@ func sharingWorker(cmd *cobra.Command, args []string) (*headscale.Headscale, str
} }
var shareMachineCmd = &cobra.Command{ var shareMachineCmd = &cobra.Command{
Use: "share ID", Use: "share",
Short: "Shares a node from the current namespace to the specified one", Short: "Shares a node from the current namespace to the specified one",
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) { Run: func(cmd *cobra.Command, args []string) {
h, output, machine, namespace := sharingWorker(cmd, args) h, output, machine, namespace := sharingWorker(cmd, args)
err := h.AddSharedMachineToNamespace(machine, namespace) err := h.AddSharedMachineToNamespace(machine, namespace)
@ -268,14 +274,8 @@ var shareMachineCmd = &cobra.Command{
} }
var unshareMachineCmd = &cobra.Command{ var unshareMachineCmd = &cobra.Command{
Use: "unshare ID", Use: "unshare",
Short: "Unshares a node from the specified namespace", Short: "Unshares a node from the specified namespace",
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) { Run: func(cmd *cobra.Command, args []string) {
h, output, machine, namespace := sharingWorker(cmd, args) h, output, machine, namespace := sharingWorker(cmd, args)
err := h.RemoveSharedMachineFromNamespace(machine, namespace) err := h.RemoveSharedMachineFromNamespace(machine, namespace)

View File

@ -97,7 +97,7 @@
9. In the server, register your machine to a namespace with the CLI 9. In the server, register your machine to a namespace with the CLI
```shell ```shell
headscale -n myfirstnamespace nodes register YOURMACHINEKEY headscale -n myfirstnamespace nodes register -k YOURMACHINEKEY
``` ```
or docker: or docker:
```shell ```shell
@ -106,11 +106,11 @@
-v $(pwd)/config.json:/config.json \ -v $(pwd)/config.json:/config.json \
-v $(pwd)/derp.yaml:/derp.yaml \ -v $(pwd)/derp.yaml:/derp.yaml \
headscale/headscale:x.x.x \ headscale/headscale:x.x.x \
headscale -n myfirstnamespace nodes register YOURMACHINEKEY headscale -n myfirstnamespace nodes register -k YOURMACHINEKEY
``` ```
or if your server is already running in docker: or if your server is already running in docker:
```shell ```shell
docker exec <container_name> headscale -n myfirstnamespace nodes register YOURMACHINEKEY docker exec <container_name> headscale -n myfirstnamespace nodes register -k YOURMACHINEKEY
``` ```
Alternatively, you can use Auth Keys to register your machines: Alternatively, you can use Auth Keys to register your machines:

View File

@ -493,7 +493,7 @@ func (s *IntegrationTestSuite) TestSharedNodes() {
result, err := executeCommand( result, err := executeCommand(
&headscale, &headscale,
[]string{"headscale", "nodes", "share", fmt.Sprint(machine.ID), "--namespace", "main"}, []string{"headscale", "nodes", "share", "--identifier", fmt.Sprint(machine.ID), "--namespace", "main"},
[]string{}, []string{},
) )
assert.Nil(s.T(), err) assert.Nil(s.T(), err)