openwhisk-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From houshen...@apache.org
Subject [incubator-openwhisk-cli] 03/09: (Review) Added --last Activation Flag (#2334)
Date Mon, 17 Jul 2017 14:10:26 GMT
This is an automated email from the ASF dual-hosted git repository.

houshengbo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-openwhisk-cli.git

commit 435bb48d5af97ed83d13897f5bdec0fe096b6a1c
Author: Benjamin Poole <Benjamin.D.Poole@ibm.com>
AuthorDate: Thu Jul 13 10:00:39 2017 -0400

    (Review) Added --last Activation Flag  (#2334)
    
    * Added lastFlag to activation logs, result and get along with docs/testing
---
 commands/activation.go           | 68 ++++++++++++++++++++++++++++++++++++++--
 commands/flags.go                |  1 +
 wski18n/resources/en_US.all.json | 20 ++++++++++++
 3 files changed, 86 insertions(+), 3 deletions(-)

diff --git a/commands/activation.go b/commands/activation.go
index d7f59b2..c9317cd 100644
--- a/commands/activation.go
+++ b/commands/activation.go
@@ -101,14 +101,22 @@ var activationListCmd = &cobra.Command{
 }
 
 var activationGetCmd = &cobra.Command{
-    Use:   "get ACTIVATION_ID [FIELD_FILTER]",
+    Use:   "get (ACTIVATION_ID | --last) [FIELD_FILTER]",
     Short: wski18n.T("get activation"),
     SilenceUsage:   true,
     SilenceErrors:  true,
     PreRunE: setupClientConfig,
     RunE: func(cmd *cobra.Command, args []string) error {
         var field string
+        var err error
 
+        if args, err = lastFlag(args); err != nil {  // Checks if any errors occured in lastFlag(args)
+          whisk.Debug(whisk.DbgError, "lastFlag(%#v) failed: %s\n", args, err)
+          errStr := wski18n.T("Unable to get activation: {{.err}}",
+            map[string]interface{}{"err": err})
+          werr := whisk.MakeWskErrorFromWskError(errors.New(errStr), err, whisk.EXITCODE_ERR_GENERAL,
whisk.DISPLAY_MSG, whisk.NO_DISPLAY_USAGE)
+          return werr
+        }
         if whiskErr := checkArgs(args, 1, 2, "Activation get",
                 wski18n.T("An activation ID is required.")); whiskErr != nil {
             return whiskErr
@@ -164,13 +172,21 @@ var activationGetCmd = &cobra.Command{
 }
 
 var activationLogsCmd = &cobra.Command{
-    Use:   "logs ACTIVATION_ID",
+    Use:   "logs (ACTIVATION_ID | --last)",
     Short: wski18n.T("get the logs of an activation"),
     SilenceUsage:   true,
     SilenceErrors:  true,
     PreRunE: setupClientConfig,
     RunE: func(cmd *cobra.Command, args []string) error {
+        var err error
 
+        if args, err = lastFlag(args); err != nil {  // Checks if any errors occured in lastFlag(args)
+          whisk.Debug(whisk.DbgError, "lastFlag(%#v) failed: %s\n", args, err)
+          errStr := wski18n.T("Unable to get logs for activation: {{.err}}",
+            map[string]interface{}{"err": err})
+          werr := whisk.MakeWskErrorFromWskError(errors.New(errStr), err, whisk.EXITCODE_ERR_GENERAL,
whisk.DISPLAY_MSG, whisk.NO_DISPLAY_USAGE)
+          return werr
+        }
         if whiskErr := checkArgs(args, 1, 1, "Activation logs",
                 wski18n.T("An activation ID is required.")); whiskErr != nil {
             return whiskErr
@@ -192,13 +208,21 @@ var activationLogsCmd = &cobra.Command{
 }
 
 var activationResultCmd = &cobra.Command{
-    Use:   "result ACTIVATION_ID",
+    Use:   "result (ACTIVATION_ID | --last)",
     Short: "get the result of an activation",
     SilenceUsage:   true,
     SilenceErrors:  true,
     PreRunE: setupClientConfig,
     RunE: func(cmd *cobra.Command, args []string) error {
+        var err error
 
+        if args, err = lastFlag(args); err != nil {  // Checks if any errors occured in lastFlag(args)
+          whisk.Debug(whisk.DbgError, "lastFlag(%#v) failed: %s\n", args, err)
+          errStr := wski18n.T("Unable to get result for activation: {{.err}}",
+            map[string]interface{}{"err": err})
+          werr := whisk.MakeWskErrorFromWskError(errors.New(errStr), err, whisk.EXITCODE_ERR_GENERAL,
whisk.DISPLAY_MSG, whisk.NO_DISPLAY_USAGE)
+          return werr
+        }
         if whiskErr := checkArgs(args, 1, 1, "Activation result",
                 wski18n.T("An activation ID is required.")); whiskErr != nil {
             return whiskErr
@@ -219,6 +243,39 @@ var activationResultCmd = &cobra.Command{
     },
 }
 
+// lastFlag(args) retrieves the last activation with flag -l or --last
+// Param: Brings in []strings from args
+// Return: Returns a []string with the latest ID or the original args and any errors
+func lastFlag(args []string) ([]string, error) {
+    if Flags.activation.last {
+        options := &whisk.ActivationListOptions {
+            Limit: 1,
+            Skip: 0,
+        }
+        activations,_, err := client.Activations.List(options)
+        if err != nil {    // Checks Activations.List for errors when retrieving latest activaiton
+            whisk.Debug(whisk.DbgError, "client.Activations.List(%#v) error during lastFlag:
%s\n", options, err)
+            return args, err
+        }
+        if len(activations) == 0 {    // Checks to to see if there are activations available
+            whisk.Debug(whisk.DbgError, "No activations found in activation list\n")
+            errStr := wski18n.T("Activation list does not contain any activations.")
+            whiskErr := whisk.MakeWskError(errors.New(errStr), whisk.EXITCODE_ERR_GENERAL,
whisk.DISPLAY_MSG, whisk.DISPLAY_USAGE)
+            return args, whiskErr
+        }
+        if len(args) == 0 {
+            whisk.Debug(whisk.DbgInfo, "Appending most recent activation ID(%s) into args\n",
activations[0].ActivationID)
+            args = append(args, activations[0].ActivationID)
+        } else {
+                whisk.Debug(whisk.DbgInfo, "Appending most recent activation ID(%s) into
args\n", activations[0].ActivationID)
+                args = append(args, activations[0].ActivationID)
+                whisk.Debug(whisk.DbgInfo, "Allocating appended ID to correct position in
args\n")
+                args[0], args[len(args) - 1] = args[len(args) - 1], args[0]    // IDs should
be located at args[0], if 1 or more arguments are given ID has to be moved to args[0]
+        }
+    }
+    return args, nil
+}
+
 var activationPollCmd = &cobra.Command{
     Use:   "poll [ NAMESPACE | ACTION_NAME ]",
     Short: wski18n.T("poll continuously for log messages from currently running actions"),
@@ -341,6 +398,11 @@ func init() {
     activationListCmd.Flags().Int64Var(&Flags.activation.since, "since", 0, wski18n.T("return
activations with timestamps later than `SINCE`; measured in milliseconds since Th, 01, Jan
1970"))
 
     activationGetCmd.Flags().BoolVarP(&Flags.common.summary, "summary", "s", false, wski18n.T("summarize
activation details"))
+    activationGetCmd.Flags().BoolVarP(&Flags.activation.last, "last", "l", false, wski18n.T("retrieves
the last activation"))
+
+    activationLogsCmd.Flags().BoolVarP(&Flags.activation.last, "last", "l", false, wski18n.T("retrieves
the last activation"))
+
+    activationResultCmd.Flags().BoolVarP(&Flags.activation.last, "last", "l", false,
wski18n.T("retrieves the last activation"))
 
     activationPollCmd.Flags().IntVarP(&Flags.activation.exit, "exit", "e", 0, wski18n.T("stop
polling after `SECONDS` seconds"))
     activationPollCmd.Flags().IntVar(&Flags.activation.sinceSeconds, "since-seconds",
0, wski18n.T("start polling for activations `SECONDS` seconds ago"))
diff --git a/commands/flags.go b/commands/flags.go
index 4dbf12a..7357426 100644
--- a/commands/flags.go
+++ b/commands/flags.go
@@ -88,6 +88,7 @@ type FlagsStruct struct {
         sinceHours      int
         sinceDays       int
         exit            int
+        last            bool
     }
 
     // rule
diff --git a/wski18n/resources/en_US.all.json b/wski18n/resources/en_US.all.json
index bcc776d..535f958 100644
--- a/wski18n/resources/en_US.all.json
+++ b/wski18n/resources/en_US.all.json
@@ -948,6 +948,10 @@
     "translation": "get activation"
   },
   {
+    "id": "Unable to get activation: {{.err}}",
+    "translation": "Unable to get activation: {{.err}}"
+  },
+  {
     "id": "Unable to get activation '{{.id}}': {{.err}}",
     "translation": "Unable to get activation '{{.id}}': {{.err}}"
   },
@@ -964,10 +968,18 @@
     "translation": "get the logs of an activation"
   },
   {
+    "id": "Unable to get logs for activation: {{.err}}",
+    "translation": "Unable to get logs for activation: {{.err}}"
+  },
+  {
     "id": "Unable to get logs for activation '{{.id}}': {{.err}}",
     "translation": "Unable to get logs for activation '{{.id}}': {{.err}}"
   },
   {
+    "id": "Unable to get result for activation: {{.err}}",
+    "translation": "Unable to get result for activation: {{.err}}"
+  },
+  {
     "id": "Unable to get result for activation '{{.id}}': {{.err}}",
     "translation": "Unable to get result for activation '{{.id}}': {{.err}}"
   },
@@ -992,6 +1004,10 @@
     "translation": "\nActivation: {{.name}} ({{.id}})\n"
   },
   {
+    "id": "Activation list does not contain any activations.",
+    "translation": "Activation list does not contain any activations."
+  },
+  {
     "id": "exclude the first `SKIP` number of activations from the result",
     "translation": "exclude the first `SKIP` number of activations from the result"
   },
@@ -1016,6 +1032,10 @@
     "translation": "summarize activation details"
   },
   {
+    "id": "retrieves the last activation",
+    "translation": "retrieves the last activation"
+  },
+  {
     "id": "stop polling after `SECONDS` seconds",
     "translation": "stop polling after `SECONDS` seconds"
   },

-- 
To stop receiving notification emails like this one, please contact
"commits@openwhisk.apache.org" <commits@openwhisk.apache.org>.

Mime
View raw message