-
Notifications
You must be signed in to change notification settings - Fork 38
/
log.go
80 lines (76 loc) · 1.41 KB
/
log.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"bufio"
"context"
"fmt"
"io"
"github.com/urfave/cli"
)
func logCommand(_ context.Context, hCtx *handlerContext) cli.Command {
return cli.Command{
Name: "log",
Usage: "show build log",
UsageText: "log [OPTIONS]",
Flags: []cli.Flag{
cli.IntFlag{
Name: "n",
Usage: "Print recent n lines",
Value: 10,
},
cli.BoolFlag{
Name: "all,a",
Usage: "show all lines",
},
cli.BoolFlag{
Name: "more",
Usage: "show buffered and unread lines",
},
},
Action: func(clicontext *cli.Context) error {
if clicontext.Bool("more") {
_, err := io.Copy(hCtx.stdout, hCtx.progress.buffered())
return err
}
r, err := hCtx.progress.reader()
if err != nil {
return err
}
defer r.Close()
if clicontext.Bool("all") {
_, err := io.Copy(hCtx.stdout, r)
return err
}
n := clicontext.Int("n")
if n <= 0 {
return nil
}
buf := make([]string, n)
cur := 0
total := 0
scanner := bufio.NewScanner(r)
for scanner.Scan() {
s := scanner.Text()
buf[cur] = s
cur++
total++
if cur >= len(buf) {
cur = 0
}
}
if total <= n {
for i := 0; i < total; i++ {
fmt.Fprintf(hCtx.stdout, "%s\n", buf[i])
}
return nil
}
for i := 0; i < n; i++ {
fmt.Fprintf(hCtx.stdout, "%s\n", buf[cur])
cur++
if cur >= len(buf) {
cur = 0
}
}
return nil
},
}
}