Conditions | 8 |
Total Lines | 60 |
Code Lines | 40 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | package main |
||
17 | func main() { |
||
18 | // var err error |
||
19 | var proxy, filePath, bwLimit, resumeTask string |
||
20 | |||
21 | conn := flag.Int("n", runtime.NumCPU(), "number of connections") |
||
22 | skiptls := flag.Bool("skip-tls", false, "skip certificate verification for https") |
||
23 | flag.StringVar(&proxy, "proxy", "", "proxy for downloading, e.g. -proxy '127.0.0.1:12345' for socks5 or -proxy 'http://proxy.com:8080' for http proxy") |
||
24 | flag.StringVar(&filePath, "file", "", "path to a file that contains one URL per line") |
||
25 | flag.StringVar(&bwLimit, "rate", "", "bandwidth limit during download, e.g. -rate 10kB or -rate 10MiB") |
||
26 | flag.StringVar(&resumeTask, "resume", "", "resume download task with given task name (or URL)") |
||
27 | |||
28 | flag.Parse() |
||
29 | args := flag.Args() |
||
30 | |||
31 | // If the resume flag is provided, use that path (ignoring other arguments) |
||
32 | if resumeTask != "" { |
||
33 | state, err := Resume(resumeTask) |
||
34 | FatalCheck(err) |
||
35 | Execute(state.URL, state, *conn, *skiptls, proxy, bwLimit) |
||
36 | return |
||
37 | } |
||
38 | |||
39 | // If no resume flag, then check for positional URL or file input |
||
40 | if len(args) < 1 { |
||
41 | if len(filePath) < 1 { |
||
42 | Errorln("A URL or input file with URLs is required") |
||
43 | usage() |
||
44 | os.Exit(1) |
||
45 | } |
||
46 | // Create a serial group for processing multiple URLs in a file. |
||
47 | g1 := task.NewSerialGroup() |
||
48 | file, err := os.Open(filePath) |
||
49 | if err != nil { |
||
50 | FatalCheck(err) |
||
51 | } |
||
52 | defer file.Close() |
||
53 | |||
54 | reader := bufio.NewReader(file) |
||
55 | for { |
||
56 | line, _, err := reader.ReadLine() |
||
57 | if err == io.EOF { |
||
58 | break |
||
59 | } |
||
60 | url := string(line) |
||
61 | // Add the download task for each URL |
||
62 | g1.AddChild(downloadTask(url, nil, *conn, *skiptls, proxy, bwLimit)) |
||
63 | } |
||
64 | g1.Run(nil) |
||
65 | return |
||
66 | } |
||
67 | |||
68 | // Otherwise, if a URL is provided as positional argument, treat it as a new download. |
||
69 | downloadURL := args[0] |
||
70 | // Check if a folder already exists for the task and remove if necessary. |
||
71 | if ExistDir(FolderOf(downloadURL)) { |
||
72 | Warnf("Downloading task already exists, remove it first \n") |
||
73 | err := os.RemoveAll(FolderOf(downloadURL)) |
||
74 | FatalCheck(err) |
||
75 | } |
||
76 | Execute(downloadURL, nil, *conn, *skiptls, proxy, bwLimit) |
||
77 | } |
||
171 |