generated from gopherdojo/template
-
Notifications
You must be signed in to change notification settings - Fork 179
Kadai3 2 misonog #53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
misonog
wants to merge
38
commits into
gopherdojo:master
Choose a base branch
from
misonog:kadai3-2-misonog
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Kadai3 2 misonog #53
Changes from 23 commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
ee9ad8d
Initialize Repository
misonog cbddf50
Create Makefile
misonog 88b2d43
Add download func
misonog 3af52b8
Add target directory option
misonog 38f84cb
Add Data struct and func
misonog 588096f
Add Utils interface
misonog 8e744fc
Add Check func
misonog e4ba981
add pdownload constructor
misonog 84fe8b8
Add pdownload test
misonog 09d012a
Change test file name
misonog 3ff17b7
Add Go sync
misonog 72d57bd
Add assignment func
misonog ad5b30e
Add Download func
misonog 54f2a64
Add MergeFiles method
misonog 0664606
Add parseURL func
misonog 7005d93
CLI setup
misonog ba7f971
Add SetDirName func
misonog 6f18cc2
Add run test
misonog 4ccd9be
Add tempdir for download
misonog 01395e9
Add termination package for cancel process
misonog 004ee1e
Add context for Ctrl + c
misonog bdada63
Add timeout setting
misonog c0d5713
Add testdata and Update README
misonog 230579e
Update flag package parse to main function
misonog 57fee38
Update timeout from Int type to time.Duration type
misonog d34d94e
Fix error handling
misonog 40aaf93
Update context in main function
misonog b2e7766
Update context
misonog b82fb95
Fix context handling
misonog aa2e3ae
Fix const name from upper case to camel case
misonog 51d9d48
Reimplemented pdwonload func without Data struct
misonog c4b316f
Remove MakeRange
misonog e5b722c
Remove MergeFiles
misonog 7af0bb5
Remove getter
misonog 25034f2
Remove setter
misonog 37a3051
Remove Data struct
misonog 664bf28
Remove Utils interface
misonog 81a25e8
Remove unnecessary comments
misonog File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Binaries for programs and plugins | ||
*.exe | ||
*.exe~ | ||
*.dll | ||
*.so | ||
*.dylib | ||
/pdownload | ||
|
||
# Test binary, built with `go test -c` | ||
*.test | ||
|
||
# Output of the go coverage tool, specifically when used with LiteIDE | ||
*.out | ||
|
||
# Dependency directories (remove the comment below to include it) | ||
# vendor/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
BINARY_NAME=pdownload | ||
|
||
all: test build | ||
|
||
build: | ||
go build -o $(BINARY_NAME) | ||
|
||
test: | ||
go test -v ./... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# 分割ダウンローダ | ||
|
||
## 仕様 | ||
|
||
- 分割ダウンロードを行う | ||
- Range アクセスを用いる | ||
- いくつかのゴルーチンでダウンロードしてマージする | ||
- エラー処理を工夫する | ||
- golang.org/x/sync/errgourp パッケージなどを使ってみる | ||
- キャンセルが発生した場合の実装を行う | ||
|
||
## オプション | ||
|
||
| オプション | 内容 | デフォルト | | ||
| ---------- | -------------------------------------- | ---------- | | ||
| -d | ファイルをダウンロードするディレクトリ | $PWD | | ||
| -t | タイムアウトするまでの時間(秒) | 10 | | ||
|
||
## 利用方法 | ||
|
||
### setup | ||
|
||
```shell | ||
$ make # テスト & ビルド | ||
``` | ||
|
||
### ダウンロードコマンドの例 | ||
|
||
```shell | ||
$ ./pdownload https://blog.golang.org/gopher/header.jpg | ||
$ # ディレクトリとタイムアウトまでの時間の指定 | ||
$ ./pdownload -d testdata/ -t 30 https://blog.golang.org/gopher/header.jpg | ||
``` | ||
|
||
## ディレクトリ構造 | ||
|
||
``` | ||
. | ||
├── Makefile | ||
├── README.md | ||
├── go.mod | ||
├── go.sum | ||
├── main.go | ||
├── pdownload | ||
├── pdownload.go | ||
├── pdownload_test.go | ||
├── requests.go | ||
├── requests_test.go | ||
├── termination | ||
│ ├── termination.go | ||
│ └── termination_test.go | ||
├── testdata | ||
│ ├── header.jpg | ||
│ └── test_download | ||
│ └── header.jpg | ||
└── util.go | ||
``` | ||
|
||
## 参考 | ||
|
||
[Code-Hex/pget](https://github.com/Code-Hex/pget)と[gopherdojo/dojo3#50](https://github.com/gopherdojo/dojo3/pull/50)を参考にさせていただきました。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module github.com/misonog/gopherdojo-studyroom/kadai3-2/misonog | ||
|
||
go 1.16 | ||
|
||
require golang.org/x/sync v0.0.0-20210220032951-036812b2e83c |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ= | ||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package main | ||
|
||
import "log" | ||
|
||
func main() { | ||
cli := New() | ||
if err := cli.Run(); err != nil { | ||
log.Fatal(err) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"flag" | ||
"fmt" | ||
"net/url" | ||
"os" | ||
"runtime" | ||
|
||
"github.com/misonog/gopherdojo-studyroom/kadai3-2/misonog/termination" | ||
) | ||
|
||
const TIMEOUT = 10 | ||
|
||
// Pdownload structs | ||
type Pdownload struct { | ||
Utils | ||
misonog marked this conversation as resolved.
Show resolved
Hide resolved
|
||
URL string | ||
TargetDir string | ||
Procs int | ||
timeout int | ||
misonog marked this conversation as resolved.
Show resolved
Hide resolved
|
||
useragent string | ||
referer string | ||
} | ||
|
||
func New() *Pdownload { | ||
return &Pdownload{ | ||
Utils: &Data{}, | ||
misonog marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Procs: runtime.NumCPU(), // default | ||
timeout: TIMEOUT, | ||
} | ||
} | ||
|
||
func (pdownload *Pdownload) Run() error { | ||
ctx := context.Background() | ||
misonog marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
ctx, clean := termination.Listen(ctx, os.Stdout) | ||
defer clean() | ||
|
||
if err := pdownload.Ready(); err != nil { | ||
return err | ||
} | ||
|
||
dir, err := os.MkdirTemp(pdownload.TargetDir, "") | ||
if err != nil { | ||
return err | ||
} | ||
clean = func() { os.RemoveAll(dir) } | ||
defer clean() | ||
termination.CleanFunc(clean) | ||
|
||
ctx, err = pdownload.Check(ctx, dir) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := pdownload.Download(ctx); err != nil { | ||
return err | ||
} | ||
|
||
if err := pdownload.Utils.MergeFiles(pdownload.Procs); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (pdownload *Pdownload) Ready() error { | ||
var targetDir string | ||
var timeout int | ||
|
||
pwd, err := os.Getwd() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
flag.StringVar(&targetDir, "d", pwd, "path to the directory to save the downloaded file, filename will be taken from url") | ||
misonog marked this conversation as resolved.
Show resolved
Hide resolved
|
||
flag.IntVar(&timeout, "t", TIMEOUT, "timeout of checking request in seconds") | ||
flag.Parse() | ||
|
||
if err := pdownload.parseURL(flag.Args()); err != nil { | ||
misonog marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return err | ||
} | ||
|
||
if _, err := os.Stat(targetDir); os.IsNotExist(err) { | ||
return fmt.Errorf("target directory is not exist: %v", err) | ||
misonog marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
pdownload.TargetDir = targetDir | ||
pdownload.timeout = timeout | ||
|
||
return nil | ||
} | ||
|
||
func (pdownload *Pdownload) parseURL(args []string) error { | ||
if len(args) > 1 { | ||
return errors.New("URL must be a single") | ||
} | ||
if len(args) < 1 { | ||
return errors.New("urls not found in the arguments passed") | ||
} | ||
|
||
for _, arg := range args { | ||
_, err := url.ParseRequestURI(arg) | ||
if err != nil { | ||
return err | ||
} | ||
pdownload.URL = arg | ||
} | ||
|
||
return nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
) | ||
|
||
// requests_test.goで作成しているテストサーバを利用してテストを行う | ||
func TestRun(t *testing.T) { | ||
url := ts.URL | ||
|
||
os.Args = []string{ | ||
"pdownload", | ||
"-d", | ||
"testdata/test_download", | ||
"-t", | ||
"30", | ||
fmt.Sprintf("%s/%s", url, "header.jpg"), | ||
} | ||
|
||
p := New() | ||
if err := p.Run(); err != nil { | ||
t.Errorf("failed to Run: %s", err) | ||
} | ||
|
||
if err := os.Remove(p.FullFileName()); err != nil { | ||
t.Errorf("failed to remove of result file: %s", err) | ||
} | ||
} | ||
|
||
func TestParseURL(t *testing.T) { | ||
|
||
cases := []struct { | ||
name string | ||
input []string | ||
expected string | ||
}{ | ||
{name: "an URL", input: []string{"https://www.google.com/"}, expected: "https://www.google.com/"}, | ||
{name: "URLs", input: []string{"https://www.google.com/", "https://golang.org/"}, expected: ""}, | ||
{name: "invalid URL", input: []string{"invalid_url"}, expected: ""}, | ||
} | ||
|
||
for _, c := range cases { | ||
c := c | ||
p := New() | ||
t.Run(c.name, func(t *testing.T) { | ||
_ = p.parseURL(c.input) | ||
if actual := p.URL; c.expected != actual { | ||
t.Errorf("want p.URL = %v, got %v", | ||
c.expected, actual) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.