github.com/blixtra/nomad@v0.7.2-0.20171221000451-da9a1d7bb050/command/namespace.go (about)

     1  package command
     2  
     3  import (
     4  	"github.com/hashicorp/nomad/api/contexts"
     5  	"github.com/mitchellh/cli"
     6  	"github.com/posener/complete"
     7  )
     8  
     9  type NamespaceCommand struct {
    10  	Meta
    11  }
    12  
    13  func (f *NamespaceCommand) Help() string {
    14  	return "This command is accessed by using one of the subcommands below."
    15  }
    16  
    17  func (f *NamespaceCommand) Synopsis() string {
    18  	return "Interact with namespaces"
    19  }
    20  
    21  func (f *NamespaceCommand) Run(args []string) int {
    22  	return cli.RunResultHelp
    23  }
    24  
    25  // NamespacePredictor returns a namespace predictor that can optionally filter
    26  // specific namespaces
    27  func NamespacePredictor(factory ApiClientFactory, filter map[string]struct{}) complete.Predictor {
    28  	return complete.PredictFunc(func(a complete.Args) []string {
    29  		client, err := factory()
    30  		if err != nil {
    31  			return nil
    32  		}
    33  
    34  		resp, _, err := client.Search().PrefixSearch(a.Last, contexts.Namespaces, nil)
    35  		if err != nil {
    36  			return []string{}
    37  		}
    38  
    39  		// Filter the returned namespaces. We assign the unfiltered slice to the
    40  		// filtered slice but with no elements. This causes the slices to share
    41  		// the underlying array and makes the filtering allocation free.
    42  		unfiltered := resp.Matches[contexts.Namespaces]
    43  		filtered := unfiltered[:0]
    44  		for _, ns := range unfiltered {
    45  			if _, ok := filter[ns]; !ok {
    46  				filtered = append(filtered, ns)
    47  			}
    48  		}
    49  
    50  		return filtered
    51  	})
    52  }