-
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.
- Loading branch information
0 parents
commit 465cca1
Showing
1 changed file
with
50 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
## shell-extender | ||
### 项目简介:对os/exec包进行扩展,增加其功能。 | ||
- ExecShellCommand 执行命令 | ||
- ExecShellCommandWithResult 执行命令并输出所有结果 | ||
- ExecShellCommandWithTimeout 执行命令并超时时间 | ||
- ExecShellCommandWithChan 执行命令并使用管道输出 | ||
|
||
### 示例1 | ||
**使用方法** | ||
```go | ||
func main() { | ||
fmt.Println("==============ExecShellCommand=================") | ||
out, i, err := command.ExecShellCommand("kubectl get node") | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
|
||
fmt.Printf("i: %v, out: %s", i, out) | ||
fmt.Println("===============================") | ||
|
||
fmt.Println("==============ExecShellCommandWithResult=================") | ||
stdout, stderr, code, err := command.ExecShellCommandWithResult("kubectl get node") | ||
fmt.Printf("stdout: %v, stderr: %s, code: %v, err: %v\n", stdout, stderr, code, err) | ||
fmt.Println("===============================") | ||
|
||
fmt.Println("==============ExecShellCommandWithTimeout=================") | ||
stdout, stderr, code, err = command.ExecShellCommandWithTimeout("sleep 10; kubectl get node", 3) | ||
fmt.Printf("stdout: %v, stderr: %s, code: %v, err: %v\n", stdout, stderr, code, err) | ||
fmt.Println("===============================") | ||
|
||
fmt.Println("==============ExecShellCommandWithChan=================") | ||
outputC := make(chan string, 10) | ||
|
||
go func() { | ||
for i := range outputC { | ||
fmt.Println("output line: ", i) | ||
} | ||
}() | ||
|
||
err = command.ExecShellCommandWithChan("kubectl get node", outputC) | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
fmt.Println("===============================") | ||
} | ||
|
||
|
||
``` |