Skip to content

Commit

Permalink
Add flag to sort listed entities by date.
Browse files Browse the repository at this point in the history
By default will sort by entity name alphabetically.

Address issue #1569 for enhancement request.
  • Loading branch information
David Liu committed Feb 9, 2017
1 parent cf53ab1 commit b949d04
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 0 deletions.
39 changes: 39 additions & 0 deletions tests/src/whisk/core/cli/test/WskBasicUsageTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1242,4 +1242,43 @@ class WskBasicUsageTests
}
tmpProps.delete()
}

behavior of "Wsk cli list"

it should "wsk cli list entities by name alphabetically by default" in withAssetCleaner(wskprops) {
(wp, assetHelper) =>

val tmpwskprops = File.createTempFile("wskprops", ".tmp")
try {
val namespace = wsk.namespace.list().stdout.trim.split("\n").last
val env = Map("WSK_CONFIG_FILE" -> tmpwskprops.getAbsolutePath())
wsk.cli(Seq("property", "set", "-i", "--apihost", wskprops.apihost, "--auth", wskprops.authKey,
"--namespace", namespace), env = env)
val name1 = "helloTest"
val file1 = Some(TestUtils.getTestActionFilename("hello.js"))

assetHelper.withCleaner(wsk.action, name1) {
(action, _) =>
action.create(name1, file1, annotations = getValidJSONTestArgInput,
parameters = getValidJSONTestArgInput)
}

val name2 = "echoTest"
val file2 = Some(TestUtils.getTestActionFilename("echo.js"))

assetHelper.withCleaner(wsk.action, name2) {
(action, _) =>
action.create(name2, file2, annotations = getValidJSONTestArgInput,
parameters = getValidJSONTestArgInput)
}
val stdout = wsk.cli(Seq("list", "-i", "--apihost", wskprops.apihost, "--auth", wskprops.authKey), env = env).stdout

stdout should include regex ("echoTest")
stdout should include regex ("helloTest")
} finally {
tmpwskprops.delete()
}


}
}
5 changes: 5 additions & 0 deletions tools/cli/go-whisk-cli/commands/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ var flags struct {
summary bool
}

// namespace
namespace struct {
date bool // sort by date flag
}

// api
api struct {
action string
Expand Down
2 changes: 2 additions & 0 deletions tools/cli/go-whisk-cli/commands/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,10 @@ var listCmd = &cobra.Command{
}

func init() {
listCmd.Flags().BoolVarP(&flags.namespace.date, "sort", "t", false, wski18n.T("sort by date"))
namespaceCmd.AddCommand(
namespaceListCmd,
namespaceGetCmd,
)

}
16 changes: 16 additions & 0 deletions tools/cli/go-whisk-cli/commands/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,10 @@ func printSummary(collection interface{}) {

func printActionList(actions []whisk.Action) {
fmt.Fprintf(color.Output, "%s\n", boldString("actions"))
if (!flags.namespace.date) {
sort.Sort(whisk.ActionArray(actions))
}

for _, action := range actions {
publishState := wski18n.T("private")
kind := getValueString(action.Annotations, "exec")
Expand All @@ -295,6 +299,10 @@ func printActionList(actions []whisk.Action) {

func printTriggerList(triggers []whisk.Trigger) {
fmt.Fprintf(color.Output, "%s\n", boldString("triggers"))
if (!flags.namespace.date) {
sort.Sort(whisk.TriggerArray(triggers))
}

for _, trigger := range triggers {
publishState := wski18n.T("private")
fmt.Printf("%-70s %s\n", fmt.Sprintf("/%s/%s", trigger.Namespace, trigger.Name), publishState)
Expand All @@ -303,6 +311,10 @@ func printTriggerList(triggers []whisk.Trigger) {

func printPackageList(packages []whisk.Package) {
fmt.Fprintf(color.Output, "%s\n", boldString("packages"))
if (!flags.namespace.date) {
sort.Sort(whisk.PackageArray(packages))
}

for _, xPackage := range packages {
publishState := wski18n.T("private")
if xPackage.Publish != nil && *xPackage.Publish {
Expand All @@ -314,6 +326,10 @@ func printPackageList(packages []whisk.Package) {

func printRuleList(rules []whisk.Rule) {
fmt.Fprintf(color.Output, "%s\n", boldString("rules"))
if (!flags.namespace.date) {
sort.Sort((whisk.RuleArray(rules)))
}

for _, rule := range rules {
publishState := wski18n.T("private")
fmt.Printf("%-70s %s\n", fmt.Sprintf("/%s/%s", rule.Namespace, rule.Name), publishState)
Expand Down
14 changes: 14 additions & 0 deletions tools/cli/go-whisk/whisk/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,20 @@ type ActionListOptions struct {
Docs bool `url:"docs,omitempty"`
}

type ActionArray []Action

func (aa ActionArray) Len() int {
return len(aa)
}

func (aa ActionArray) Less(i int, j int) bool {
return aa[i].Name < aa[j].Name
}

func (aa ActionArray) Swap(i int, j int) {
aa[i], aa[j] = aa[j], aa[i]
}

////////////////////
// Action Methods //
////////////////////
Expand Down
14 changes: 14 additions & 0 deletions tools/cli/go-whisk/whisk/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ func (p *Package) GetName() string {
return p.Name
}

type PackageArray []Package

func (pa PackageArray) Len() int {
return len(pa)
}

func (pa PackageArray) Less(i int, j int) bool {
return pa[i].Name < pa[j].Name
}

func (pa PackageArray) Swap(i int, j int) {
pa[i], pa[j] = pa[j], pa[i]
}

// Use this struct when creating a binding
// Publish is NOT optional; Binding is a namespace/name object, not a bool
type BindingPackage struct {
Expand Down
14 changes: 14 additions & 0 deletions tools/cli/go-whisk/whisk/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ type RuleListOptions struct {
Docs bool `url:"docs,omitempty"`
}

type RuleArray []Rule

func (ra RuleArray) Len() int {
return len(ra)
}

func (ra RuleArray) Less(i int, j int) bool {
return ra[i].Name < ra[j].Name
}

func (ta RuleArray) Swap(i int, j int) {
ta[i], ta[j] = ta[j], ta[i]
}

func (s *RuleService) List(options *RuleListOptions) ([]Rule, *http.Response, error) {
route := "rules"
routeUrl, err := addRouteOptions(route, options)
Expand Down
14 changes: 14 additions & 0 deletions tools/cli/go-whisk/whisk/trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ type TriggerListOptions struct {
Docs bool `url:"docs,omitempty"`
}

type TriggerArray []Trigger

func (ta TriggerArray) Len() int {
return len(ta)
}

func (ta TriggerArray) Less(i int, j int) bool {
return ta[i].Name < ta[j].Name
}

func (ta TriggerArray) Swap(i int, j int) {
ta[i], ta[j] = ta[j], ta[i]
}

func (s *TriggerService) List(options *TriggerListOptions) ([]Trigger, *http.Response, error) {
route := "triggers"
routeUrl, err := addRouteOptions(route, options)
Expand Down

0 comments on commit b949d04

Please sign in to comment.