-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathcommon.go
More file actions
70 lines (57 loc) · 2.33 KB
/
common.go
File metadata and controls
70 lines (57 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package stack
import (
"context"
"fmt"
"strings"
"unicode"
"github.com/docker/cli/cli/compose/convert"
"github.com/docker/cli/opts"
"github.com/moby/moby/client"
)
// validateStackName checks if the provided string is a valid stack name (namespace).
// It currently only does a rudimentary check if the string is empty, or consists
// of only whitespace and quoting characters.
func validateStackName(namespace string) error {
v := strings.TrimFunc(namespace, quotesOrWhitespace)
if v == "" {
return fmt.Errorf("invalid stack name: %q", namespace)
}
return nil
}
func validateStackNames(namespaces []string) error {
for _, ns := range namespaces {
if err := validateStackName(ns); err != nil {
return err
}
}
return nil
}
func quotesOrWhitespace(r rune) bool {
return unicode.IsSpace(r) || r == '"' || r == '\''
}
func getStackFilter(namespace string) client.Filters {
return make(client.Filters).Add("label", convert.LabelNamespace+"="+namespace)
}
func getStackFilterFromOpt(namespace string, opt opts.FilterOpt) client.Filters {
filter := opt.Value()
filter.Add("label", convert.LabelNamespace+"="+namespace)
return filter
}
func getAllStacksFilter() client.Filters {
return make(client.Filters).Add("label", convert.LabelNamespace)
}
func getStackServices(ctx context.Context, apiclient client.APIClient, namespace string) (client.ServiceListResult, error) {
return apiclient.ServiceList(ctx, client.ServiceListOptions{Filters: getStackFilter(namespace)})
}
func getStackNetworks(ctx context.Context, apiclient client.APIClient, namespace string) (client.NetworkListResult, error) {
return apiclient.NetworkList(ctx, client.NetworkListOptions{Filters: getStackFilter(namespace)})
}
func getStackSecrets(ctx context.Context, apiclient client.APIClient, namespace string) (client.SecretListResult, error) {
return apiclient.SecretList(ctx, client.SecretListOptions{Filters: getStackFilter(namespace)})
}
func getStackConfigs(ctx context.Context, apiclient client.APIClient, namespace string) (client.ConfigListResult, error) {
return apiclient.ConfigList(ctx, client.ConfigListOptions{Filters: getStackFilter(namespace)})
}
func getStackTasks(ctx context.Context, apiclient client.APIClient, namespace string) (client.TaskListResult, error) {
return apiclient.TaskList(ctx, client.TaskListOptions{Filters: getStackFilter(namespace)})
}