github.com/quite/nomad@v0.8.6/command/namespace_list.go (about)

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