Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kadai3-1/manhdaovan/vendor
cover.out
kadai3-2/manhdaovan/vendor
vendor
kadai3-2/manhdaovan/tmp
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module github.com/gopherdojo/dojo5

go 1.12

require (
github.com/pkg/errors v0.8.0
golang.org/x/net v0.0.0-20190619014844-b5b0513f8c1b
golang.org/x/sync v0.0.0-20190423024810-112230192c58
)
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190619014844-b5b0513f8c1b h1:lkjdUzSyJ5P1+eal9fxXX9Xg2BTfswsonKUse48C0uE=
golang.org/x/net v0.0.0-20190619014844-b5b0513f8c1b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEhagSSnXWGV915qUMm9mrU=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
13 changes: 13 additions & 0 deletions kadai3-2/manhdaovan/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
COVERAGE_VIEW_MODE=html# or "func"
GOMOD=on#or "off" or "auto"

install:
- GO111MODULE=${GOMOD} go mod vendor
- GO111MODULE=${GOMOD} go mod tidy
build: ./cmd/main.go
- GO111MODULE=${GOMOD} go build -a -o ./bin/mget ./cmd/main.go
test: ./pkg/mget/mget_test.go
- GO111MODULE=${GOMOD} go test -a -race ./pkg/...
coverage: test
- GO111MODULE=${GOMOD} go test -coverprofile=cover.out ./...
- GO111MODULE=${GOMOD} go tool cover -${COVERAGE_VIEW_MODE} cover.out
Empty file added kadai3-2/manhdaovan/README.md
Empty file.
Binary file added kadai3-2/manhdaovan/bin/mget
Binary file not shown.
84 changes: 84 additions & 0 deletions kadai3-2/manhdaovan/cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package main

import (
"context"
"flag"
"fmt"
"net/http"
"os"
"runtime"

"github.com/gopherdojo/dojo5/kadai3-2/manhdaovan/pkg/mget"
)

const (
exitErr = 1
)

func main() {
ca := parseArgs()
if err := ca.validate(); err != nil {
fmt.Fprintln(os.Stderr, "given args error: ", err)
printHelp()
os.Exit(exitErr)
}
if _, err := os.Stat(ca.outPath); err != nil {
fmt.Fprintf(os.Stderr, "invalid output path: %s, err: %+v\n", ca.outPath, err)
printHelp()
os.Exit(exitErr)
}

downloader := mget.NewMGet(http.DefaultClient, ca.numWorkers, mget.DefaultExitSigs, "", "")
outputPath, err := downloader.Download(context.Background(), ca.outPath, ca.url)
if err != nil {
fmt.Fprintf(os.Stderr, "download error: %+v\n", err)
printHelp()
os.Exit(exitErr)
}

fmt.Println("Download completed. Output: ", outputPath)
}

type cliArgs struct {
numWorkers uint
outPath string
url string
}

func (ca *cliArgs) validate() error {
if ca.numWorkers == 0 {
return fmt.Errorf("number worker must be greater than 0")
}
if ca.url == "" {
return fmt.Errorf("no download url given")
}

if ca.outPath == "" {
ca.outPath = "./out/"
}

return nil
}

func parseArgs() *cliArgs {
var ca cliArgs
flag.UintVar(&ca.numWorkers, "w", uint(runtime.NumCPU()), "number of workers")
flag.StringVar(&ca.outPath, "o", "", "output file path")
flag.Parse()
ca.url = flag.Arg(0) // last arg is download url

return &ca
}

func printHelp() {
helpStr := `
mget -- a simple concurrency download tool
usage: $./bin/mget [-w] [-o] download-url
options:
-w unit
Number of workers that download in concurrent
-o string
Output of downloaded file. It can be a directory or a file path.
`
fmt.Println(helpStr)
}
2 changes: 2 additions & 0 deletions kadai3-2/manhdaovan/out/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*/*
*
34 changes: 34 additions & 0 deletions kadai3-2/manhdaovan/pkg/mget/chunk.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package mget

import (
"fmt"
"path/filepath"
)

type chunkInfo struct {
idx int
url string
size uint64
rangeLow uint64
rangeHigh uint64
}

func newChunkInfo(idx int, url string, fileSize, chunkSize uint64) *chunkInfo {
offset := uint64(idx) * chunkSize
if offset+chunkSize > fileSize { // last chunk
chunkSize = fileSize - offset
}

return &chunkInfo{
idx: idx,
url: url,
size: chunkSize,
rangeLow: offset,
rangeHigh: offset + chunkSize - 1,
}
}

func chunkPath(dstDir, dstFile string, idx int) string {
chunkName := fmt.Sprintf("%s-%d.chunk", dstFile, idx)
return filepath.Join(dstDir, chunkName)
}
22 changes: 22 additions & 0 deletions kadai3-2/manhdaovan/pkg/mget/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package mget

import (
"path/filepath"
"strings"
)

// parseDirAndFileName returns dir path and file name from given path
func parseDirAndFileName(path string) (dir, file string) {
lastSlashIdx := strings.LastIndex(path, "/")
dir = path[:lastSlashIdx+1]
if len(dir) == len(path) { // path is a dir
return dir, ""
}

return dir, path[(len(dir) + 1):]
}

// parseFileName returns file name from given url
func parseFileName(url string) string {
return filepath.Base(url)
}
Loading