File tree Expand file tree Collapse file tree 5 files changed +164
-0
lines changed
Expand file tree Collapse file tree 5 files changed +164
-0
lines changed Original file line number Diff line number Diff line change 1010| withcontext.go| cmdexec\_ withcontext| コマンドを context.Context 付きで実行するサンプルです|
1111| pipe.go| cmdexec\_ pipe| (* Cmd).StdinPipe,StdoutPipe,StderrPipeのサンプルです|
1212| multi\_ command\_ with\_ pipe.go| cmdexec\_ multi\_ command\_ with\_ pipe| 複数の (* exec.Cmd) をパイプストリームで繋いで実行するサンプルです|
13+ | withenv.go| cmdexec\_ env| * exec.Cmd 実行時に追加の環境変数を指定するサンプルです|
14+ | withdir.go| cmdexec\_ dir| * exec.Cmd 実行時にワーキングディレクトリを指定するサンプルです|
15+ | withslice.go| cmdexec\_ slice| * exec.Cmd 実行時にスライスの値をコマンドの引数で指定するサンプルです|
Original file line number Diff line number Diff line change @@ -19,4 +19,7 @@ func (r *register) Regist(m mapping.ExampleMapping) {
1919 m ["cmdexec_withcontext" ] = WithContext
2020 m ["cmdexec_pipe" ] = Pipe
2121 m ["cmdexec_multi_command_with_pipe" ] = MultiCommandWithPipe
22+ m ["cmdexec_env" ] = WithEnv
23+ m ["cmdexec_dir" ] = WithDir
24+ m ["cmdexec_slice" ] = WithSlice
2225}
Original file line number Diff line number Diff line change 1+ package cmdexec
2+
3+ import (
4+ "errors"
5+ "os"
6+ "os/exec"
7+ "runtime"
8+
9+ "github.com/devlights/gomy/output"
10+ )
11+
12+ // WithDir -- *exec.Cmd 実行時にワーキングディレクトリを指定するサンプルです.
13+ //
14+ // REFERENCES
15+ // - https://dev.to/tobychui/quick-notes-for-go-os-exec-3ejg
16+ func WithDir () error {
17+ if runtime .GOOS == "windows" {
18+ return errors .New ("this example cannot run on Windows, sorry" )
19+ }
20+
21+ const (
22+ Shell = "/bin/bash"
23+ )
24+
25+ var (
26+ cmd * exec.Cmd
27+ out []byte
28+ err error
29+ )
30+
31+ output .Stdoutl ("[cwd]" , func () string { c , _ := os .Getwd (); return c }())
32+
33+ //
34+ // プロセス実行時のワーキングディレクトリの指定なし
35+ //
36+ cmd = exec .Command (Shell , "-c" , "pwd" )
37+
38+ out , err = cmd .Output ()
39+ if err != nil {
40+ return err
41+ }
42+
43+ output .Stdoutf ("[no dir]" , "%s" , out )
44+
45+ //
46+ // プロセス実行時のワーキングディレクトリの指定あり
47+ //
48+ cmd = exec .Command (Shell , "-c" , "pwd" )
49+ cmd .Dir = "/tmp"
50+
51+ out , err = cmd .Output ()
52+ if err != nil {
53+ return err
54+ }
55+
56+ output .Stdoutf ("[with dir]" , "%s" , out )
57+
58+ return nil
59+ }
Original file line number Diff line number Diff line change 1+ package cmdexec
2+
3+ import (
4+ "errors"
5+ "os"
6+ "os/exec"
7+ "runtime"
8+
9+ "github.com/devlights/gomy/output"
10+ )
11+
12+ // WithEnv -- *exec.Cmd 実行時に追加の環境変数を指定するサンプルです.
13+ //
14+ // REFERENCES
15+ // - https://dev.to/tobychui/quick-notes-for-go-os-exec-3ejg
16+ func WithEnv () error {
17+ if runtime .GOOS == "windows" {
18+ return errors .New ("this example cannot run on Windows, sorry" )
19+ }
20+
21+ const (
22+ Shell = "/bin/bash"
23+ )
24+
25+ var (
26+ cmd * exec.Cmd
27+ out []byte
28+ err error
29+ )
30+
31+ //
32+ // 環境変数の追加なし
33+ //
34+ cmd = exec .Command (Shell , "-c" , "env | grep LANG" )
35+
36+ out , err = cmd .Output ()
37+ if err != nil {
38+ return err
39+ }
40+
41+ output .Stdoutf ("[no append]" , "\n %s\n " , out )
42+ output .StdoutHr ()
43+
44+ //
45+ // 環境変数の追加あり
46+ //
47+ cmd = exec .Command (Shell , "-c" , "env | grep LANG" )
48+ cmd .Env = append (os .Environ (), "LANG2=Japanese" )
49+
50+ out , err = cmd .Output ()
51+ if err != nil {
52+ return err
53+ }
54+
55+ output .Stdoutf ("[append ]" , "\n %s\n " , out )
56+ output .StdoutHr ()
57+
58+ return nil
59+ }
Original file line number Diff line number Diff line change 1+ package cmdexec
2+
3+ import (
4+ "fmt"
5+ "os/exec"
6+
7+ "github.com/devlights/gomy/output"
8+ )
9+
10+ // WithSlice -- *exec.Cmd 実行時にスライスの値をコマンドの引数で指定するサンプルです.
11+ //
12+ // REFERENCES
13+ // - https://dev.to/tobychui/quick-notes-for-go-os-exec-3ejg
14+ func WithSlice () error {
15+ var (
16+ cmd * exec.Cmd
17+ out []byte
18+ err error
19+ )
20+
21+ var (
22+ p = []string {
23+ "hello" ,
24+ "world" ,
25+ "こんにちわ" ,
26+ "世界" ,
27+ }
28+ )
29+
30+ cmd = exec .Command ("echo" , p ... )
31+
32+ out , err = cmd .CombinedOutput ()
33+ if err != nil {
34+ return fmt .Errorf ("%w (%s)" , err , out )
35+ }
36+
37+ output .Stdoutf ("[cmd]" , "%s" , out )
38+
39+ return nil
40+ }
You can’t perform that action at this time.
0 commit comments