github.com/hernad/nomad@v1.6.112/command/namespace_list.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package command
     5  
     6  import (
     7  	"fmt"
     8  	"sort"
     9  	"strings"
    10  
    11  	"github.com/hernad/nomad/api"
    12  	"github.com/posener/complete"
    13  )
    14  
    15  type NamespaceListCommand struct {
    16  	Meta
    17  }
    18  
    19  func (c *NamespaceListCommand) Help() string {
    20  	helpText := `
    21  Usage: nomad namespace list [options]
    22  
    23    List is used to list available namespaces.
    24  
    25    If ACLs are enabled, this command requires a management ACL token to view
    26    all namespaces. A non-management token can be used to list namespaces for
    27    which it has an associated capability.
    28  
    29  General Options:
    30  
    31    ` + generalOptionsUsage(usageOptsDefault|usageOptsNoNamespace) + `
    32  
    33  List Options:
    34  
    35    -json
    36      Output the namespaces in a JSON format.
    37  
    38    -t
    39      Format and display the namespaces using a Go template.
    40  `
    41  	return strings.TrimSpace(helpText)
    42  }
    43  
    44  func (c *NamespaceListCommand) AutocompleteFlags() complete.Flags {
    45  	return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
    46  		complete.Flags{
    47  			"-json": complete.PredictNothing,
    48  			"-t":    complete.PredictAnything,
    49  		})
    50  }
    51  
    52  func (c *NamespaceListCommand) AutocompleteArgs() complete.Predictor {
    53  	return complete.PredictNothing
    54  }
    55  
    56  func (c *NamespaceListCommand) Synopsis() string {
    57  	return "List namespaces"
    58  }
    59  
    60  func (c *NamespaceListCommand) Name() string { return "namespace list" }
    61  
    62  func (c *NamespaceListCommand) Run(args []string) int {
    63  	var json bool
    64  	var tmpl string
    65  
    66  	flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
    67  	flags.Usage = func() { c.Ui.Output(c.Help()) }
    68  	flags.BoolVar(&json, "json", false, "")
    69  	flags.StringVar(&tmpl, "t", "", "")
    70  
    71  	if err := flags.Parse(args); err != nil {
    72  		return 1
    73  	}
    74  
    75  	// Check that we got no arguments
    76  	args = flags.Args()
    77  	if l := len(args); l != 0 {
    78  		c.Ui.Error("This command takes no arguments")
    79  		c.Ui.Error(commandErrorText(c))
    80  		return 1
    81  	}
    82  
    83  	// Get the HTTP client
    84  	client, err := c.Meta.Client()
    85  	if err != nil {
    86  		c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
    87  		return 1
    88  	}
    89  
    90  	namespaces, _, err := client.Namespaces().List(nil)
    91  	if err != nil {
    92  		c.Ui.Error(fmt.Sprintf("Error retrieving namespaces: %s", err))
    93  		return 1
    94  	}
    95  
    96  	if json || len(tmpl) > 0 {
    97  		out, err := Format(json, tmpl, namespaces)
    98  		if err != nil {
    99  			c.Ui.Error(err.Error())
   100  			return 1
   101  		}
   102  
   103  		c.Ui.Output(out)
   104  		return 0
   105  	}
   106  
   107  	c.Ui.Output(formatNamespaces(namespaces))
   108  	return 0
   109  }
   110  
   111  func formatNamespaces(namespaces []*api.Namespace) string {
   112  	if len(namespaces) == 0 {
   113  		return "No namespaces found"
   114  	}
   115  
   116  	// Sort the output by namespace name
   117  	sort.Slice(namespaces, func(i, j int) bool { return namespaces[i].Name < namespaces[j].Name })
   118  
   119  	rows := make([]string, len(namespaces)+1)
   120  	rows[0] = "Name|Description"
   121  	for i, ns := range namespaces {
   122  		rows[i+1] = fmt.Sprintf("%s|%s",
   123  			ns.Name,
   124  			ns.Description)
   125  	}
   126  	return formatList(rows)
   127  }