github.com/djenriquez/nomad-1@v0.8.1/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) Run(args []string) int {
    54  	var json bool
    55  	var tmpl string
    56  
    57  	flags := c.Meta.FlagSet("namespace list", FlagSetClient)
    58  	flags.Usage = func() { c.Ui.Output(c.Help()) }
    59  	flags.BoolVar(&json, "json", false, "")
    60  	flags.StringVar(&tmpl, "t", "", "")
    61  
    62  	if err := flags.Parse(args); err != nil {
    63  		return 1
    64  	}
    65  
    66  	// Check that we got no arguments
    67  	args = flags.Args()
    68  	if l := len(args); l != 0 {
    69  		c.Ui.Error(c.Help())
    70  		return 1
    71  	}
    72  
    73  	// Get the HTTP client
    74  	client, err := c.Meta.Client()
    75  	if err != nil {
    76  		c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
    77  		return 1
    78  	}
    79  
    80  	namespaces, _, err := client.Namespaces().List(nil)
    81  	if err != nil {
    82  		c.Ui.Error(fmt.Sprintf("Error retrieving namespaces: %s", err))
    83  		return 1
    84  	}
    85  
    86  	if json || len(tmpl) > 0 {
    87  		out, err := Format(json, tmpl, namespaces)
    88  		if err != nil {
    89  			c.Ui.Error(err.Error())
    90  			return 1
    91  		}
    92  
    93  		c.Ui.Output(out)
    94  		return 0
    95  	}
    96  
    97  	c.Ui.Output(formatNamespaces(namespaces))
    98  	return 0
    99  }
   100  
   101  func formatNamespaces(namespaces []*api.Namespace) string {
   102  	if len(namespaces) == 0 {
   103  		return "No namespaces found"
   104  	}
   105  
   106  	// Sort the output by namespace name
   107  	sort.Slice(namespaces, func(i, j int) bool { return namespaces[i].Name < namespaces[j].Name })
   108  
   109  	rows := make([]string, len(namespaces)+1)
   110  	rows[0] = "Name|Description"
   111  	for i, ns := range namespaces {
   112  		rows[i+1] = fmt.Sprintf("%s|%s",
   113  			ns.Name,
   114  			ns.Description)
   115  	}
   116  	return formatList(rows)
   117  }