Skip to content

Commit

Permalink
"wsk property get" can now return raw output for specific properties (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
neerajmangal authored and rabbah committed Apr 17, 2019
1 parent 8861d4b commit 23de5a3
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 77 deletions.
1 change: 1 addition & 0 deletions commands/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ type FlagsStruct struct {
apihostSet string
apiversionSet string
namespaceSet string
output string
}

action ActionFlags
Expand Down
153 changes: 107 additions & 46 deletions commands/property.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"os"

"github.com/fatih/color"
"github.com/mitchellh/go-homedir"
homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"

"github.com/apache/incubator-openwhisk-cli/wski18n"
Expand Down Expand Up @@ -53,6 +53,18 @@ const DefaultAPIBuildNo string = ""
const DefaultNamespace string = "_"
const DefaultPropsFile string = "~/.wskprops"

const (
propDisplayCert = "client cert"
propDisplayKey = "Client key"
propDisplayAuth = "whisk auth"
propDisplayAPIHost = "whisk API host"
propDisplayAPIVersion = "whisk API version"
propDisplayNamespace = "whisk namespace"
propDisplayCLIVersion = "whisk CLI version"
propDisplayAPIBuild = "whisk API build"
propDisplayAPIBuildNo = "whisk API build number"
)

var propertyCmd = &cobra.Command{
Use: "property",
Short: wski18n.T("work with whisk properties"),
Expand Down Expand Up @@ -287,6 +299,22 @@ var propertyGetCmd = &cobra.Command{
PreRunE: SetupClientConfig,
RunE: func(cmd *cobra.Command, args []string) error {

var outputFormat string = "std"
if Flags.property.output != "std" {
switch Flags.property.output {
case "raw":
outputFormat = "raw"
break
//case "json": For future implementation
//case "yaml": For future implementation
default:
errStr := fmt.Sprintf(
wski18n.T("Supported output format are std|raw"))
werr := whisk.MakeWskErrorFromWskError(errors.New(errStr), nil, whisk.EXIT_CODE_ERR_GENERAL, whisk.DISPLAY_MSG, whisk.NO_DISPLAY_USAGE)
return werr
}
}

// If no property is explicitly specified, default to all properties
if !(Flags.property.all || Flags.property.cert ||
Flags.property.key || Flags.property.auth ||
Expand All @@ -295,33 +323,44 @@ var propertyGetCmd = &cobra.Command{
Flags.property.apihost || Flags.property.apibuildno) {
Flags.property.all = true
}
if Flags.property.all {
// Currently with all only standard output format is supported.
if outputFormat != "std" {
errStr := fmt.Sprintf(
wski18n.T("--output|-o raw only supported with specific property type"))
werr := whisk.MakeWskErrorFromWskError(errors.New(errStr), nil, whisk.EXIT_CODE_ERR_GENERAL, whisk.DISPLAY_MSG, whisk.NO_DISPLAY_USAGE)
return werr
}

if Flags.property.all || Flags.property.cert {
fmt.Fprintf(color.Output, "%s\t\t%s\n", wski18n.T("client cert"), boldString(Properties.Cert))
}

if Flags.property.all || Flags.property.key {
fmt.Fprintf(color.Output, "%s\t\t%s\n", wski18n.T("Client key"), boldString(Properties.Key))
}

if Flags.property.all || Flags.property.auth {
fmt.Fprintf(color.Output, "%s\t\t%s\n", wski18n.T("whisk auth"), boldString(Properties.Auth))
}

if Flags.property.all || Flags.property.apihost {
fmt.Fprintf(color.Output, "%s\t\t%s\n", wski18n.T("whisk API host"), boldString(Properties.APIHost))
}

if Flags.property.all || Flags.property.apiversion {
fmt.Fprintf(color.Output, "%s\t%s\n", wski18n.T("whisk API version"), boldString(Properties.APIVersion))
}

if Flags.property.all || Flags.property.namespace {
fmt.Fprintf(color.Output, "%s\t\t%s\n", wski18n.T("whisk namespace"), boldString(Properties.Namespace))
}

if Flags.property.all || Flags.property.cliversion {
fmt.Fprintf(color.Output, "%s\t%s\n", wski18n.T("whisk CLI version"), boldString(Properties.CLIVersion))
fmt.Fprintf(color.Output, "%s\t\t%s\n", wski18n.T(propDisplayCert), boldString(Properties.Cert))
fmt.Fprintf(color.Output, "%s\t\t%s\n", wski18n.T(propDisplayKey), boldString(Properties.Key))
fmt.Fprintf(color.Output, "%s\t\t%s\n", wski18n.T(propDisplayAuth), boldString(Properties.Auth))
fmt.Fprintf(color.Output, "%s\t\t%s\n", wski18n.T(propDisplayAPIHost), boldString(Properties.APIHost))
fmt.Fprintf(color.Output, "%s\t%s\n", wski18n.T(propDisplayAPIVersion), boldString(Properties.APIVersion))
fmt.Fprintf(color.Output, "%s\t\t%s\n", wski18n.T(propDisplayNamespace), boldString(Properties.Namespace))
fmt.Fprintf(color.Output, "%s\t%s\n", wski18n.T(propDisplayCLIVersion), boldString(Properties.CLIVersion))
} else {
if Flags.property.cert {
printProperty(Properties.Cert, propDisplayCert, outputFormat)
}
if Flags.property.key {
printProperty(Properties.Key, propDisplayKey, outputFormat)
}
if Flags.property.cliversion {
printProperty(Properties.CLIVersion, propDisplayCLIVersion, outputFormat, "%s\t%s\n")
}
if Flags.property.apihost {
printProperty(Properties.APIHost, propDisplayAPIHost, outputFormat)
}
if Flags.property.auth {
printProperty(Properties.Auth, propDisplayAuth, outputFormat)
}
if Flags.property.apiversion {
printProperty(Properties.APIVersion, propDisplayAPIVersion, outputFormat, "%s\t%s\n")
}
if Flags.property.namespace {
printProperty(Properties.Namespace, propDisplayNamespace, outputFormat)
}
}

if Flags.property.all || Flags.property.apibuild || Flags.property.apibuildno {
Expand All @@ -332,11 +371,15 @@ var propertyGetCmd = &cobra.Command{
info.Build = wski18n.T("Unknown")
info.BuildNo = wski18n.T("Unknown")
}
if Flags.property.all || Flags.property.apibuild {
fmt.Fprintf(color.Output, "%s\t\t%s\n", wski18n.T("whisk API build"), boldString(info.Build))
if Flags.property.all {
fmt.Fprintf(color.Output, "%s\t\t%s\n", wski18n.T(propDisplayAPIBuild), boldString(info.Build))
fmt.Fprintf(color.Output, "%s\t%s\n", wski18n.T(propDisplayAPIBuildNo), boldString(info.BuildNo))
}
if Flags.property.apibuild && !Flags.property.all {
printProperty(info.Build, propDisplayAPIBuild, outputFormat)
}
if Flags.property.all || Flags.property.apibuildno {
fmt.Fprintf(color.Output, "%s\t%s\n", wski18n.T("whisk API build number"), boldString(info.BuildNo))
if Flags.property.apibuildno && !Flags.property.all {
printProperty(info.BuildNo, propDisplayAPIBuildNo, outputFormat, "%s\t%s\n")
}
if err != nil {
errStr := fmt.Sprintf(
Expand All @@ -346,7 +389,6 @@ var propertyGetCmd = &cobra.Command{
return werr
}
}

return nil
},
}
Expand All @@ -359,30 +401,31 @@ func init() {
)

// need to set property flags as booleans instead of strings... perhaps with boolApihost...
propertyGetCmd.Flags().BoolVar(&Flags.property.cert, "cert", false, wski18n.T("client cert"))
propertyGetCmd.Flags().BoolVar(&Flags.property.key, "key", false, wski18n.T("client key"))
propertyGetCmd.Flags().BoolVar(&Flags.property.cert, "cert", false, wski18n.T(propDisplayCert))
propertyGetCmd.Flags().BoolVar(&Flags.property.key, "key", false, wski18n.T(propDisplayKey))
propertyGetCmd.Flags().BoolVar(&Flags.property.auth, "auth", false, wski18n.T("authorization key"))
propertyGetCmd.Flags().BoolVar(&Flags.property.apihost, "apihost", false, wski18n.T("whisk API host"))
propertyGetCmd.Flags().BoolVar(&Flags.property.apiversion, "apiversion", false, wski18n.T("whisk API version"))
propertyGetCmd.Flags().BoolVar(&Flags.property.apihost, "apihost", false, wski18n.T(propDisplayAPIHost))
propertyGetCmd.Flags().BoolVar(&Flags.property.apiversion, "apiversion", false, wski18n.T(propDisplayAPIVersion))
propertyGetCmd.Flags().BoolVar(&Flags.property.apibuild, "apibuild", false, wski18n.T("whisk API build version"))
propertyGetCmd.Flags().BoolVar(&Flags.property.apibuildno, "apibuildno", false, wski18n.T("whisk API build number"))
propertyGetCmd.Flags().BoolVar(&Flags.property.cliversion, "cliversion", false, wski18n.T("whisk CLI version"))
propertyGetCmd.Flags().BoolVar(&Flags.property.namespace, "namespace", false, wski18n.T("whisk namespace"))
propertyGetCmd.Flags().BoolVar(&Flags.property.apibuildno, "apibuildno", false, wski18n.T(propDisplayAPIBuildNo))
propertyGetCmd.Flags().BoolVar(&Flags.property.cliversion, "cliversion", false, wski18n.T(propDisplayCLIVersion))
propertyGetCmd.Flags().BoolVar(&Flags.property.namespace, "namespace", false, wski18n.T(propDisplayNamespace))
propertyGetCmd.Flags().BoolVar(&Flags.property.all, "all", false, wski18n.T("all properties"))
propertyGetCmd.Flags().StringVarP(&Flags.property.output, "output", "o", "std", wski18n.T("Output format in std|raw"))

propertySetCmd.Flags().StringVarP(&Flags.Global.Auth, "auth", "u", "", wski18n.T("authorization `KEY`"))
propertySetCmd.Flags().StringVar(&Flags.Global.Cert, "cert", "", wski18n.T("client cert"))
propertySetCmd.Flags().StringVar(&Flags.Global.Key, "key", "", wski18n.T("client key"))
propertySetCmd.Flags().StringVar(&Flags.Global.Cert, "cert", "", wski18n.T(propDisplayCert))
propertySetCmd.Flags().StringVar(&Flags.Global.Key, "key", "", wski18n.T(propDisplayKey))
propertySetCmd.Flags().StringVar(&Flags.property.apihostSet, "apihost", "", wski18n.T("whisk API `HOST`"))
propertySetCmd.Flags().StringVar(&Flags.property.apiversionSet, "apiversion", "", wski18n.T("whisk API `VERSION`"))
propertySetCmd.Flags().StringVar(&Flags.property.namespaceSet, "namespace", "", wski18n.T("whisk `NAMESPACE`"))

propertyUnsetCmd.Flags().BoolVar(&Flags.property.cert, "cert", false, wski18n.T("client cert"))
propertyUnsetCmd.Flags().BoolVar(&Flags.property.key, "key", false, wski18n.T("client key"))
propertyUnsetCmd.Flags().BoolVar(&Flags.property.cert, "cert", false, wski18n.T(propDisplayCert))
propertyUnsetCmd.Flags().BoolVar(&Flags.property.key, "key", false, wski18n.T(propDisplayKey))
propertyUnsetCmd.Flags().BoolVar(&Flags.property.auth, "auth", false, wski18n.T("authorization key"))
propertyUnsetCmd.Flags().BoolVar(&Flags.property.apihost, "apihost", false, wski18n.T("whisk API host"))
propertyUnsetCmd.Flags().BoolVar(&Flags.property.apiversion, "apiversion", false, wski18n.T("whisk API version"))
propertyUnsetCmd.Flags().BoolVar(&Flags.property.namespace, "namespace", false, wski18n.T("whisk namespace"))
propertyUnsetCmd.Flags().BoolVar(&Flags.property.apihost, "apihost", false, wski18n.T(propDisplayAPIHost))
propertyUnsetCmd.Flags().BoolVar(&Flags.property.apiversion, "apiversion", false, wski18n.T(propDisplayAPIVersion))
propertyUnsetCmd.Flags().BoolVar(&Flags.property.namespace, "namespace", false, wski18n.T(propDisplayNamespace))

}

Expand Down Expand Up @@ -558,3 +601,21 @@ func parseConfigFlags(cmd *cobra.Command, args []string) error {

return nil
}

func printProperty(propertyName string, displayText string, formatType string, format ...string) {
switch formatType {
case "std":
if len(format) > 0 {
fmt.Fprintf(color.Output, format[0], wski18n.T(displayText), boldString(propertyName))
break
}
fmt.Fprintf(color.Output, "%s\t\t%s\n", wski18n.T(displayText), boldString(propertyName))
break
case "raw":
fmt.Fprintf(color.Output, "%s\n", boldString(propertyName))
break
default:
// In case of any other type for now print in std format.
fmt.Fprintf(color.Output, "%s\t\t%s\n", wski18n.T(displayText), boldString(propertyName))
}
}
33 changes: 17 additions & 16 deletions tests/src/integration/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
package tests

import (
"github.com/apache/incubator-openwhisk-cli/tests/src/integration/common"
"github.com/stretchr/testify/assert"
"os"
"testing"

"github.com/apache/incubator-openwhisk-cli/tests/src/integration/common"
"github.com/stretchr/testify/assert"
)

var wsk *common.Wsk = common.NewWsk()
Expand Down Expand Up @@ -65,17 +66,17 @@ func TestShowCLIBuildVersion(t *testing.T) {
stdout, err := wsk.RunCommand("property", "get", "--cliversion")
assert.Equal(t, nil, err, "The command property get --cliversion failed to run.")
output := common.RemoveRedundentSpaces(string(stdout))
assert.NotContains(t, output, "whisk CLI version not set",
"The output of the command property get --cliversion contains \"whisk CLI version not set\".")
assert.Contains(t, output, "whisk CLI version",
"The output of the command property get --cliversion does not contain \"whisk CLI version\".")
assert.NotContains(t, output, common.PropDisplayCLIVersion+" not set",
"The output of the command property get --cliversion contains "+common.PropDisplayCLIVersion+" not set")
assert.Contains(t, output, common.PropDisplayCLIVersion,
"The output of the command property get --cliversion does not contain "+common.PropDisplayCLIVersion)
}

func TestShowAPIVersion(t *testing.T) {
stdout, err := wsk.RunCommand("property", "get", "--apiversion")
assert.Equal(t, nil, err, "The command property get --apiversion failed to run.")
assert.Contains(t, string(stdout), "whisk API version",
"The output of the command property get --apiversion does not contain \"whisk API version\".")
assert.Contains(t, string(stdout), common.PropDisplayAPIVersion,
"The output of the command property get --apiversion does not contain "+common.PropDisplayCLIVersion)
}

// Test case to verify the default namespace _.
Expand All @@ -88,8 +89,8 @@ func TestDefaultNamespace(t *testing.T) {

stdout, err := wsk.RunCommand("property", "get", "-i", "--namespace")
assert.Equal(t, nil, err, "The command property get -i --namespace failed to run.")
assert.Contains(t, common.RemoveRedundentSpaces(string(stdout)), "whisk namespace _",
"The output of the command does not contain \"whisk namespace _\".")
assert.Contains(t, common.RemoveRedundentSpaces(string(stdout)), common.PropDisplayNamespace+" _",
"The output of the command does not contain "+common.PropDisplayCLIVersion+" _")
common.DeleteFile(tmpProp)
}

Expand All @@ -114,18 +115,18 @@ func TestValidateDefaultProperties(t *testing.T) {

stdout, err = wsk.RunCommand("property", "get", "--auth")
assert.Equal(t, nil, err, "The command property get --auth failed to run.")
assert.Equal(t, "whisk auth", common.RemoveRedundentSpaces(string(stdout)),
"The output of the command does not equal to \"whisk auth\".")
assert.Equal(t, common.PropDisplayAuth, common.RemoveRedundentSpaces(string(stdout)),
"The output of the command does not equal to "+common.PropDisplayAuth)

stdout, err = wsk.RunCommand("property", "get", "--apihost")
assert.Equal(t, nil, err, "The command property get --apihost failed to run.")
assert.Equal(t, "whisk API host", common.RemoveRedundentSpaces(string(stdout)),
"The output of the command does not equal to \"whisk API host\".")
assert.Equal(t, common.PropDisplayAPIHost, common.RemoveRedundentSpaces(string(stdout)),
"The output of the command does not equal to "+common.PropDisplayAPIHost)

stdout, err = wsk.RunCommand("property", "get", "--namespace")
assert.Equal(t, nil, err, "The command property get --namespace failed to run.")
assert.Equal(t, "whisk namespace _", common.RemoveRedundentSpaces(string(stdout)),
"The output of the command does not equal to \"whisk namespace _\".")
assert.Equal(t, common.PropDisplayNamespace+" _", common.RemoveRedundentSpaces(string(stdout)),
"The output of the command does not equal to "+common.PropDisplayNamespace+" _")

common.DeleteFile(tmpProp)
}
Expand Down
12 changes: 12 additions & 0 deletions tests/src/integration/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ import (
"unicode"
)

const (
PropDisplayCert = "client cert"
PropDisplayKey = "Client key"
PropDisplayAuth = "whisk auth"
PropDisplayAPIHost = "whisk API host"
PropDisplayAPIVersion = "whisk API version"
PropDisplayNamespace = "whisk namespace"
PropDisplayCLIVersion = "whisk CLI version"
PropDisplayAPIBuild = "whisk API build"
PropDisplayAPIBuildNo = "whisk API build number"
)

func checkError(err error) {
if err != nil {
fmt.Println(err.Error())
Expand Down
25 changes: 13 additions & 12 deletions tests/src/integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ package tests

import (
"fmt"
"github.com/apache/incubator-openwhisk-cli/tests/src/integration/common"
"github.com/stretchr/testify/assert"
"os"
"strings"
"testing"

"github.com/apache/incubator-openwhisk-cli/tests/src/integration/common"
"github.com/stretchr/testify/assert"
)

var invalidArgs []common.InvalidArg
Expand Down Expand Up @@ -370,12 +371,12 @@ func TestShowAPIBuildVersion(t *testing.T) {
stdout, err = wsk.RunCommand("property", "get", "-i", "--apibuild")
assert.Equal(t, nil, err, "The command property get -i --apibuild failed to run.")
println(common.RemoveRedundentSpaces(string(stdout)))
assert.NotContains(t, common.RemoveRedundentSpaces(string(stdout)), "whisk API build Unknown",
"The output of the command property get --apibuild does not contain \"whisk API build Unknown\".")
assert.NotContains(t, common.RemoveRedundentSpaces(string(stdout)), common.PropDisplayAPIBuild+" Unknown",
"The output of the command property get --apibuild does not contain "+common.PropDisplayAPIBuild+" Unknown")
assert.NotContains(t, common.RemoveRedundentSpaces(string(stdout)), "Unable to obtain API build information",
"The output of the command property get --apibuild does not contain \"Unable to obtain API build information\".")
assert.Contains(t, common.RemoveRedundentSpaces(string(stdout)), "whisk API build 20",
"The output of the command property get --apibuild does not contain \"whisk API build 20\".")
assert.Contains(t, common.RemoveRedundentSpaces(string(stdout)), common.PropDisplayAPIBuild+" 20",
"The output of the command property get --apibuild does not contain"+common.PropDisplayAPIBuild+" 20")
common.DeleteFile(tmpProp)
}

Expand All @@ -390,8 +391,8 @@ func TestFailShowAPIBuildVersion(t *testing.T) {
assert.Equal(t, nil, err, "The command property set --apihost failed to run.")
stdout, err := wsk.RunCommand("property", "get", "-i", "--apibuild")
assert.NotEqual(t, nil, err, "The command property get -i --apibuild does not raise any error.")
assert.Contains(t, common.RemoveRedundentSpaces(string(stdout)), "whisk API build Unknown",
"The output of the command property get --apibuild does not contain \"whisk API build Unknown\".")
assert.Contains(t, common.RemoveRedundentSpaces(string(stdout)), common.PropDisplayAPIBuild+" Unknown",
"The output of the command property get --apibuild does not contain"+common.PropDisplayAPIBuild+" Unknown")
assert.Contains(t, common.RemoveRedundentSpaces(string(stdout)), "Unable to obtain API build information",
"The output of the command property get --apibuild does not contain \"Unable to obtain API build information\".")
}
Expand All @@ -409,12 +410,12 @@ func TestShowAPIBuildVersionHTTP(t *testing.T) {
stdout, err = wsk.RunCommand("property", "get", "-i", "--apibuild")
println(common.RemoveRedundentSpaces(string(stdout)))
//assert.Equal(t, nil, err, "The command property get -i --apibuild failed to run.")
assert.NotContains(t, common.RemoveRedundentSpaces(string(stdout)), "whisk API build Unknown",
"The output of the command property get --apibuild does not contain \"whisk API build Unknown\".")
assert.NotContains(t, common.RemoveRedundentSpaces(string(stdout)), common.PropDisplayAPIBuild+" Unknown",
"The output of the command property get --apibuild does not contain "+common.PropDisplayAPIBuild+" Unknown")
assert.NotContains(t, common.RemoveRedundentSpaces(string(stdout)), "Unable to obtain API build information",
"The output of the command property get --apibuild does not contain \"Unable to obtain API build information\".")
assert.Contains(t, common.RemoveRedundentSpaces(string(stdout)), "whisk API build 20",
"The output of the command property get --apibuild does not contain \"whisk API build 20\".")
assert.Contains(t, common.RemoveRedundentSpaces(string(stdout)), common.PropDisplayAPIBuild+" 20",
"The output of the command property get --apibuild does not contain "+common.PropDisplayAPIBuild+" 20")
common.DeleteFile(tmpProp)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class WskCliBasicUsageTests extends TestHelpers with WskTestHelpers {

it should "show cli build version" in {
val stdout = wsk.cli(Seq("property", "get", "--cliversion")).stdout
stdout should include regex ("""(?i)whisk CLI version\s+201.*""")
stdout should include regex ("""(?i)whisk CLI version\s+20.*""")
}

it should "show api version" in {
Expand Down
Loading

0 comments on commit 23de5a3

Please sign in to comment.