Skip to content

Commit 1ca7fcb

Browse files
authored
Merge pull request #55 from vedala/empty-value-wrong-error
Check and report error on empty option values
2 parents 074962e + 8b9822b commit 1ca7fcb

File tree

2 files changed

+117
-3
lines changed

2 files changed

+117
-3
lines changed

main.go

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,11 @@ func runFetchWrapper(c *cli.Context) {
117117

118118
// Run the fetch program
119119
func runFetch(c *cli.Context) error {
120-
options := parseOptions(c)
120+
options, err := parseOptions(c)
121+
if err != nil {
122+
return err
123+
}
124+
121125
if err := validateOptions(options); err != nil {
122126
return err
123127
}
@@ -188,7 +192,7 @@ func runFetch(c *cli.Context) error {
188192
return nil
189193
}
190194

191-
func parseOptions(c *cli.Context) FetchOptions {
195+
func parseOptions(c *cli.Context) (FetchOptions, error) {
192196
localDownloadPath := c.Args().First()
193197
sourcePaths := c.StringSlice(OPTION_SOURCE_PATH)
194198
assetChecksums := c.StringSlice(OPTION_RELEASE_ASSET_CHECKSUM)
@@ -202,6 +206,36 @@ func parseOptions(c *cli.Context) FetchOptions {
202206
localDownloadPath = c.Args().Get(1)
203207
}
204208

209+
optionsList := []string{
210+
OPTION_REPO,
211+
OPTION_TAG,
212+
OPTION_COMMIT,
213+
OPTION_BRANCH,
214+
OPTION_GITHUB_TOKEN,
215+
OPTION_SOURCE_PATH,
216+
OPTION_RELEASE_ASSET,
217+
OPTION_RELEASE_ASSET_CHECKSUM,
218+
OPTION_RELEASE_ASSET_CHECKSUM_ALGO,
219+
OPTION_GITHUB_API_VERSION,
220+
}
221+
222+
for _, option := range optionsList {
223+
switch option {
224+
case OPTION_SOURCE_PATH, OPTION_RELEASE_ASSET_CHECKSUM:
225+
if c.IsSet(option) {
226+
for _, slice := range c.StringSlice(option) {
227+
if slice == "" {
228+
return FetchOptions{}, fmt.Errorf("You specified the --%s flag but did not provide any value.", option)
229+
}
230+
}
231+
}
232+
default:
233+
if c.IsSet(option) && c.String(option) == "" {
234+
return FetchOptions{}, fmt.Errorf("You specified the --%s flag but did not provide any value.", option)
235+
}
236+
}
237+
}
238+
205239
for _, assetChecksum := range assetChecksums {
206240
assetChecksumMap[assetChecksum] = true
207241
}
@@ -218,7 +252,7 @@ func parseOptions(c *cli.Context) FetchOptions {
218252
ReleaseAssetChecksumAlgo: c.String(OPTION_RELEASE_ASSET_CHECKSUM_ALGO),
219253
LocalDownloadPath: localDownloadPath,
220254
GithubApiVersion: c.String(OPTION_GITHUB_API_VERSION),
221-
}
255+
}, nil
222256
}
223257

224258
func validateOptions(options FetchOptions) error {

main_test.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"fmt"
55
"os"
66
"testing"
7+
8+
cli "gopkg.in/urfave/cli.v1"
79
)
810

911
// Expect to download 2 assets:
@@ -58,3 +60,81 @@ func TestInvalidReleaseAssetsRegex(t *testing.T) {
5860
t.Fatalf("Expected error for invalid regex")
5961
}
6062
}
63+
64+
func TestEmptyOptionValues(t *testing.T) {
65+
app := cli.NewApp()
66+
app.Name = "main_test"
67+
app.Flags = []cli.Flag{
68+
cli.StringFlag{
69+
Name: OPTION_REPO,
70+
},
71+
cli.StringFlag{
72+
Name: OPTION_COMMIT,
73+
},
74+
cli.StringFlag{
75+
Name: OPTION_BRANCH,
76+
},
77+
cli.StringFlag{
78+
Name: OPTION_TAG,
79+
},
80+
cli.StringFlag{
81+
Name: OPTION_GITHUB_TOKEN,
82+
EnvVar: ENV_VAR_GITHUB_TOKEN,
83+
},
84+
cli.StringSliceFlag{
85+
Name: OPTION_SOURCE_PATH,
86+
},
87+
cli.StringFlag{
88+
Name: OPTION_RELEASE_ASSET,
89+
},
90+
cli.StringSliceFlag{
91+
Name: OPTION_RELEASE_ASSET_CHECKSUM,
92+
},
93+
cli.StringFlag{
94+
Name: OPTION_RELEASE_ASSET_CHECKSUM_ALGO,
95+
},
96+
cli.StringFlag{
97+
Name: OPTION_GITHUB_API_VERSION,
98+
Value: "v3",
99+
},
100+
}
101+
102+
app.Action = func(c *cli.Context) error {
103+
_, err := parseOptions(c)
104+
return err
105+
}
106+
107+
var args []string
108+
var expected string
109+
var err error
110+
111+
optionsList := []string{
112+
OPTION_REPO,
113+
OPTION_COMMIT,
114+
OPTION_BRANCH,
115+
OPTION_TAG,
116+
OPTION_GITHUB_TOKEN,
117+
OPTION_SOURCE_PATH,
118+
OPTION_RELEASE_ASSET,
119+
OPTION_RELEASE_ASSET_CHECKSUM,
120+
OPTION_RELEASE_ASSET_CHECKSUM_ALGO,
121+
OPTION_GITHUB_API_VERSION,
122+
}
123+
124+
for _, option := range optionsList {
125+
args = os.Args[0:1]
126+
dashedOption := "--" + option
127+
emptyOption := dashedOption + "="
128+
args = append(args, emptyOption)
129+
130+
err = app.Run(args)
131+
expected = fmt.Sprintf("You specified the %s flag but did not provide any value.", dashedOption)
132+
if (err != nil) && (err.Error() != expected) {
133+
t.Fatalf("Expected '%s' but received '%s'", expected, err.Error())
134+
}
135+
136+
if err == nil {
137+
t.Fatalf("Expected '%s' but received nothing", expected)
138+
}
139+
}
140+
}

0 commit comments

Comments
 (0)