feat: improve nodes list with inputs from @deonthomasgy

cf: https://github.com/juanfont/headscale/compare/main...deonthomasgy:dev/thomas/show-tags
This commit is contained in:
Adrien Raffin-Caboisse 2022-05-13 11:46:28 +02:00
parent 16f9691e80
commit 63d920510d
No known key found for this signature in database
GPG key ID: 7FB60532DEBEAD6A

View file

@ -19,6 +19,7 @@ import (
func init() {
rootCmd.AddCommand(nodeCmd)
listNodesCmd.Flags().StringP("namespace", "n", "", "Filter by namespace")
listNodesCmd.Flags().BoolP("tags", "t", false, "Show tags")
nodeCmd.AddCommand(listNodesCmd)
registerNodeCmd.Flags().StringP("namespace", "n", "", "Namespace")
@ -141,6 +142,12 @@ var listNodesCmd = &cobra.Command{
return
}
showTags, err := cmd.Flags().GetBool("tags")
if err != nil {
ErrorOutput(err, fmt.Sprintf("Error getting tags flag: %s", err), output)
return
}
ctx, client, conn, cancel := getHeadscaleCLIClient()
defer cancel()
@ -167,7 +174,7 @@ var listNodesCmd = &cobra.Command{
return
}
tableData, err := nodesToPtables(namespace, response.Machines)
tableData, err := nodesToPtables(namespace, showTags, response.Machines)
if err != nil {
ErrorOutput(err, fmt.Sprintf("Error converting to table: %s", err), output)
@ -397,10 +404,10 @@ var moveNodeCmd = &cobra.Command{
func nodesToPtables(
currentNamespace string,
showTags bool,
machines []*v1.Machine,
) (pterm.TableData, error) {
tableData := pterm.TableData{
{
tableHeader := []string{
"ID",
"Name",
"NodeKey",
@ -410,9 +417,15 @@ func nodesToPtables(
"Last seen",
"Online",
"Expired",
"Tags",
},
}
if showTags {
tableHeader = append(tableHeader, []string{
"ForcedTags",
"InvalidTags",
"ValidTags",
}...)
}
tableData := pterm.TableData{tableHeader}
for _, machine := range machines {
var ephemeral bool
@ -456,21 +469,25 @@ func nodesToPtables(
expired = pterm.LightRed("yes")
}
var tags string
var forcedTags string
for _, tag := range machine.ForcedTags {
tags += "," + tag
forcedTags += "," + tag
}
forcedTags = strings.TrimLeft(forcedTags, ",")
var invalidTags string
for _, tag := range machine.InvalidTags {
if !contains(machine.ForcedTags, tag) {
tags += "," + pterm.LightRed(tag)
invalidTags += "," + pterm.LightRed(tag)
}
}
invalidTags = strings.TrimLeft(invalidTags, ",")
var validTags string
for _, tag := range machine.ValidTags {
if !contains(machine.ForcedTags, tag) {
tags += "," + pterm.LightGreen(tag)
validTags += "," + pterm.LightGreen(tag)
}
}
tags = strings.TrimLeft(tags, ",")
validTags = strings.TrimLeft(validTags, ",")
var namespace string
if currentNamespace == "" || (currentNamespace == machine.Namespace.Name) {
@ -479,9 +496,7 @@ func nodesToPtables(
// Shared into this namespace
namespace = pterm.LightYellow(machine.Namespace.Name)
}
tableData = append(
tableData,
[]string{
nodeData := []string{
strconv.FormatUint(machine.Id, headscale.Base10),
machine.Name,
nodeKey.ShortString(),
@ -491,8 +506,13 @@ func nodesToPtables(
lastSeenTime,
online,
expired,
tags,
},
}
if showTags {
nodeData = append(nodeData, []string{forcedTags, invalidTags, validTags}...)
}
tableData = append(
tableData,
nodeData,
)
}