| Conditions | 18 |
| Total Lines | 98 |
| Code Lines | 63 |
| 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.initApp 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 |
||
| 104 | func initApp() { |
||
| 105 | fmt.Printf("planfix-toggl %s\n", version) |
||
| 106 | cfg := config.GetConfig() |
||
| 107 | |||
| 108 | parseFlags(&cfg) |
||
| 109 | |||
| 110 | logger := getLogger(cfg) |
||
| 111 | |||
| 112 | errors, isValid := cfg.Validate() |
||
| 113 | if !isValid { |
||
| 114 | for _, e := range errors { |
||
| 115 | log.Println(e) |
||
| 116 | } |
||
| 117 | } |
||
| 118 | |||
| 119 | if cfg.NoConsole { |
||
| 120 | util.HideConsole() |
||
| 121 | } |
||
| 122 | |||
| 123 | if cfg.CheckNewVersion { |
||
| 124 | checkNewVersion() |
||
| 125 | } |
||
| 126 | |||
| 127 | togglClient := client.TogglClient{ |
||
| 128 | Config: &cfg, |
||
| 129 | Logger: logger, |
||
| 130 | SentLog: make(map[string]int), |
||
| 131 | Opts: map[string]string{"LastSent": ""}, |
||
| 132 | } |
||
| 133 | togglClient.ReloadConfig() |
||
| 134 | |||
| 135 | // get planfix and toggl user IDs, for early API check |
||
| 136 | err := connectServices(&cfg, logger, &togglClient) |
||
| 137 | if err != nil { |
||
| 138 | isValid = false |
||
| 139 | logger.Printf("[ERROR] %s", err.Error()) |
||
| 140 | util.Notify(err.Error()) |
||
| 141 | } |
||
| 142 | |||
| 143 | trayMenu["web"].Enable() |
||
| 144 | trayMenu["log"].Enable() |
||
| 145 | |||
| 146 | if isValid { |
||
| 147 | trayMenu["send"].Enable() |
||
| 148 | togglClient.Run() |
||
| 149 | } else { |
||
| 150 | util.OpenBrowser(fmt.Sprintf("https://localhost:%d/#settings", cfg.PortSSL)) |
||
| 151 | } |
||
| 152 | |||
| 153 | // update last sent on menuitem |
||
| 154 | go func() { |
||
| 155 | for { |
||
| 156 | if togglClient.Opts["LastSent"] != "" { |
||
| 157 | t := togglClient.Opts["LastSent"] |
||
| 158 | trayMenu["send"].SetTitle(fmt.Sprintf("Sync (last at %s)", t)) |
||
| 159 | } |
||
| 160 | time.Sleep(time.Duration(60 * time.Second)) |
||
| 161 | } |
||
| 162 | }() |
||
| 163 | |||
| 164 | go func() { |
||
| 165 | // tray menu actions |
||
| 166 | for { |
||
| 167 | select { |
||
| 168 | case <-trayMenu["send"].ClickedCh: |
||
| 169 | err := togglClient.SendToPlanfix() |
||
| 170 | t := togglClient.Opts["LastSent"] |
||
| 171 | trayMenu["send"].SetTitle(fmt.Sprintf("Sync (last at %s)", t)) |
||
| 172 | if err != nil { |
||
| 173 | logger.Println(err) |
||
| 174 | } |
||
| 175 | |||
| 176 | case <-trayMenu["web"].ClickedCh: |
||
| 177 | cfg := config.GetConfig() |
||
| 178 | util.OpenBrowser(fmt.Sprintf("https://localhost:%d", cfg.PortSSL)) |
||
| 179 | |||
| 180 | case <-trayMenu["current"].ClickedCh: |
||
| 181 | cfg := config.GetConfig() |
||
| 182 | util.OpenBrowser(fmt.Sprintf("https://localhost:%d/#current-go", cfg.PortSSL)) |
||
| 183 | |||
| 184 | case <-trayMenu["log"].ClickedCh: |
||
| 185 | cfg := config.GetConfig() |
||
| 186 | systray.ShowAppWindow(fmt.Sprintf("https://localhost:%d/log", cfg.PortSSL)) |
||
| 187 | |||
| 188 | case <-trayMenu["quit"].ClickedCh: |
||
| 189 | onExit() |
||
| 190 | } |
||
| 191 | } |
||
| 192 | }() |
||
| 193 | |||
| 194 | // start API server |
||
| 195 | server := rest.Server{ |
||
| 196 | Version: version, |
||
| 197 | TogglClient: &togglClient, |
||
| 198 | Config: &cfg, |
||
| 199 | Logger: logger, |
||
| 200 | } |
||
| 201 | server.Run(cfg.PortSSL) |
||
| 202 | } |
||
| 271 |