@@ -228,55 +228,59 @@ func finishUpdate(
228228 cleanup (ctx )
229229 cleanupAlways ()
230230
231- var err error
232231 if runtime .GOOS == "windows" {
233- if runningAsService {
234- // NOTE: We can't restart the service via "kardianos/service"
235- // package, because it kills the process first we can't start a new
236- // instance, because Windows doesn't allow it.
237- //
238- // TODO(a.garipov): Recheck the claim above.
239- var cmd executil.Command
240- cmd , err = cmdCons .New (ctx , & executil.CommandConfig {
241- Path : "cmd" ,
242- Args : []string {"/c" , "net stop AdGuardHome & net start AdGuardHome" },
243- })
244- if err != nil {
245- panic (fmt .Errorf ("constructing cmd: %w" , err ))
246- }
247-
248- err = cmd .Start (ctx )
249- if err != nil {
250- panic (fmt .Errorf ("restarting service: %w" , err ))
251- }
252-
253- os .Exit (osutil .ExitCodeSuccess )
254- }
232+ finalizeWindowsUpdate (ctx , l , cmdCons , execPath , runningAsService )
255233
256- l .InfoContext (ctx , "restarting" , "exec_path" , execPath , "args" , os .Args [1 :])
234+ os .Exit (osutil .ExitCodeSuccess )
235+ }
236+
237+ var err error
238+ l .InfoContext (ctx , "restarting" , "exec_path" , execPath , "args" , os .Args [1 :])
239+ err = syscall .Exec (execPath , os .Args , os .Environ ())
240+ if err != nil {
241+ panic (fmt .Errorf ("restarting: %w" , err ))
242+ }
243+ }
257244
258- var cmd executil.Command
259- cmd , err = cmdCons .New (ctx , & executil.CommandConfig {
245+ // finalizeWindowsUpdate completes an update procedure on windows. l and
246+ // cmdCons must not be nil.
247+ func finalizeWindowsUpdate (ctx context.Context ,
248+ l * slog.Logger ,
249+ cmdCons executil.CommandConstructor ,
250+ execPath string ,
251+ runningAsService bool ,
252+ ) {
253+ var commandConf * executil.CommandConfig
254+
255+ if runningAsService {
256+ // NOTE: We can't restart the service via "kardianos/service" package,
257+ // because it kills the process first we can't start a new instance,
258+ // because Windows doesn't allow it.
259+ //
260+ // TODO(a.garipov): Recheck the claim above.
261+ commandConf = & executil.CommandConfig {
262+ Path : "cmd" ,
263+ Args : []string {"/c" , "net stop AdGuardHome & net start AdGuardHome" },
264+ }
265+ } else {
266+ commandConf = & executil.CommandConfig {
260267 Path : execPath ,
261268 Args : os .Args [1 :],
262269 Stdin : os .Stdin ,
263270 Stdout : os .Stdout ,
264271 Stderr : os .Stderr ,
265- })
266- if err != nil {
267- panic (fmt .Errorf ("constructing cmd: %w" , err ))
268272 }
273+ }
269274
270- err = cmd .Start (ctx )
271- if err != nil {
272- panic (fmt .Errorf ("restarting: %w" , err ))
273- }
275+ l .InfoContext (ctx , "restarting" , "exec_path" , execPath , "args" , os .Args [1 :])
274276
275- os .Exit (osutil .ExitCodeSuccess )
277+ var cmd executil.Command
278+ cmd , err := cmdCons .New (ctx , commandConf )
279+ if err != nil {
280+ panic (fmt .Errorf ("constructing cmd: %w" , err ))
276281 }
277282
278- l .InfoContext (ctx , "restarting" , "exec_path" , execPath , "args" , os .Args [1 :])
279- err = syscall .Exec (execPath , os .Args , os .Environ ())
283+ err = cmd .Start (ctx )
280284 if err != nil {
281285 panic (fmt .Errorf ("restarting: %w" , err ))
282286 }
0 commit comments