-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.go
124 lines (108 loc) · 2.58 KB
/
README.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//go:build ignore
// +build ignore
// This program generates README.md. It can be invoked by running
// go generate
package main
import (
"log"
"os"
"os/exec"
"text/template"
// "time"
"fmt"
"runtime"
"strings"
)
func die(err error) {
r := "panic exit."
if err == nil {
return
}
pc, file, no, ok := runtime.Caller(10)
details := runtime.FuncForPC(pc)
d := ""
if details != nil {
d = " in " + details.Name() + "()"
}
if ok {
r = fmt.Sprintf("// died %s with err=%v\n// %s#%d", d, err, file, no)
} else {
r = fmt.Sprintf("// died %s with err=%v", d, err)
}
dumpstack()
log.Fatal(r)
}
func dumpstack() {
pc := make([]uintptr, 30)
n := runtime.Callers(0, pc)
if n == 0 {
// No pcs available. Stop now.
// This can happen if the first argument to runtime.Callers is large.
return
}
pc = pc[:n] // pass only valid pcs to runtime.CallersFrames
frames := runtime.CallersFrames(pc)
// Loop to get frames.
// A fixed number of pcs can expand to an indefinite number of Frames.
n = 1
for {
frame, more := frames.Next()
// To keep this example's output stable
// even if there are changes in the testing package,
// stop unwinding when we leave package runtime.
fmt.Printf("[%2d] : %s in %s line %d\n", n, frame.Function, frame.File, frame.Line)
if strings.HasPrefix(frame.Function, "main.main") {
break
}
n += 1
if !more {
break
}
}
}
func run(cmd string) string {
fmt.Printf("---> run(" + cmd + ")\n")
cwd, err := os.Getwd()
die(err)
defer os.Chdir(cwd)
die(os.Chdir(os.TempDir()))
tmpdir := "movieinfo.tmp"
_ = os.Mkdir(tmpdir, 0755)
die(os.Chdir(tmpdir))
c := exec.Command("bash", "-c", cmd)
out, err := c.CombinedOutput()
die(err)
return string(out)
}
func runclean(cmd string) string {
fmt.Printf("---> runclean(" + cmd + ")\n")
cwd, err := os.Getwd()
die(err)
defer os.Chdir(cwd)
die(os.Chdir(os.TempDir()))
tmpdir := "movieinfo.tmp"
die(os.RemoveAll(tmpdir))
_ = os.Mkdir(tmpdir, 0755)
die(os.Chdir(tmpdir))
c := exec.Command("bash", "-c", cmd)
out, err := c.CombinedOutput()
die(err)
return string(out)
}
func main() {
cwd, err := os.Getwd()
err = os.Setenv("PATH", cwd+":"+os.Getenv("PATH"))
funcMap := template.FuncMap{
"run": run,
"runclean": runclean,
}
var packageTemplate = template.Must(template.New("").Funcs(funcMap).ParseFiles("README.md.template"))
// Create an *exec.Cmd
cmd := exec.Command("echo", "Called from Go!")
output, err := cmd.CombinedOutput()
log.Print(string(output))
f, err := os.Create("README.md")
die(err)
defer f.Close()
packageTemplate.ExecuteTemplate(f, "README.md.template", nil)
}