Skip to content

Commit 8ee63ab

Browse files
committed
add --type validation
add more bats tests
1 parent be18d5f commit 8ee63ab

File tree

8 files changed

+44
-13
lines changed

8 files changed

+44
-13
lines changed

cmd/download.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"io"
1010
"os"
1111
"path/filepath"
12-
"strings"
1312

1413
"github.com/spf13/cobra"
1514
"github.com/vmware-labs/vmware-customer-connect-cli/api"
@@ -37,6 +36,7 @@ Either VCC_USER and VCC_PASS environment variable must be set
3736
or the --user and --pass flags should be added`,
3837
Example: downloadUsage,
3938
Run: func(cmd *cobra.Command, args []string) {
39+
dlgType = validateDlgType(dlgType)
4040
validateCredentials(cmd)
4141
validateOutputDir()
4242
manifestWorkflow := validateDownloadFlags(cmd)
@@ -140,5 +140,4 @@ func init() {
140140
downloadCmd.Flags().BoolVarP(&acceptEula, "accepteula", "a", false, "Filename string")
141141
downloadCmd.Flags().BoolVarP(&forceDownload, "forcedownload", "d", false, "(optional) Force a file to be re-downloaded even if it already exists")
142142
downloadCmd.Flags().StringVarP(&dlgType, "type", "t", "product_binary", "(optional) Download type. One of: (product_binary, drivers_tools, custom_iso, addons). Default: product_binary")
143-
dlgType = strings.ToUpper(dlgType)
144143
}

cmd/eula.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package cmd
55

66
import (
77
"fmt"
8-
"strings"
98

109
"github.com/spf13/cobra"
1110
"github.com/vmware-labs/vmware-customer-connect-cli/api"
@@ -22,6 +21,7 @@ Either VCC_USER and VCC_PASS environment variable must be set
2221
or the --user and --pass flags should be added`,
2322
Example: getFiles,
2423
Run: func(cmd *cobra.Command, args []string) {
24+
dlgType = validateDlgType(dlgType)
2525
validateCredentials(cmd)
2626
eula, err := api.GetEula(slug, subProduct, version, username, password, dlgType)
2727
handleErrors(err)
@@ -38,5 +38,4 @@ func init() {
3838
eulaCmd.MarkFlagRequired("sub-product")
3939
eulaCmd.MarkFlagRequired("version")
4040
eulaCmd.Flags().StringVarP(&dlgType, "type", "t", "product_binary", "(optional) Download type. One of: (product_binary, drivers_tools, custom_iso, addons). Default: product_binary")
41-
dlgType = strings.ToUpper(dlgType)
4241
}

cmd/files.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"encoding/json"
88
"fmt"
99
"os"
10-
"strings"
1110

1211
"github.com/spf13/cobra"
1312
"github.com/vmware-labs/vmware-customer-connect-cli/api"
@@ -35,6 +34,7 @@ Either VCC_USER and VCC_PASS environment variable must be set
3534
or the --user and --pass flags should be added`,
3635
Example: getFiles,
3736
Run: func(cmd *cobra.Command, args []string) {
37+
dlgType = validateDlgType(dlgType)
3838
if !(outputFormat == "text" || outputFormat == "json") {
3939
fmt.Fprintf(os.Stderr, "Format type %s is not supported\n", outputFormat)
4040
os.Exit(128)
@@ -89,5 +89,4 @@ func init() {
8989
filesCmd.MarkFlagRequired("sub-product")
9090
filesCmd.MarkFlagRequired("version")
9191
filesCmd.Flags().StringVarP(&dlgType, "type", "t", "product_binary", "(optional) Download type. One of: (product_binary, drivers_tools, custom_iso, addons). Default: product_binary")
92-
dlgType = strings.ToUpper(dlgType)
9392
}

cmd/subproducts.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package cmd
66
import (
77
"fmt"
88
"os"
9-
"strings"
109

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

1615
var dlgType, majorVersion, slug string
1716

17+
18+
1819
// subproductsCmd represents the subproducts command
1920
var subproductsCmd = &cobra.Command{
2021
Use: "subproducts",
@@ -23,12 +24,13 @@ var subproductsCmd = &cobra.Command{
2324
Long: "List sub-products for a specified product",
2425
Example: getSubProductsUsage,
2526
Run: func(cmd *cobra.Command, args []string) {
27+
dlgType = validateDlgType(dlgType)
2628
products, err := api.ListSubProducts(slug, dlgType, majorVersion)
2729
handleErrors(err)
2830
headings := []string{"Sub-Product Code", "Description"}
2931
presenters.RenderTable(headings, products)
3032

31-
if dlgType == "drivers_tools" && len(products) > 200 {
33+
if dlgType == "drivers_tools" && majorVersion != "" && len(products) > 200 {
3234
majorVersions, err := api.GetMajorVersionsString(slug)
3335
handleErrors(err)
3436
fmt.Println("\nDue to the high number of results it's recommended to provide the major version to cut down results.")
@@ -42,7 +44,6 @@ func init() {
4244
getCmd.AddCommand(subproductsCmd)
4345
subproductsCmd.Flags().StringVarP(&slug, "product", "p", "", "Product code")
4446
subproductsCmd.MarkFlagRequired("product")
45-
subproductsCmd.Flags().StringVarP(&dlgType, "type", "t", "product_binary", "(optional) Download type. One of: (product_binary, drivers_tools, custom_iso, addons). Default: product_binary")
46-
dlgType = strings.ToUpper(dlgType)
47+
subproductsCmd.Flags().StringVarP(&dlgType, "type", "t", "product_binary", "(optional) Download type. Allowed: product_binary, drivers_tools, custom_iso, addons. (Default: product_binary)")
4748
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`")
4849
}

