diff --git a/main.go b/main.go index 38420ca..0384f49 100644 --- a/main.go +++ b/main.go @@ -123,11 +123,7 @@ func runFetchWrapper(c *cli.Context) { // Run the fetch program func runFetch(c *cli.Context) error { - options, err := parseOptions(c) - if err != nil { - return err - } - + options := parseOptions(c) if err := validateOptions(options); err != nil { return err } @@ -198,7 +194,7 @@ func runFetch(c *cli.Context) error { return nil } -func parseOptions(c *cli.Context) (FetchOptions, error) { +func parseOptions(c *cli.Context) FetchOptions { localDownloadPath := c.Args().First() sourcePaths := c.StringSlice(OPTION_SOURCE_PATH) assetChecksums := c.StringSlice(OPTION_RELEASE_ASSET_CHECKSUM) @@ -212,37 +208,6 @@ func parseOptions(c *cli.Context) (FetchOptions, error) { localDownloadPath = c.Args().Get(1) } - optionsList := []string{ - OPTION_REPO, - OPTION_TAG, - OPTION_COMMIT, - OPTION_BRANCH, - OPTION_GITHUB_TOKEN, - OPTION_SOURCE_PATH, - OPTION_RELEASE_ASSET, - OPTION_RELEASE_ASSET_CHECKSUM, - OPTION_RELEASE_ASSET_CHECKSUM_ALGO, - OPTION_GITHUB_API_VERSION, - OPTION_WITH_PROGRESS, - } - - for _, option := range optionsList { - switch option { - case OPTION_SOURCE_PATH, OPTION_RELEASE_ASSET_CHECKSUM: - if c.IsSet(option) { - for _, slice := range c.StringSlice(option) { - if slice == "" { - return FetchOptions{}, fmt.Errorf("You specified the --%s flag but did not provide any value.", option) - } - } - } - default: - if c.IsSet(option) && c.String(option) == "" { - return FetchOptions{}, fmt.Errorf("You specified the --%s flag but did not provide any value.", option) - } - } - } - for _, assetChecksum := range assetChecksums { assetChecksumMap[assetChecksum] = true } @@ -260,7 +225,7 @@ func parseOptions(c *cli.Context) (FetchOptions, error) { LocalDownloadPath: localDownloadPath, GithubApiVersion: c.String(OPTION_GITHUB_API_VERSION), WithProgress: c.IsSet(OPTION_WITH_PROGRESS), - }, nil + } } func validateOptions(options FetchOptions) error { diff --git a/main_test.go b/main_test.go index 3c07e8e..2a7fae7 100644 --- a/main_test.go +++ b/main_test.go @@ -4,8 +4,6 @@ import ( "fmt" "os" "testing" - - cli "gopkg.in/urfave/cli.v1" ) // Expect to download 2 assets: @@ -60,85 +58,3 @@ func TestInvalidReleaseAssetsRegex(t *testing.T) { t.Fatalf("Expected error for invalid regex") } } - -func TestEmptyOptionValues(t *testing.T) { - app := cli.NewApp() - app.Name = "main_test" - app.Flags = []cli.Flag{ - cli.StringFlag{ - Name: OPTION_REPO, - }, - cli.StringFlag{ - Name: OPTION_COMMIT, - }, - cli.StringFlag{ - Name: OPTION_BRANCH, - }, - cli.StringFlag{ - Name: OPTION_TAG, - }, - cli.StringFlag{ - Name: OPTION_GITHUB_TOKEN, - EnvVar: ENV_VAR_GITHUB_TOKEN, - }, - cli.StringSliceFlag{ - Name: OPTION_SOURCE_PATH, - }, - cli.StringFlag{ - Name: OPTION_RELEASE_ASSET, - }, - cli.StringSliceFlag{ - Name: OPTION_RELEASE_ASSET_CHECKSUM, - }, - cli.StringFlag{ - Name: OPTION_RELEASE_ASSET_CHECKSUM_ALGO, - }, - cli.StringFlag{ - Name: OPTION_GITHUB_API_VERSION, - Value: "v3", - }, - cli.StringFlag{ - Name: OPTION_WITH_PROGRESS, - }, - } - - app.Action = func(c *cli.Context) error { - _, err := parseOptions(c) - return err - } - - var args []string - var expected string - var err error - - optionsList := []string{ - OPTION_REPO, - OPTION_COMMIT, - OPTION_BRANCH, - OPTION_TAG, - OPTION_GITHUB_TOKEN, - OPTION_SOURCE_PATH, - OPTION_RELEASE_ASSET, - OPTION_RELEASE_ASSET_CHECKSUM, - OPTION_RELEASE_ASSET_CHECKSUM_ALGO, - OPTION_GITHUB_API_VERSION, - OPTION_WITH_PROGRESS, - } - - for _, option := range optionsList { - args = os.Args[0:1] - dashedOption := "--" + option - emptyOption := dashedOption + "=" - args = append(args, emptyOption) - - err = app.Run(args) - expected = fmt.Sprintf("You specified the %s flag but did not provide any value.", dashedOption) - if (err != nil) && (err.Error() != expected) { - t.Fatalf("Expected '%s' but received '%s'", expected, err.Error()) - } - - if err == nil { - t.Fatalf("Expected '%s' but received nothing", expected) - } - } -}