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 | |||
18 | func main() { |
||
19 | // var err error |
||
20 | var proxy, filePath, bwLimit, resumeTask string |
||
21 | |||
22 | conn := flag.Int("n", runtime.NumCPU(), "number of connections") |
||
23 | skiptls := flag.Bool("skip-tls", false, "skip certificate verification for https") |
||
24 | 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") |
||
25 | flag.StringVar(&filePath, "file", "", "path to a file that contains one URL per line") |
||
26 | flag.StringVar(&bwLimit, "rate", "", "bandwidth limit during download, e.g. -rate 10kB or -rate 10MiB") |
||
27 | flag.StringVar(&resumeTask, "resume", "", "resume download task with given task name (or URL)") |
||
28 | probe := flag.String("probe", "", "probe URL for range and content-length without downloading") |
||
29 | |||
30 | flag.Parse() |
||
31 | args := flag.Args() |
||
32 | |||
33 | // Probe diagnostics mode |
||
34 | if *probe != "" { |
||
35 | DebugProbe(*probe, *skiptls, proxy) |
||
36 | return |
||
37 | } |
||
38 | |||
39 | // If the resume flag is provided, use that path (ignoring other arguments) |
||
40 | if resumeTask != "" { |
||
41 | state, err := Resume(resumeTask) |
||
42 | FatalCheck(err) |
||
43 | Execute(state.URL, state, *conn, *skiptls, proxy, bwLimit) |
||
44 | return |
||
45 | } |
||
46 | |||
47 | // If no resume flag, then check for positional URL or file input |
||
48 | if len(args) < 1 { |
||
49 | if len(filePath) < 1 { |
||
50 | Errorln("A URL or input file with URLs is required") |
||
51 | usage() |
||
52 | os.Exit(1) |
||
53 | } |
||
54 | // Create a serial group for processing multiple URLs in a file. |
||
55 | g1 := task.NewSerialGroup() |
||
56 | file, err := os.Open(filePath) |
||
57 | if err != nil { |
||
58 | FatalCheck(err) |
||
59 | } |
||
60 | defer file.Close() |
||
61 | |||
62 | reader := bufio.NewReader(file) |
||
63 | for { |
||
64 | line, _, err := reader.ReadLine() |
||
65 | if err == io.EOF { |
||
66 | break |
||
67 | } |
||
68 | url := strings.TrimSpace(string(line)) |
||
69 | if url == "" || strings.HasPrefix(url, "#") { |
||
70 | continue |
||
71 | } |
||
72 | // Add the download task for each URL |
||
73 | g1.AddChild(downloadTask(url, nil, *conn, *skiptls, proxy, bwLimit)) |
||
74 | } |
||
75 | g1.Run(nil) |
||
76 | return |
||
77 | } |
||
193 |