forked from apache/openwhisk-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnamespace.go
131 lines (113 loc) · 4.44 KB
/
namespace.go
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package commands
import (
"errors"
"fmt"
"github.com/fatih/color"
"github.com/spf13/cobra"
"github.com/apache/incubator-openwhisk-cli/wski18n"
"github.com/apache/incubator-openwhisk-client-go/whisk"
)
// namespaceCmd represents the namespace command
var namespaceCmd = &cobra.Command{
Use: "namespace",
Short: wski18n.T("work with namespaces"),
}
var namespaceListCmd = &cobra.Command{
Use: "list",
Short: wski18n.T("list available namespaces"),
SilenceUsage: true,
SilenceErrors: true,
PreRunE: SetupClientConfig,
RunE: func(cmd *cobra.Command, args []string) error {
// add "TYPE" --> public / private
if whiskErr := CheckArgs(args, 0, 0, "Namespace list", wski18n.T("No arguments are required.")); whiskErr != nil {
return whiskErr
}
namespaces, _, err := Client.Namespaces.List()
if err != nil {
whisk.Debug(whisk.DbgError, "Client.Namespaces.List() error: %s\n", err)
errStr := wski18n.T("Unable to obtain the list of available namespaces: {{.err}}",
map[string]interface{}{"err": err})
werr := whisk.MakeWskErrorFromWskError(errors.New(errStr), err, whisk.EXIT_CODE_ERR_NETWORK, whisk.DISPLAY_MSG, whisk.NO_DISPLAY_USAGE)
return werr
}
printList(namespaces, false) // `-n` flag applies to `namespace get`, not list, so must pass value false for printList here
return nil
},
}
var namespaceGetCmd = &cobra.Command{
Use: "get",
Short: wski18n.T("get triggers, actions, and rules in the registry for namespace"),
SilenceUsage: true,
SilenceErrors: true,
PreRunE: SetupClientConfig,
RunE: func(cmd *cobra.Command, args []string) error {
var err error
var namespace string = getClientNamespace()
if !(len(args) == 1 && args[0] == "/_") {
if whiskErr := CheckArgs(args, 0, 0, "Namespace get",
wski18n.T("No arguments are required.")); whiskErr != nil {
return whiskErr
}
}
actions, _, err := Client.Actions.List("", &whisk.ActionListOptions{Skip: 0, Limit: 0})
if err != nil {
return entityListError(err, namespace, "Actions")
}
packages, _, err := Client.Packages.List(&whisk.PackageListOptions{Skip: 0, Limit: 0})
if err != nil {
return entityListError(err, namespace, "Packages")
}
triggers, _, err := Client.Triggers.List(&whisk.TriggerListOptions{Skip: 0, Limit: 0})
if err != nil {
return entityListError(err, namespace, "Triggers")
}
rules, _, err := Client.Rules.List(&whisk.RuleListOptions{Skip: 0, Limit: 0})
if err != nil {
return entityListError(err, namespace, "Rules")
}
//No errors, lets attempt to retrieve the status of each rule
for index, rule := range rules {
ruleStatus, _, err := Client.Rules.Get(rule.Name)
if err != nil {
errStr := wski18n.T("Unable to get status of rule '{{.name}}': {{.err}}",
map[string]interface{}{"name": rule.Name, "err": err})
fmt.Println(errStr)
werr := whisk.MakeWskErrorFromWskError(errors.New(errStr), err, whisk.EXIT_CODE_ERR_GENERAL, whisk.DISPLAY_MSG, whisk.NO_DISPLAY_USAGE)
return werr
}
rules[index].Status = ruleStatus.Status
}
fmt.Fprintf(color.Output, wski18n.T("Entities in namespace: {{.namespace}}\n",
map[string]interface{}{"namespace": boldString(getClientNamespace())}))
sortByName := Flags.common.nameSort
printList(packages, sortByName)
printList(actions, sortByName)
printList(triggers, sortByName)
printList(rules, sortByName)
return nil
},
}
func init() {
namespaceGetCmd.Flags().BoolVarP(&Flags.common.nameSort, "name-sort", "n", false, wski18n.T("sorts a list alphabetically by entity name; only applicable within the limit/skip returned entity block"))
namespaceCmd.AddCommand(
namespaceListCmd,
namespaceGetCmd,
)
}