Skip to content

Commit

Permalink
add --type validation
Browse files Browse the repository at this point in the history
add more bats tests
  • Loading branch information
Matt Proud committed Dec 8, 2023
1 parent be18d5f commit 8ee63ab
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 13 deletions.
3 changes: 1 addition & 2 deletions cmd/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"io"
"os"
"path/filepath"
"strings"

"github.com/spf13/cobra"
"github.com/vmware-labs/vmware-customer-connect-cli/api"
Expand Down Expand Up @@ -37,6 +36,7 @@ Either VCC_USER and VCC_PASS environment variable must be set
or the --user and --pass flags should be added`,
Example: downloadUsage,
Run: func(cmd *cobra.Command, args []string) {
dlgType = validateDlgType(dlgType)
validateCredentials(cmd)
validateOutputDir()
manifestWorkflow := validateDownloadFlags(cmd)
Expand Down Expand Up @@ -140,5 +140,4 @@ func init() {
downloadCmd.Flags().BoolVarP(&acceptEula, "accepteula", "a", false, "Filename string")
downloadCmd.Flags().BoolVarP(&forceDownload, "forcedownload", "d", false, "(optional) Force a file to be re-downloaded even if it already exists")
downloadCmd.Flags().StringVarP(&dlgType, "type", "t", "product_binary", "(optional) Download type. One of: (product_binary, drivers_tools, custom_iso, addons). Default: product_binary")
dlgType = strings.ToUpper(dlgType)
}
3 changes: 1 addition & 2 deletions cmd/eula.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package cmd

import (
"fmt"
"strings"

"github.com/spf13/cobra"
"github.com/vmware-labs/vmware-customer-connect-cli/api"
Expand All @@ -22,6 +21,7 @@ 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) {
dlgType = validateDlgType(dlgType)
validateCredentials(cmd)
eula, err := api.GetEula(slug, subProduct, version, username, password, dlgType)
handleErrors(err)
Expand All @@ -38,5 +38,4 @@ func init() {
eulaCmd.MarkFlagRequired("sub-product")
eulaCmd.MarkFlagRequired("version")
eulaCmd.Flags().StringVarP(&dlgType, "type", "t", "product_binary", "(optional) Download type. One of: (product_binary, drivers_tools, custom_iso, addons). Default: product_binary")
dlgType = strings.ToUpper(dlgType)
}
3 changes: 1 addition & 2 deletions cmd/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"encoding/json"
"fmt"
"os"
"strings"

"github.com/spf13/cobra"
"github.com/vmware-labs/vmware-customer-connect-cli/api"
Expand Down Expand Up @@ -35,6 +34,7 @@ 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) {
dlgType = validateDlgType(dlgType)
if !(outputFormat == "text" || outputFormat == "json") {
fmt.Fprintf(os.Stderr, "Format type %s is not supported\n", outputFormat)
os.Exit(128)
Expand Down Expand Up @@ -89,5 +89,4 @@ func init() {
filesCmd.MarkFlagRequired("sub-product")
filesCmd.MarkFlagRequired("version")
filesCmd.Flags().StringVarP(&dlgType, "type", "t", "product_binary", "(optional) Download type. One of: (product_binary, drivers_tools, custom_iso, addons). Default: product_binary")
dlgType = strings.ToUpper(dlgType)
}
9 changes: 5 additions & 4 deletions cmd/subproducts.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package cmd
import (
"fmt"
"os"
"strings"

"github.com/spf13/cobra"
"github.com/vmware-labs/vmware-customer-connect-cli/api"
Expand All @@ -15,6 +14,8 @@ import (

var dlgType, majorVersion, slug string



// subproductsCmd represents the subproducts command
var subproductsCmd = &cobra.Command{
Use: "subproducts",
Expand All @@ -23,12 +24,13 @@ var subproductsCmd = &cobra.Command{
Long: "List sub-products for a specified product",
Example: getSubProductsUsage,
Run: func(cmd *cobra.Command, args []string) {
dlgType = validateDlgType(dlgType)
products, err := api.ListSubProducts(slug, dlgType, majorVersion)
handleErrors(err)
headings := []string{"Sub-Product Code", "Description"}
presenters.RenderTable(headings, products)

if dlgType == "drivers_tools" && len(products) > 200 {
if dlgType == "drivers_tools" && majorVersion != "" && len(products) > 200 {
majorVersions, err := api.GetMajorVersionsString(slug)
handleErrors(err)
fmt.Println("\nDue to the high number of results it's recommended to provide the major version to cut down results.")
Expand All @@ -42,7 +44,6 @@ func init() {
getCmd.AddCommand(subproductsCmd)
subproductsCmd.Flags().StringVarP(&slug, "product", "p", "", "Product code")
subproductsCmd.MarkFlagRequired("product")
subproductsCmd.Flags().StringVarP(&dlgType, "type", "t", "product_binary", "(optional) Download type. One of: (product_binary, drivers_tools, custom_iso, addons). Default: product_binary")
dlgType = strings.ToUpper(dlgType)
subproductsCmd.Flags().StringVarP(&dlgType, "type", "t", "product_binary", "(optional) Download type. Allowed: product_binary, drivers_tools, custom_iso, addons. (Default: product_binary)")
subproductsCmd.Flags().StringVarP(&majorVersion, "majorversion", "m", "", "(optional) Reduce the number of results by passing in the major verion.\nGet versions with `vcc get majorversions -p vmware_vsphere`")
}
12 changes: 12 additions & 0 deletions cmd/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,22 @@ import (
"os"
"path/filepath"
"runtime"
"slices"
"strings"

"github.com/spf13/cobra"
)

func validateDlgType(dlgType string) (returnDlgType string) {
returnDlgType = strings.ToUpper(dlgType)
allowedFlags := []string{"PRODUCT_BINARY", "DRIVERS_TOOLS", "CUSTOM_ISO", "ADDONS"}
if !slices.Contains(allowedFlags, returnDlgType) {
fmt.Fprintf(os.Stderr, "\n--type '%s' invalid. Supported types are: (product_binary, drivers_tools, custom_iso, addons)\n", dlgType)
os.Exit(1)
}
return
}

func validateOutputDir() {
if outputDir == "" {
home := homeDir()
Expand Down
3 changes: 1 addition & 2 deletions cmd/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package cmd

import (
"fmt"
"strings"

"github.com/spf13/cobra"
"github.com/vmware-labs/vmware-customer-connect-cli/api"
Expand All @@ -21,6 +20,7 @@ var versionsCmd = &cobra.Command{
Long: "List available versions of a sub-product",
Example: getVersions,
Run: func(cmd *cobra.Command, args []string) {
dlgType = validateDlgType(dlgType)
versionString, err := api.ListVersions(slug, subProduct, dlgType)
handleErrors(err)
fmt.Println(versionString)
Expand All @@ -34,5 +34,4 @@ func init() {
versionsCmd.MarkFlagRequired("product")
versionsCmd.MarkFlagRequired("sub-product")
versionsCmd.Flags().StringVarP(&dlgType, "type", "t", "product_binary", "(optional) Download type. One of: (product_binary, drivers_tools, custom_iso, addons). Default: product_binary")
dlgType = strings.ToUpper(dlgType)
}
15 changes: 15 additions & 0 deletions test/bats/download.bats
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@ teardown() {
[ -f $TEMP_DIR/VMware-Horizon-Client-*.apk ]
}

@test "download driver file successfully to temp" {
$VCC_CMD logout
rm -f $TEMP_DIR/*
local cmd="$VCC_CMD download -p vmware_horizon_clients -s cart+andrd_x8632 -v 2106 -f VMware-Horizon-Client-AndroidOS-x86-*-store.apk --accepteula -o $TEMP_DIR"
echo $cmd
run $cmd
echo "$output"
[[ "$output" != *"No output directory set."* ]]
[[ "$output" == *"Collecting download payload"* ]]
[[ "$output" == *"Download started to"* ]]
[[ "$output" == *"Download finished"* ]]
[ "$status" -eq 0 ]
[ -f $TEMP_DIR/VMware-Horizon-Client-*.apk ]
}

@test "re-download single file successfully to temp" {
$VCC_CMD logout
rm -f $TEMP_DIR/*
Expand Down
9 changes: 8 additions & 1 deletion test/bats/subproducts.bats
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@ setup() {
[ "$status" -eq 0 ]
}

@test "get subproducts successfully - drivers with version" {
@test "get subproducts drivers with version successfully" {
run $VCC_CMD get subproducts -p vmware_vsphere -t drivers_tool
echo $output
[[ "$output" == *"Supported types are:"* ]]
[ "$status" -eq 1 ]
}

@test "get subproducts drivers with version invalid type" {
run $VCC_CMD get subproducts -p vmware_vsphere -t drivers_tools -m 8_0
echo $output
[[ "$output" == *"ESX"* ]]
Expand Down

0 comments on commit 8ee63ab

Please sign in to comment.