Skip to content

Commit

Permalink
add json output option
Browse files Browse the repository at this point in the history
move info print statements to stderr
  • Loading branch information
laidbackware committed Jan 26, 2023
1 parent e7ab767 commit c96f58e
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 26 deletions.
17 changes: 12 additions & 5 deletions api/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package api

import (
"fmt"
"os"

"github.com/vmware-labs/vmware-customer-connect-sdk/sdk"
)
Expand All @@ -14,7 +15,7 @@ type Availability struct {
EligibleToDownload bool
}

func ListFiles(slug, subProduct, version, username, password string) (data [][]string, availability Availability, apiVersions sdk.APIVersions, err error) {
func ListFiles(slug, subProduct, version, username, password string) (dlgDetails sdk.DlgDetails, apiVersions sdk.APIVersions, err error) {
if err = EnsureLogin(username, password); err != nil {
return
}
Expand All @@ -25,23 +26,29 @@ func ListFiles(slug, subProduct, version, username, password string) (data [][]s
return
}

fmt.Println("Getting DLG Details")
var dlgDetails sdk.DlgDetails
fmt.Fprintf(os.Stderr, "Getting DLG Details\n")
dlgDetails, err = authenticatedClient.GetDlgDetails(apiVersions.Code, productID)
if err != nil {
return
}

return
}

func ListFilesArray(slug, subProduct, version, username, password string) (data [][]string, availability Availability, apiVersions sdk.APIVersions, err error) {
dlgDetails, apiVersions, err := ListFiles(slug, subProduct, version, username, password)
if err != nil {
return
}
for _, v := range dlgDetails.DownloadDetails {
if v.FileName != "" {
line := []string{v.FileName, v.FileSize, v.Build, v.Title}
data = append(data, line)
}
}

availability = Availability{
EulaAccepted: dlgDetails.EulaResponse.EulaAccepted,
EligibleToDownload: dlgDetails.EligibilityResponse.EligibleToDownload,
}
return
}
}
10 changes: 5 additions & 5 deletions api/files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,33 @@ var testing_user = mustEnv("VCC_USER")
var testing_pass = mustEnv("VCC_PASS")

func TestGetFiles(t *testing.T) {
files, availability, apiVersions, err := ListFiles("vmware_tools", "vmtools", "11.1.1", testing_user, testing_pass)
files, availability, apiVersions, err := ListFilesArray("vmware_tools", "vmtools", "11.1.1", testing_user, testing_pass)
assert.Nil(t, err)
assert.Greater(t, len(files), 5, "Expected response to contain at least 5 items")
assert.Equal(t, apiVersions.MinorVersion, "11.1.1")
assert.True(t, availability.EligibleToDownload)
}

func TestGetFilesInvalidSlug(t *testing.T) {
files, _, _, err := ListFiles("tools", "vmtools", "", testing_user, testing_pass)
files, _, _, err := ListFilesArray("tools", "vmtools", "", testing_user, testing_pass)
assert.ErrorIs(t, err, sdk.ErrorInvalidSlug)
assert.Empty(t, files, "Expected response to be empty")
}

func TestGetFilesInvalidSubProduct(t *testing.T) {
files, _, _, err := ListFiles("vmware_tools", "tools", "", testing_user, testing_pass)
files, _, _, err := ListFilesArray("vmware_tools", "tools", "", testing_user, testing_pass)
assert.ErrorIs(t, err, sdk.ErrorInvalidSubProduct)
assert.Empty(t, files, "Expected response to be empty")
}

func TestGetFilesInvalidVersion(t *testing.T) {
files, _, _, err := ListFiles("vmware_tools", "vmtools", "666", testing_user, testing_pass)
files, _, _, err := ListFilesArray("vmware_tools", "vmtools", "666", testing_user, testing_pass)
assert.ErrorIs(t, err, sdk.ErrorInvalidVersion)
assert.Empty(t, files, "Expected response to be empty")
}