cmd/validators.go

+12
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,22 @@ import (
88
"os"
99
"path/filepath"
1010
"runtime"
11+
"slices"
12+
"strings"
1113

1214
"github.com/spf13/cobra"
1315
)
1416

17+
func validateDlgType(dlgType string) (returnDlgType string) {
18+
returnDlgType = strings.ToUpper(dlgType)
19+
allowedFlags := []string{"PRODUCT_BINARY", "DRIVERS_TOOLS", "CUSTOM_ISO", "ADDONS"}
20+
if !slices.Contains(allowedFlags, returnDlgType) {
21+
fmt.Fprintf(os.Stderr, "\n--type '%s' invalid. Supported types are: (product_binary, drivers_tools, custom_iso, addons)\n", dlgType)
22+
os.Exit(1)
23+
}
24+
return
25+
}
26+
1527
func validateOutputDir() {
1628
if outputDir == "" {
1729
home := homeDir()

cmd/versions.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package cmd
55

66
import (
77
"fmt"
8-
"strings"
98

109
"github.com/spf13/cobra"
1110
"github.com/vmware-labs/vmware-customer-connect-cli/api"
@@ -21,6 +20,7 @@ var versionsCmd = &cobra.Command{
2120
Long: "List available versions of a sub-product",
2221
Example: getVersions,
2322
Run: func(cmd *cobra.Command, args []string) {
23+
dlgType = validateDlgType(dlgType)
2424
versionString, err := api.ListVersions(slug, subProduct, dlgType)
2525
handleErrors(err)
2626
fmt.Println(versionString)
@@ -34,5 +34,4 @@ func init() {
3434
versionsCmd.MarkFlagRequired("product")
3535
versionsCmd.MarkFlagRequired("sub-product")
3636
versionsCmd.Flags().StringVarP(&dlgType, "type", "t", "product_binary", "(optional) Download type. One of: (product_binary, drivers_tools, custom_iso, addons). Default: product_binary")
37-
dlgType = strings.ToUpper(dlgType)
3837
}

test/bats/download.bats

+15
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,21 @@ teardown() {
3131
[ -f $TEMP_DIR/VMware-Horizon-Client-*.apk ]
3232
}
3333

34+
@test "download driver file successfully to temp" {
35+
$VCC_CMD logout
36+
rm -f $TEMP_DIR/*
37+
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"
38+
echo $cmd
39+
run $cmd
40+
echo "$output"
41+
[[ "$output" != *"No output directory set."* ]]
42+
[[ "$output" == *"Collecting download payload"* ]]
43+
[[ "$output" == *"Download started to"* ]]
44+
[[ "$output" == *"Download finished"* ]]
45+
[ "$status" -eq 0 ]
46+
[ -f $TEMP_DIR/VMware-Horizon-Client-*.apk ]
47+
}
48+
3449
@test "re-download single file successfully to temp" {
3550
$VCC_CMD logout
3651
rm -f $TEMP_DIR/*

test/bats/subproducts.bats

+8-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,14 @@ setup() {
2121
[ "$status" -eq 0 ]
2222
}
2323

24-
@test "get subproducts successfully - drivers with version" {
24+
@test "get subproducts drivers with version successfully" {
25+
run $VCC_CMD get subproducts -p vmware_vsphere -t drivers_tool
26+
echo $output
27+
[[ "$output" == *"Supported types are:"* ]]
28+
[ "$status" -eq 1 ]
29+
}
30+
31+
@test "get subproducts drivers with version invalid type" {
2532
run $VCC_CMD get subproducts -p vmware_vsphere -t drivers_tools -m 8_0
2633
echo $output
2734
[[ "$output" == *"ESX"* ]]

0 commit comments

Comments
 (0)