Skip to content

Commit ca94683

Browse files
committed
Add opt package
1 parent 64178e1 commit ca94683

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed

kadai3-2/hioki-daichi/opt/opt.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
Package opt deals with CLI options.
3+
*/
4+
package opt
5+
6+
import (
7+
"errors"
8+
"flag"
9+
"net/url"
10+
"path"
11+
"time"
12+
)
13+
14+
var errExist = errors.New("file already exists")
15+
16+
// Options has the options required for parallel-download.
17+
type Options struct {
18+
Parallelism int
19+
Output string
20+
URL *url.URL
21+
Timeout time.Duration
22+
}
23+
24+
// Parse parses args and returns Options.
25+
func Parse(args ...string) (*Options, error) {
26+
flg := flag.NewFlagSet("parallel-download", flag.ExitOnError)
27+
28+
parallelism := flg.Int("p", 8, "Download files in parallel according to the specified number.")
29+
output := flg.String("o", "", "Save the downloaded file in the specified path. (Overwrite if duplicates.)")
30+
timeout := flg.Duration("t", 30*time.Second, "Terminate when the specified value has elapsed since download started.")
31+
32+
flg.Parse(args)
33+
34+
u, err := url.ParseRequestURI(flg.Arg(0))
35+
if err != nil {
36+
return nil, err
37+
}
38+
39+
if *output == "" {
40+
_, filename := path.Split(u.Path)
41+
42+
// Inspired by the --default-page option of wget
43+
if filename == "" {
44+
filename = "index.html"
45+
}
46+
47+
*output = filename
48+
}
49+
50+
return &Options{
51+
Parallelism: *parallelism,
52+
Output: *output,
53+
URL: u,
54+
Timeout: *timeout,
55+
}, nil
56+
}

kadai3-2/hioki-daichi/opt/opt_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package opt
2+
3+
import (
4+
"net/url"
5+
"reflect"
6+
"testing"
7+
)
8+
9+
func TestMain_parse(t *testing.T) {
10+
cases := map[string]struct {
11+
args []string
12+
expectedParallelism int
13+
expectedOutput string
14+
expectedURLString string
15+
}{
16+
"no options": {args: []string{"http://example.com/foo.png"}, expectedParallelism: 8, expectedOutput: "foo.png", expectedURLString: "http://example.com/foo.png"},
17+
"-p=2": {args: []string{"-p=2", "http://example.com/foo.png"}, expectedParallelism: 2, expectedOutput: "foo.png", expectedURLString: "http://example.com/foo.png"},
18+
"-o=bar.png": {args: []string{"-o=bar.png", "http://example.com/foo.png"}, expectedParallelism: 8, expectedOutput: "bar.png", expectedURLString: "http://example.com/foo.png"},
19+
"index.html": {args: []string{"http://example.com/"}, expectedParallelism: 8, expectedOutput: "index.html", expectedURLString: "http://example.com/"},
20+
}
21+
22+
for n, c := range cases {
23+
c := c
24+
t.Run(n, func(t *testing.T) {
25+
args := c.args
26+
expectedParallelism := c.expectedParallelism
27+
expectedOutput := c.expectedOutput
28+
expectedURL, err := url.ParseRequestURI(c.expectedURLString)
29+
if err != nil {
30+
t.Fatalf("err %s", err)
31+
}
32+
33+
opts, err := Parse(args...)
34+
if err != nil {
35+
t.Fatalf("err %s", err)
36+
}
37+
38+
actualParallelism := opts.Parallelism
39+
actualOutput := opts.Output
40+
actualURL := opts.URL
41+
42+
if actualParallelism != expectedParallelism {
43+
t.Errorf(`unexpected parallelism: expected: %d actual: %d`, expectedParallelism, actualParallelism)
44+
}
45+
46+
if actualOutput != expectedOutput {
47+
t.Errorf(`unexpected output: expected: "%s" actual: "%s"`, expectedOutput, actualOutput)
48+
}
49+
50+
if !reflect.DeepEqual(actualURL, expectedURL) {
51+
t.Errorf(`unexpected URL: expected: "%s" actual: "%s"`, expectedURL, actualURL)
52+
}
53+
})
54+
}
55+
}
56+
57+
func TestMain_parse_InvalidURL(t *testing.T) {
58+
t.Parallel()
59+
60+
expected := "parse %: invalid URI for request"
61+
62+
_, err := Parse([]string{"%"}...)
63+
if err == nil {
64+
t.Fatal("Unexpectedly err was nil")
65+
}
66+
67+
actual := err.Error()
68+
if actual != expected {
69+
t.Errorf(`unexpected error: expected: "%s" actual: "%s"`, expected, actual)
70+
}
71+
}

0 commit comments

Comments
 (0)