Updated CLI entries

This commit is contained in:
Juan Font 2023-05-01 14:50:38 +00:00
parent cf22604a4b
commit 46221cc220
3 changed files with 78 additions and 78 deletions

View File

@ -57,7 +57,7 @@ var debugCmd = &cobra.Command{
var createNodeCmd = &cobra.Command{ var createNodeCmd = &cobra.Command{
Use: "create-node", Use: "create-node",
Short: "Create a node (machine) that can be registered with `nodes register <>` command", Short: "Create a node that can be registered with `nodes register <>` command",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
output, _ := cmd.Flags().GetString("output") output, _ := cmd.Flags().GetString("output")
@ -83,7 +83,7 @@ var createNodeCmd = &cobra.Command{
return return
} }
machineKey, err := cmd.Flags().GetString("key") nodeKey, err := cmd.Flags().GetString("key")
if err != nil { if err != nil {
ErrorOutput( ErrorOutput(
err, err,
@ -93,7 +93,7 @@ var createNodeCmd = &cobra.Command{
return return
} }
if !headscale.NodePublicKeyRegex.Match([]byte(machineKey)) { if !headscale.NodePublicKeyRegex.Match([]byte(nodeKey)) {
err = errPreAuthKeyMalformed err = errPreAuthKeyMalformed
ErrorOutput( ErrorOutput(
err, err,
@ -115,24 +115,24 @@ var createNodeCmd = &cobra.Command{
return return
} }
request := &v1.DebugCreateMachineRequest{ request := &v1.DebugCreateNodeRequest{
Key: machineKey, Key: nodeKey,
Name: name, Name: name,
User: user, User: user,
Routes: routes, Routes: routes,
} }
response, err := client.DebugCreateMachine(ctx, request) response, err := client.DebugCreateNode(ctx, request)
if err != nil { if err != nil {
ErrorOutput( ErrorOutput(
err, err,
fmt.Sprintf("Cannot create machine: %s", status.Convert(err).Message()), fmt.Sprintf("Cannot create node: %s", status.Convert(err).Message()),
output, output,
) )
return return
} }
SuccessOutput(response.Machine, "Machine created", output) SuccessOutput(response.Node, "Node created", output)
}, },
} }

View File

@ -107,7 +107,7 @@ var nodeCmd = &cobra.Command{
var registerNodeCmd = &cobra.Command{ var registerNodeCmd = &cobra.Command{
Use: "register", Use: "register",
Short: "Registers a machine to your network", Short: "Registers a node to your network",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
output, _ := cmd.Flags().GetString("output") output, _ := cmd.Flags().GetString("output")
user, err := cmd.Flags().GetString("user") user, err := cmd.Flags().GetString("user")
@ -132,12 +132,12 @@ var registerNodeCmd = &cobra.Command{
return return
} }
request := &v1.RegisterMachineRequest{ request := &v1.RegisterNodeRequest{
Key: machineKey, Key: machineKey,
User: user, User: user,
} }
response, err := client.RegisterMachine(ctx, request) response, err := client.RegisterNode(ctx, request)
if err != nil { if err != nil {
ErrorOutput( ErrorOutput(
err, err,
@ -152,8 +152,8 @@ var registerNodeCmd = &cobra.Command{
} }
SuccessOutput( SuccessOutput(
response.Machine, response.Node,
fmt.Sprintf("Machine %s registered", response.Machine.GivenName), output) fmt.Sprintf("Node %s registered", response.Node.GivenName), output)
}, },
} }
@ -180,11 +180,11 @@ var listNodesCmd = &cobra.Command{
defer cancel() defer cancel()
defer conn.Close() defer conn.Close()
request := &v1.ListMachinesRequest{ request := &v1.ListNodesRequest{
User: user, User: user,
} }
response, err := client.ListMachines(ctx, request) response, err := client.ListNodes(ctx, request)
if err != nil { if err != nil {
ErrorOutput( ErrorOutput(
err, err,
@ -196,12 +196,12 @@ var listNodesCmd = &cobra.Command{
} }
if output != "" { if output != "" {
SuccessOutput(response.Machines, "", output) SuccessOutput(response.Nodes, "", output)
return return
} }
tableData, err := nodesToPtables(user, showTags, response.Machines) tableData, err := nodesToPtables(user, showTags, response.Nodes)
if err != nil { if err != nil {
ErrorOutput(err, fmt.Sprintf("Error converting to table: %s", err), output) ErrorOutput(err, fmt.Sprintf("Error converting to table: %s", err), output)
@ -244,11 +244,11 @@ var expireNodeCmd = &cobra.Command{
defer cancel() defer cancel()
defer conn.Close() defer conn.Close()
request := &v1.ExpireMachineRequest{ request := &v1.ExpireNodeRequest{
MachineId: identifier, NodeId: identifier,
} }
response, err := client.ExpireMachine(ctx, request) response, err := client.ExpireNode(ctx, request)
if err != nil { if err != nil {
ErrorOutput( ErrorOutput(
err, err,
@ -262,7 +262,7 @@ var expireNodeCmd = &cobra.Command{
return return
} }
SuccessOutput(response.Machine, "Machine expired", output) SuccessOutput(response.Node, "Node expired", output)
}, },
} }
@ -291,12 +291,12 @@ var renameNodeCmd = &cobra.Command{
if len(args) > 0 { if len(args) > 0 {
newName = args[0] newName = args[0]
} }
request := &v1.RenameMachineRequest{ request := &v1.RenameNodeRequest{
MachineId: identifier, NodeId: identifier,
NewName: newName, NewName: newName,
} }
response, err := client.RenameMachine(ctx, request) response, err := client.RenameNode(ctx, request)
if err != nil { if err != nil {
ErrorOutput( ErrorOutput(
err, err,
@ -310,7 +310,7 @@ var renameNodeCmd = &cobra.Command{
return return
} }
SuccessOutput(response.Machine, "Machine renamed", output) SuccessOutput(response.Node, "Node renamed", output)
}, },
} }
@ -336,11 +336,11 @@ var deleteNodeCmd = &cobra.Command{
defer cancel() defer cancel()
defer conn.Close() defer conn.Close()
getRequest := &v1.GetMachineRequest{ getRequest := &v1.GetNodeRequest{
MachineId: identifier, NodeId: identifier,
} }
getResponse, err := client.GetMachine(ctx, getRequest) getResponse, err := client.GetNode(ctx, getRequest)
if err != nil { if err != nil {
ErrorOutput( ErrorOutput(
err, err,
@ -354,8 +354,8 @@ var deleteNodeCmd = &cobra.Command{
return return
} }
deleteRequest := &v1.DeleteMachineRequest{ deleteRequest := &v1.DeleteNodeRequest{
MachineId: identifier, NodeId: identifier,
} }
confirm := false confirm := false
@ -364,7 +364,7 @@ var deleteNodeCmd = &cobra.Command{
prompt := &survey.Confirm{ prompt := &survey.Confirm{
Message: fmt.Sprintf( Message: fmt.Sprintf(
"Do you want to remove the node %s?", "Do you want to remove the node %s?",
getResponse.GetMachine().Name, getResponse.GetNode().Name,
), ),
} }
err = survey.AskOne(prompt, &confirm) err = survey.AskOne(prompt, &confirm)
@ -374,7 +374,7 @@ var deleteNodeCmd = &cobra.Command{
} }
if confirm || force { if confirm || force {
response, err := client.DeleteMachine(ctx, deleteRequest) response, err := client.DeleteNode(ctx, deleteRequest)
if output != "" { if output != "" {
SuccessOutput(response, "", output) SuccessOutput(response, "", output)
@ -436,11 +436,11 @@ var moveNodeCmd = &cobra.Command{
defer cancel() defer cancel()
defer conn.Close() defer conn.Close()
getRequest := &v1.GetMachineRequest{ getRequest := &v1.GetNodeRequest{
MachineId: identifier, NodeId: identifier,
} }
_, err = client.GetMachine(ctx, getRequest) _, err = client.GetNode(ctx, getRequest)
if err != nil { if err != nil {
ErrorOutput( ErrorOutput(
err, err,
@ -454,12 +454,12 @@ var moveNodeCmd = &cobra.Command{
return return
} }
moveRequest := &v1.MoveMachineRequest{ moveRequest := &v1.MoveNodeRequest{
MachineId: identifier, NodeId: identifier,
User: user, User: user,
} }
moveResponse, err := client.MoveMachine(ctx, moveRequest) moveResponse, err := client.MoveNode(ctx, moveRequest)
if err != nil { if err != nil {
ErrorOutput( ErrorOutput(
err, err,
@ -473,14 +473,14 @@ var moveNodeCmd = &cobra.Command{
return return
} }
SuccessOutput(moveResponse.Machine, "Node moved to another user", output) SuccessOutput(moveResponse.Node, "Node moved to another user", output)
}, },
} }
func nodesToPtables( func nodesToPtables(
currentUser string, currentUser string,
showTags bool, showTags bool,
machines []*v1.Machine, nodes []*v1.Node,
) (pterm.TableData, error) { ) (pterm.TableData, error) {
tableHeader := []string{ tableHeader := []string{
"ID", "ID",
@ -505,23 +505,23 @@ func nodesToPtables(
} }
tableData := pterm.TableData{tableHeader} tableData := pterm.TableData{tableHeader}
for _, machine := range machines { for _, node := range nodes {
var ephemeral bool var ephemeral bool
if machine.PreAuthKey != nil && machine.PreAuthKey.Ephemeral { if node.PreAuthKey != nil && node.PreAuthKey.Ephemeral {
ephemeral = true ephemeral = true
} }
var lastSeen time.Time var lastSeen time.Time
var lastSeenTime string var lastSeenTime string
if machine.LastSeen != nil { if node.LastSeen != nil {
lastSeen = machine.LastSeen.AsTime() lastSeen = node.LastSeen.AsTime()
lastSeenTime = lastSeen.Format("2006-01-02 15:04:05") lastSeenTime = lastSeen.Format("2006-01-02 15:04:05")
} }
var expiry time.Time var expiry time.Time
var expiryTime string var expiryTime string
if machine.Expiry != nil { if node.Expiry != nil {
expiry = machine.Expiry.AsTime() expiry = node.Expiry.AsTime()
expiryTime = expiry.Format("2006-01-02 15:04:05") expiryTime = expiry.Format("2006-01-02 15:04:05")
} else { } else {
expiryTime = "N/A" expiryTime = "N/A"
@ -529,7 +529,7 @@ func nodesToPtables(
var machineKey key.MachinePublic var machineKey key.MachinePublic
err := machineKey.UnmarshalText( err := machineKey.UnmarshalText(
[]byte(headscale.MachinePublicKeyEnsurePrefix(machine.MachineKey)), []byte(headscale.MachinePublicKeyEnsurePrefix(node.MachineKey)),
) )
if err != nil { if err != nil {
machineKey = key.MachinePublic{} machineKey = key.MachinePublic{}
@ -537,14 +537,14 @@ func nodesToPtables(
var nodeKey key.NodePublic var nodeKey key.NodePublic
err = nodeKey.UnmarshalText( err = nodeKey.UnmarshalText(
[]byte(headscale.NodePublicKeyEnsurePrefix(machine.NodeKey)), []byte(headscale.NodePublicKeyEnsurePrefix(node.NodeKey)),
) )
if err != nil { if err != nil {
return nil, err return nil, err
} }
var online string var online string
if machine.Online { if node.Online {
online = pterm.LightGreen("online") online = pterm.LightGreen("online")
} else { } else {
online = pterm.LightRed("offline") online = pterm.LightRed("offline")
@ -558,36 +558,36 @@ func nodesToPtables(
} }
var forcedTags string var forcedTags string
for _, tag := range machine.ForcedTags { for _, tag := range node.ForcedTags {
forcedTags += "," + tag forcedTags += "," + tag
} }
forcedTags = strings.TrimLeft(forcedTags, ",") forcedTags = strings.TrimLeft(forcedTags, ",")
var invalidTags string var invalidTags string
for _, tag := range machine.InvalidTags { for _, tag := range node.InvalidTags {
if !contains(machine.ForcedTags, tag) { if !contains(node.ForcedTags, tag) {
invalidTags += "," + pterm.LightRed(tag) invalidTags += "," + pterm.LightRed(tag)
} }
} }
invalidTags = strings.TrimLeft(invalidTags, ",") invalidTags = strings.TrimLeft(invalidTags, ",")
var validTags string var validTags string
for _, tag := range machine.ValidTags { for _, tag := range node.ValidTags {
if !contains(machine.ForcedTags, tag) { if !contains(node.ForcedTags, tag) {
validTags += "," + pterm.LightGreen(tag) validTags += "," + pterm.LightGreen(tag)
} }
} }
validTags = strings.TrimLeft(validTags, ",") validTags = strings.TrimLeft(validTags, ",")
var user string var user string
if currentUser == "" || (currentUser == machine.User.Name) { if currentUser == "" || (currentUser == node.User.Name) {
user = pterm.LightMagenta(machine.User.Name) user = pterm.LightMagenta(node.User.Name)
} else { } else {
// Shared into this user // Shared into this user
user = pterm.LightYellow(machine.User.Name) user = pterm.LightYellow(node.User.Name)
} }
var IPV4Address string var IPV4Address string
var IPV6Address string var IPV6Address string
for _, addr := range machine.IpAddresses { for _, addr := range node.IpAddresses {
if netip.MustParseAddr(addr).Is4() { if netip.MustParseAddr(addr).Is4() {
IPV4Address = addr IPV4Address = addr
} else { } else {
@ -596,9 +596,9 @@ func nodesToPtables(
} }
nodeData := []string{ nodeData := []string{
strconv.FormatUint(machine.Id, headscale.Base10), strconv.FormatUint(node.Id, headscale.Base10),
machine.Name, node.Name,
machine.GetGivenName(), node.GetGivenName(),
machineKey.ShortString(), machineKey.ShortString(),
nodeKey.ShortString(), nodeKey.ShortString(),
user, user,
@ -655,8 +655,8 @@ var tagCmd = &cobra.Command{
// Sending tags to machine // Sending tags to machine
request := &v1.SetTagsRequest{ request := &v1.SetTagsRequest{
MachineId: identifier, NodeId: identifier,
Tags: tagsToSet, Tags: tagsToSet,
} }
resp, err := client.SetTags(ctx, request) resp, err := client.SetTags(ctx, request)
if err != nil { if err != nil {
@ -671,8 +671,8 @@ var tagCmd = &cobra.Command{
if resp != nil { if resp != nil {
SuccessOutput( SuccessOutput(
resp.GetMachine(), resp.GetNode(),
"Machine updated", "Node updated",
output, output,
) )
} }

View File

@ -57,11 +57,11 @@ var listRoutesCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
output, _ := cmd.Flags().GetString("output") output, _ := cmd.Flags().GetString("output")
machineID, err := cmd.Flags().GetUint64("identifier") nodeID, err := cmd.Flags().GetUint64("identifier")
if err != nil { if err != nil {
ErrorOutput( ErrorOutput(
err, err,
fmt.Sprintf("Error getting machine id from flag: %s", err), fmt.Sprintf("Error getting node id from flag: %s", err),
output, output,
) )
@ -74,7 +74,7 @@ var listRoutesCmd = &cobra.Command{
var routes []*v1.Route var routes []*v1.Route
if machineID == 0 { if nodeID == 0 {
response, err := client.GetRoutes(ctx, &v1.GetRoutesRequest{}) response, err := client.GetRoutes(ctx, &v1.GetRoutesRequest{})
if err != nil { if err != nil {
ErrorOutput( ErrorOutput(
@ -94,13 +94,13 @@ var listRoutesCmd = &cobra.Command{
routes = response.Routes routes = response.Routes
} else { } else {
response, err := client.GetMachineRoutes(ctx, &v1.GetMachineRoutesRequest{ response, err := client.GetNodeRoutes(ctx, &v1.GetNodeRoutesRequest{
MachineId: machineID, NodeId: nodeID,
}) })
if err != nil { if err != nil {
ErrorOutput( ErrorOutput(
err, err,
fmt.Sprintf("Cannot get routes for machine %d: %s", machineID, status.Convert(err).Message()), fmt.Sprintf("Cannot get routes for node %d: %s", nodeID, status.Convert(err).Message()),
output, output,
) )
@ -147,7 +147,7 @@ var enableRouteCmd = &cobra.Command{
if err != nil { if err != nil {
ErrorOutput( ErrorOutput(
err, err,
fmt.Sprintf("Error getting machine id from flag: %s", err), fmt.Sprintf("Error getting node id from flag: %s", err),
output, output,
) )
@ -190,7 +190,7 @@ var disableRouteCmd = &cobra.Command{
if err != nil { if err != nil {
ErrorOutput( ErrorOutput(
err, err,
fmt.Sprintf("Error getting machine id from flag: %s", err), fmt.Sprintf("Error getting node id from flag: %s", err),
output, output,
) )
@ -233,7 +233,7 @@ var deleteRouteCmd = &cobra.Command{
if err != nil { if err != nil {
ErrorOutput( ErrorOutput(
err, err,
fmt.Sprintf("Error getting machine id from flag: %s", err), fmt.Sprintf("Error getting node id from flag: %s", err),
output, output,
) )
@ -267,7 +267,7 @@ var deleteRouteCmd = &cobra.Command{
// routesToPtables converts the list of routes to a nice table. // routesToPtables converts the list of routes to a nice table.
func routesToPtables(routes []*v1.Route) pterm.TableData { func routesToPtables(routes []*v1.Route) pterm.TableData {
tableData := pterm.TableData{{"ID", "Machine", "Prefix", "Advertised", "Enabled", "Primary"}} tableData := pterm.TableData{{"ID", "Node", "Prefix", "Advertised", "Enabled", "Primary"}}
for _, route := range routes { for _, route := range routes {
var isPrimaryStr string var isPrimaryStr string
@ -286,7 +286,7 @@ func routesToPtables(routes []*v1.Route) pterm.TableData {
tableData = append(tableData, tableData = append(tableData,
[]string{ []string{
strconv.FormatUint(route.Id, Base10), strconv.FormatUint(route.Id, Base10),
route.Machine.GivenName, route.Node.GivenName,
route.Prefix, route.Prefix,
strconv.FormatBool(route.Advertised), strconv.FormatBool(route.Advertised),
strconv.FormatBool(route.Enabled), strconv.FormatBool(route.Enabled),