diff --git a/api/files.go b/api/files.go index 27a80e1..6710b2a 100644 --- a/api/files.go +++ b/api/files.go @@ -5,6 +5,7 @@ package api import ( "fmt" + "os" "github.com/vmware-labs/vmware-customer-connect-sdk/sdk" ) @@ -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 } @@ -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 -} +} \ No newline at end of file diff --git a/api/files_test.go b/api/files_test.go index 6f2c479..9e218a2 100644 --- a/api/files_test.go +++ b/api/files_test.go @@ -14,7 +14,7 @@ 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") @@ -22,25 +22,25 @@ func TestGetFiles(t *testing.T) { } 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") diff --git a/api/login.go b/api/login.go index f47396a..ab81c99 100644 --- a/api/login.go +++ b/api/login.go @@ -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() diff --git a/api/versions.go b/api/versions.go index f85cc18..ba7387a 100644 --- a/api/versions.go +++ b/api/versions.go @@ -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) { @@ -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 } diff --git a/cmd/download.go b/cmd/download.go index b9a82ce..126d684 100644 --- a/cmd/download.go +++ b/cmd/download.go @@ -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) } diff --git a/cmd/files.go b/cmd/files.go index 7417773..1211296 100644 --- a/cmd/files.go +++ b/cmd/files.go @@ -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{ @@ -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") diff --git a/test/bats/files.bats b/test/bats/files.bats index b0ea0b7..c43f403 100644 --- a/test/bats/files.bats +++ b/test/bats/files.bats @@ -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"* ]] @@ -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