forked from arriven/db1000n
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DB1000N | Add support for the restart option after the auto-update fo…
…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
1 parent
c7f620b
commit 936d6dc
Showing
6 changed files
with
75 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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...) | ||
} |
3 changes: 0 additions & 3 deletions
3
src/utils/ota/restart_linux_test.go → src/utils/ota/shared_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,3 @@ | ||
//go:build linux | ||
// +build linux | ||
|
||
package ota | ||
|
||
import ( | ||
|