Skip to content

Commit 7b81007

Browse files
committed
Add examples/singleapp/exit_code
1 parent 50dedf9 commit 7b81007

File tree

5 files changed

+128
-0
lines changed

5 files changed

+128
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
capp
2+
goapp
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# https://taskfile.dev
2+
3+
version: '3'
4+
5+
vars:
6+
C_APP: capp
7+
GO_APP: goapp
8+
9+
includes:
10+
C:
11+
taskfile: ./c/Taskfile.yml
12+
dir: ./c
13+
14+
tasks:
15+
default:
16+
cmds:
17+
- task: run
18+
build:
19+
cmds:
20+
- task: C:build
21+
- go build -o {{.GO_APP}}{{exeExt}} main.go
22+
run:
23+
deps: [ build ]
24+
cmds:
25+
- ./{{.GO_APP}}{{exeExt}} "./{{.C_APP}}{{exeExt}}"
26+
clean:
27+
cmds:
28+
- rm -f ./{{.C_APP}}{{exeExt}} ./{{.GO_APP}}{{exeExt}}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# https://taskfile.dev
2+
3+
version: '3'
4+
5+
vars:
6+
C_APP: capp
7+
8+
tasks:
9+
default:
10+
cmds:
11+
- task: run
12+
build:
13+
cmds:
14+
- gcc -o ../{{.C_APP}}{{exeExt}} main.c
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
int main(int argc, char *argv[])
5+
{
6+
int i = atoi(argv[argc-1]);
7+
if (i >= 5) {
8+
exit(99-i);
9+
}
10+
11+
if (i%2 == 0) {
12+
exit(EXIT_FAILURE);
13+
}
14+
15+
exit(EXIT_SUCCESS);
16+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"errors"
6+
"fmt"
7+
"log"
8+
"os"
9+
"os/exec"
10+
"strconv"
11+
"time"
12+
)
13+
14+
func main() {
15+
var (
16+
rootCtx = context.Background()
17+
mainCtx, mainCxl = context.WithTimeout(rootCtx, 1*time.Second)
18+
)
19+
defer mainCxl()
20+
21+
if err := run(mainCtx); err != nil {
22+
log.Fatal(err)
23+
}
24+
}
25+
26+
func run(ctx context.Context) error {
27+
for i := range 10 {
28+
var (
29+
ctx, cxl = context.WithTimeout(ctx, 100*time.Millisecond)
30+
program = os.Args[1]
31+
param = strconv.Itoa(i)
32+
cmd = exec.CommandContext(ctx, program, param)
33+
34+
err error
35+
exitCode int
36+
success = true
37+
)
38+
defer cxl()
39+
40+
if err = cmd.Run(); err != nil {
41+
//
42+
// 結果コードが 0、つまり、成功の場合は err は nil となる。
43+
//
44+
// 0以外の場合、errとして返ってくる。
45+
// この場合、err は *exec.ExitError となっているので
46+
// errors.As() で、変換できるか確認し、ExitCode() で取得する。
47+
//
48+
// Run() している最中に context がタイムアウトした場合
49+
// 結果コードは -1 となって返ってくる。
50+
//
51+
// Run() する前に context がタイムアウトした場合
52+
// context.DeadlineExceeded が返ってくる。
53+
//
54+
var exitErr *exec.ExitError
55+
if errors.As(err, &exitErr) {
56+
exitCode = exitErr.ExitCode()
57+
success = exitErr.Success()
58+
} else {
59+
// 別のエラーの場合 (context.DeadlineExceeded など)
60+
return err
61+
}
62+
}
63+
64+
fmt.Printf("実行引数: %d\t成功: %v\t結果コード: %d\n", i, success, exitCode)
65+
}
66+
67+
return nil
68+
}

0 commit comments

Comments
 (0)