| Conditions | 12 |
| Total Lines | 66 |
| Code Lines | 54 |
| 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:
Complex classes like main.Execute often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | package main |
||
| 87 | func Execute(url string, state *State, conn int, skiptls bool, proxy string, bwLimit string) { |
||
| 88 | // Capture OS interrupt signals |
||
| 89 | signalChan := make(chan os.Signal, 1) |
||
| 90 | signal.Notify(signalChan, |
||
| 91 | syscall.SIGHUP, |
||
| 92 | syscall.SIGINT, |
||
| 93 | syscall.SIGTERM, |
||
| 94 | syscall.SIGQUIT) |
||
| 95 | |||
| 96 | var files = make([]string, 0) |
||
| 97 | var parts = make([]Part, 0) |
||
| 98 | var isInterrupted = false |
||
| 99 | |||
| 100 | doneChan := make(chan bool, conn) |
||
| 101 | fileChan := make(chan string, conn) |
||
| 102 | errorChan := make(chan error, 1) |
||
| 103 | stateChan := make(chan Part, 1) |
||
| 104 | interruptChan := make(chan bool, conn) |
||
| 105 | |||
| 106 | var downloader *HTTPDownloader |
||
| 107 | if state == nil { |
||
| 108 | downloader = NewHTTPDownloader(url, conn, skiptls, proxy, bwLimit) |
||
| 109 | } else { |
||
| 110 | downloader = &HTTPDownloader{ |
||
| 111 | url: state.URL, |
||
| 112 | file: TaskFromURL(state.URL), |
||
| 113 | par: int64(len(state.Parts)), |
||
| 114 | parts: state.Parts, |
||
| 115 | resumable: true, |
||
| 116 | } |
||
| 117 | } |
||
| 118 | go downloader.Do(doneChan, fileChan, errorChan, interruptChan, stateChan) |
||
| 119 | |||
| 120 | for { |
||
| 121 | select { |
||
| 122 | case <-signalChan: |
||
| 123 | // Signal all active download routines to interrupt. |
||
| 124 | isInterrupted = true |
||
| 125 | for range conn { |
||
| 126 | interruptChan <- true |
||
| 127 | } |
||
| 128 | case file := <-fileChan: |
||
| 129 | files = append(files, file) |
||
| 130 | case err := <-errorChan: |
||
| 131 | Errorf("%v", err) |
||
| 132 | panic(err) |
||
| 133 | case part := <-stateChan: |
||
| 134 | parts = append(parts, part) |
||
| 135 | case <-doneChan: |
||
| 136 | if isInterrupted { |
||
| 137 | if downloader.resumable { |
||
| 138 | Printf("Interrupted, saving state...\n") |
||
| 139 | s := &State{URL: url, Parts: parts} |
||
| 140 | if err := s.Save(); err != nil { |
||
| 141 | Errorf("%v\n", err) |
||
| 142 | } |
||
| 143 | } else { |
||
| 144 | Warnf("Interrupted, but the download is not resumable. Exiting silently.\n") |
||
| 145 | } |
||
| 146 | } else { |
||
| 147 | err := JoinFile(files, TaskFromURL(url)) |
||
| 148 | FatalCheck(err) |
||
| 149 | err = os.RemoveAll(FolderOf(url)) |
||
| 150 | FatalCheck(err) |
||
| 151 | } |
||
| 152 | return |
||
| 153 | } |
||
| 171 |