From 247cf8994796550656994cdbe1fbde0bcf93de41 Mon Sep 17 00:00:00 2001 From: Nothin <55579792+Nothing-no@users.noreply.github.com> Date: Mon, 17 Apr 2023 14:06:49 +0800 Subject: [PATCH 1/2] [FEATURE] system add option (#87) --- system/os.go | 12 +++++++++++- system/os_linux.go | 18 ++++++++++++++++++ system/os_windows.go | 18 ++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 system/os_linux.go create mode 100644 system/os_windows.go diff --git a/system/os.go b/system/os.go index 55ac2e9..2924b33 100644 --- a/system/os.go +++ b/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 diff --git a/system/os_linux.go b/system/os_linux.go new file mode 100644 index 0000000..ce6bfbe --- /dev/null +++ b/system/os_linux.go @@ -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 + } + } +} diff --git a/system/os_windows.go b/system/os_windows.go new file mode 100644 index 0000000..db38238 --- /dev/null +++ b/system/os_windows.go @@ -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 + } + } +} From a12a691ee617b31121bd4e95b2d46356380dfaff Mon Sep 17 00:00:00 2001 From: dudaodong Date: Mon, 17 Apr 2023 15:28:01 +0800 Subject: [PATCH 2/2] release v2.1.19 --- README.md | 2 +- README_zh-CN.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3d427aa..7065239 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@
![Go version](https://img.shields.io/badge/go-%3E%3Dv1.18-9cf) -[![Release](https://img.shields.io/badge/release-2.1.18-green.svg)](https://github.com/duke-git/lancet/releases) +[![Release](https://img.shields.io/badge/release-2.1.19-green.svg)](https://github.com/duke-git/lancet/releases) [![GoDoc](https://godoc.org/github.com/duke-git/lancet/v2?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet/v2) [![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet/v2)](https://goreportcard.com/report/github.com/duke-git/lancet/v2) [![test](https://github.com/duke-git/lancet/actions/workflows/codecov.yml/badge.svg?branch=main&event=push)](https://github.com/duke-git/lancet/actions/workflows/codecov.yml) diff --git a/README_zh-CN.md b/README_zh-CN.md index 9b0cda2..e2c9bae 100644 --- a/README_zh-CN.md +++ b/README_zh-CN.md @@ -4,7 +4,7 @@
![Go version](https://img.shields.io/badge/go-%3E%3Dv1.18-9cf) -[![Release](https://img.shields.io/badge/release-2.1.18-green.svg)](https://github.com/duke-git/lancet/releases) +[![Release](https://img.shields.io/badge/release-2.1.19-green.svg)](https://github.com/duke-git/lancet/releases) [![GoDoc](https://godoc.org/github.com/duke-git/lancet/v2?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet/v2) [![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet/v2)](https://goreportcard.com/report/github.com/duke-git/lancet/v2) [![test](https://github.com/duke-git/lancet/actions/workflows/codecov.yml/badge.svg?branch=main&event=push)](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)