func TestGetFilesNotEntitled(t *testing.T) {
files, availability, apiVersions, err := ListFiles("vmware_nsx_t_data_center", "nsx-t", "3.1.3.1", testing_user, testing_pass)
files, availability, apiVersions, err := ListFilesArray("vmware_nsx_t_data_center", "nsx-t", "3.1.3.1", testing_user, testing_pass)
assert.Nil(t, err)
assert.Greater(t, len(files), 5, "Expected response to contain at least 5 items")
assert.Equal(t, apiVersions.MinorVersion, "3.1.3.1")
Expand Down
2 changes: 1 addition & 1 deletion api/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func EnsureLogin(username, password string) (err error) {
if err != nil {
return
}
fmt.Println("Logging in...")
fmt.Fprintf(os.Stderr, "Logging in...\n")
authenticatedClient, err = sdk.Login(username, password, jar)
if err == nil {
err = jar.Save()
Expand Down
2 changes: 0 additions & 2 deletions api/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package api
import (
"fmt"
"strings"
// "github.com/vmware-labs/vmware-customer-connect-sdk/sdk"
)

func ListVersions(slug, subProduct string) (data string, err error) {
Expand All @@ -18,6 +17,5 @@ func ListVersions(slug, subProduct string) (data string, err error) {
data = strings.Join(versionArray[:], "' '")
data = fmt.Sprintf("'%s'", data)

// fmt.Println(versionString)
return
}
4 changes: 2 additions & 2 deletions cmd/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ func downloadFromManifest() {
fmt.Printf("Opening manifest file: %s\n", manifestFile)
manifestArray, err := manifest.ProcessFile(manifestFile)
if err == manifest.ErrorFileDoesNotExist {
fmt.Printf("File %s does not exist", manifestFile)
fmt.Fprintf(os.Stderr, "File %s does not exist\n", manifestFile)
os.Exit(1)
} else if err == manifest.ErrorInvalidSpec {
os.Exit(1)
} else if err != nil {
fmt.Printf("Parsing file failed with error: %e\n", err)
fmt.Fprintf(os.Stderr, "Parsing file failed with error: %e\n", err)
os.Exit(1)
}

Expand Down
65 changes: 55 additions & 10 deletions cmd/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,24 @@
package cmd

import (
"encoding/json"
"fmt"
"os"

"github.com/spf13/cobra"
"github.com/vmware-labs/vmware-customer-connect-cli/api"
"github.com/vmware-labs/vmware-customer-connect-cli/presenters"
"github.com/vmware-labs/vmware-customer-connect-sdk/sdk"
)

var version string
type JsonOutput struct {
EulaAccepted bool `json:"eula_accepted"`
EligibleToDownload bool `json:"eligible_to_download"`
Version string `json:"version"`
Files []sdk.DownloadDetails `json:"files"`
}

var version, outputFormat string

// filesCmd represents the files command
var filesCmd = &cobra.Command{
Expand All @@ -24,25 +34,60 @@ Either VCC_USER and VCC_PASS environment variable must be set
or the --user and --pass flags should be added`,
Example: getFiles,
Run: func(cmd *cobra.Command, args []string) {
validateCredentials(cmd)
files, availability, apiVersions, err := api.ListFiles(slug, subProduct, version, username, password)
if err != nil {
handleErrors(err)
if !(outputFormat == "text" || outputFormat == "json") {
fmt.Fprintf(os.Stderr, "Format type %s is not supported\n", outputFormat)
os.Exit(128)
}
headings := []string{"Filename", "Size", "Build number", "Description"}

fmt.Printf("\nVersion: %s\n", apiVersions.MinorVersion)
fmt.Printf("Eula Accepted: %t\n", availability.EulaAccepted)
fmt.Printf("Eligable to Download: %t\n\n", availability.EligibleToDownload)
presenters.RenderTable(headings, files)
validateCredentials(cmd)
if outputFormat == "text" {
files, availability, apiVersions, err := api.ListFilesArray(slug, subProduct, version, username, password)
if err != nil {
handleErrors(err)
}
printText(apiVersions, availability, files)
} else if outputFormat == "json" {
dlgDetails, apiVersions, err := api.ListFiles(slug, subProduct, version, username, password)
if err != nil {
handleErrors(err)
}
printJson(dlgDetails, apiVersions)
}
},
}

func printText(apiVersions sdk.APIVersions, availability api.Availability, files [][]string) {
fmt.Printf("\nVersion: %s\n", apiVersions.MinorVersion)
fmt.Printf("Eula Accepted: %t\n", availability.EulaAccepted)
fmt.Printf("Eligable to Download: %t\n\n", availability.EligibleToDownload)

headings := []string{"Filename", "Size", "Build number", "Description"}
presenters.RenderTable(headings, files)
}

func printJson(dlgDetails sdk.DlgDetails, apiVersions sdk.APIVersions) {
var jsonOutput JsonOutput
jsonOutput.Version = apiVersions.MinorVersion
jsonOutput.EligibleToDownload = dlgDetails.EligibilityResponse.EligibleToDownload
jsonOutput.EulaAccepted = dlgDetails.EulaResponse.EulaAccepted
jsonOutput.Files = dlgDetails.DownloadDetails

var p []byte
// var err := error
p, err := json.MarshalIndent(jsonOutput, "", "\t")
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("%s \n", p)
}

func init() {
getCmd.AddCommand(filesCmd)
filesCmd.Flags().StringVarP(&slug, "product", "p", "", "Product code")
filesCmd.Flags().StringVarP(&subProduct, "subproduct", "s", "", "Sub Product code")
filesCmd.Flags().StringVarP(&version, "version", "v", "", "Version string")
filesCmd.Flags().StringVarP(&outputFormat, "format", "", "text", "Format of command output. Options [text, json]")
filesCmd.MarkFlagRequired("product")
filesCmd.MarkFlagRequired("sub-product")
filesCmd.MarkFlagRequired("version")
Expand Down
11 changes: 10 additions & 1 deletion test/bats/files.bats
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ setup() {
export_errors
}

@test "get files successfully" {
@test "get files successfully_text" {
run $VCC_CMD get files -p vmware_tools -s vmtools -v 11.3.0
echo $output
[[ "$output" == *" 11.3.0"* ]]
Expand All @@ -17,6 +17,15 @@ setup() {
[ "$status" -eq 0 ]
}

@test "get files successfully_json" {
run $VCC_CMD get files -p vmware_tools -s vmtools -v 11.3.0 --format json
echo $output
[[ "$output" == *"eula_accepted"* ]]
[[ "$output" == *"eligible_to_download"* ]]
[[ "$output" == *"sha256checksum"* ]]
[ "$status" -eq 0 ]
}

@test "get files with invalid product" {
run $VCC_CMD get files -p INVALID -s vmtools -v 11.3.0
echo $output
Expand Down

0 comments on commit c96f58e

Please sign in to comment.