mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-04 12:52:28 +08:00
[FEATURE] system add option (#87)
This commit is contained in:
12
system/os.go
12
system/os.go
@@ -15,6 +15,10 @@ import (
|
||||
"golang.org/x/text/encoding/simplifiedchinese"
|
||||
)
|
||||
|
||||
type (
|
||||
Option func(*exec.Cmd)
|
||||
)
|
||||
|
||||
// IsWindows check if current os is windows.
|
||||
// Play: https://go.dev/play/p/XzJULbzmf9m
|
||||
func IsWindows() bool {
|
||||
@@ -66,7 +70,7 @@ func CompareOsEnv(key, comparedEnv string) bool {
|
||||
// in linux, use /bin/bash -c to execute command
|
||||
// in windows, use powershell.exe to execute command
|
||||
// Play: https://go.dev/play/p/n-2fLyZef-4
|
||||
func ExecCommand(command string) (stdout, stderr string, err error) {
|
||||
func ExecCommand(command string, opts ...Option) (stdout, stderr string, err error) {
|
||||
var out bytes.Buffer
|
||||
var errOut bytes.Buffer
|
||||
|
||||
@@ -74,6 +78,12 @@ func ExecCommand(command string) (stdout, stderr string, err error) {
|
||||
if IsWindows() {
|
||||
cmd = exec.Command("powershell.exe", command)
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
if opt != nil {
|
||||
opt(cmd)
|
||||
}
|
||||
}
|
||||
cmd.Stdout = &out
|
||||
cmd.Stderr = &errOut
|
||||
|
||||
|
||||
18
system/os_linux.go
Normal file
18
system/os_linux.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
func WithForeground() Option {
|
||||
return func(c *exec.Cmd) {
|
||||
if c.SysProcAttr == nil {
|
||||
c.SysProcAttr = &syscall.SysProcAttr{
|
||||
Foreground: true,
|
||||
}
|
||||
} else {
|
||||
c.SysProcAttr.Foreground = true
|
||||
}
|
||||
}
|
||||
}
|
||||
18
system/os_windows.go
Normal file
18
system/os_windows.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
func WithWinHide() Option {
|
||||
return func(c *exec.Cmd) {
|
||||
if c.SysProcAttr == nil {
|
||||
c.SysProcAttr = &syscall.SysProcAttr{
|
||||
HideWindow: true,
|
||||
}
|
||||
} else {
|
||||
c.SysProcAttr.HideWindow = true
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user