1package command
2
3import (
4	"strings"
5
6	"github.com/hashicorp/nomad/api/contexts"
7	"github.com/mitchellh/cli"
8	"github.com/posener/complete"
9)
10
11type NamespaceCommand struct {
12	Meta
13}
14
15func (f *NamespaceCommand) Help() string {
16	helpText := `
17Usage: nomad namespace <subcommand> [options] [args]
18
19  This command groups subcommands for interacting with namespaces. Namespaces
20  allow jobs and their associated objects to be segmented from each other and
21  other users of the cluster. For a full guide on namespaces see:
22  https://www.nomadproject.io/guides/namespaces.html
23
24  Create or update a namespace:
25
26      $ nomad namespace apply -description "My new namespace" <name>
27
28  List namespaces:
29
30      $ nomad namespace list
31
32  View the status of a namespace:
33
34      $ nomad namespace status <name>
35
36  Please see the individual subcommand help for detailed usage information.
37`
38
39	return strings.TrimSpace(helpText)
40}
41
42func (f *NamespaceCommand) Synopsis() string {
43	return "Interact with namespaces"
44}
45
46func (f *NamespaceCommand) Name() string { return "namespace" }
47
48func (f *NamespaceCommand) Run(args []string) int {
49	return cli.RunResultHelp
50}
51
52// NamespacePredictor returns a namespace predictor that can optionally filter
53// specific namespaces
54func NamespacePredictor(factory ApiClientFactory, filter map[string]struct{}) complete.Predictor {
55	return complete.PredictFunc(func(a complete.Args) []string {
56		client, err := factory()
57		if err != nil {
58			return nil
59		}
60
61		resp, _, err := client.Search().PrefixSearch(a.Last, contexts.Namespaces, nil)
62		if err != nil {
63			return []string{}
64		}
65
66		// Filter the returned namespaces. We assign the unfiltered slice to the
67		// filtered slice but with no elements. This causes the slices to share
68		// the underlying array and makes the filtering allocation free.
69		unfiltered := resp.Matches[contexts.Namespaces]
70		filtered := unfiltered[:0]
71		for _, ns := range unfiltered {
72			if _, ok := filter[ns]; !ok {
73				filtered = append(filtered, ns)
74			}
75		}
76
77		return filtered
78	})
79}
80