Skip to content

Commit

Permalink
DB1000N | Add support for the restart option after the auto-update fo…
Browse files Browse the repository at this point in the history
…r the Windows OS (arriven#391)

* DB1000N | Add support for the restart option after the auto-update for the Windows OS

* DB1000N | Change the Run method to Start to restart the process on the Windows OS
  • Loading branch information
sivillakonski authored Mar 23, 2022
1 parent c7f620b commit 936d6dc
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 25 deletions.
5 changes: 4 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@ func main() {

logger, err := newZapLogger(*debug)
if err != nil {
log.Fatal("failed to create zap logger", err)
log.Printf("failed to initialize Zap logger: %s", err)
os.Exit(1)

return
}

configPathsArray := strings.Split(*configPaths, ",")
Expand Down
17 changes: 0 additions & 17 deletions src/utils/ota/ota.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,3 @@ func MockAutoUpdate(shouldUpdateBeFound bool) (updateFound bool, newVersion, cha

return
}

func appendArgIfNotPresent(osArgs, extraArgs []string) []string {
osArgsMap := make(map[string]interface{}, len(osArgs))
for _, osArg := range osArgs {
osArgsMap[osArg] = nil
}

acceptedExtraArgs := make([]string, 0)

for _, extraArg := range extraArgs {
if _, isAlreadyOSArg := osArgsMap[extraArg]; !isAlreadyOSArg {
acceptedExtraArgs = append(acceptedExtraArgs, extraArg)
}
}

return append(osArgs, acceptedExtraArgs...)
}
11 changes: 7 additions & 4 deletions src/utils/ota/restart.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
//go:build windows || netbsd || solaris || aix || dragonfly || freebsd || (illumos && !linux && !darwin)
// +build windows netbsd solaris aix dragonfly freebsd illumos,!linux,!darwin
//go:build !linux && !darwin && !windows
// +build !linux,!darwin,!windows

package ota

import "errors"
import (
"fmt"
"runtime"
)

func Restart(extraArgs ...string) error {
return errors.New("restart on the Windows system is not available")
return fmt.Errorf("restart on the %s system is not available", runtime.GOOS)
}
46 changes: 46 additions & 0 deletions src/utils/ota/restart_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//go:build windows
// +build windows

package ota

import (
"fmt"
"os"
"os/exec"
"strings"
"syscall"
)

func Restart(extraArgs ...string) error {
execPath, err := os.Executable()
if err != nil {
return fmt.Errorf("failed to locate the executable file: %w", err)
}

// A specific way the `cmd.exe` processes the `start` command: it takes the
// first quoted substring as a process name! (https://superuser.com/a/1656458)
cmdLine := fmt.Sprintf(`/C start "process" "%s"`, execPath)
args := []string{}

if len(extraArgs) != 0 {
args = appendArgIfNotPresent(os.Args[1:], extraArgs)
} else {
args = os.Args[1:]
}

if len(args) > 0 {
cmdLine += " " + strings.Join(args, " ")
}

cmd := exec.Command("cmd.exe")
cmd.SysProcAttr = &syscall.SysProcAttr{CmdLine: cmdLine}

if err := cmd.Start(); err != nil {
return fmt.Errorf("failed to start the command: %w", err)
}

os.Exit(0)

return nil
}

18 changes: 18 additions & 0 deletions src/utils/ota/shared.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ota

func appendArgIfNotPresent(osArgs, extraArgs []string) []string {
osArgsMap := make(map[string]interface{}, len(osArgs))
for _, osArg := range osArgs {
osArgsMap[osArg] = nil
}

acceptedExtraArgs := make([]string, 0)

for _, extraArg := range extraArgs {
if _, isAlreadyOSArg := osArgsMap[extraArg]; !isAlreadyOSArg {
acceptedExtraArgs = append(acceptedExtraArgs, extraArg)
}
}

return append(osArgs, acceptedExtraArgs...)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
//go:build linux
// +build linux

package ota

import (
Expand Down

0 comments on commit 936d6dc

Please sign in to comment.