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