/
boilerplate.go
104 lines (91 loc) · 2.33 KB
/
boilerplate.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
package wio
import (
"crypto/md5"
"fmt"
"os"
)
// WriteMainGo is called by wio command to generate main.go.
// You don't need to use this unless you want to implement your own generator.
func WriteMainGo(name, version string) error {
out, err := os.OpenFile("main.go", os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer out.Close()
_, err = fmt.Fprintf(out, `// This file was generated by wio-init.
package main
import(
"os"
wio "github.com/Maki-Daisuke/go-whole-in-one"
)
func init() {
wio.Name = %q
wio.Version = %q
}
func main() {
// Customize here as you like.
wio.Register("help", wio.HelpCommand)
wio.Register("-h", wio.HelpCommand)
wio.Register("--help", wio.HelpCommand)
wio.Register("version", wio.VersionCommand)
wio.Register("-v", wio.VersionCommand)
wio.Register("--version", wio.VersionCommand)
wio.Exec(os.Args[1:])
}
`, name, version)
return err
}
// WritePackGo is called by wio command to generate pack.go.
// You don't need to use this unless you want to implement your own generator.
func WritePackGo(data []byte, codec string) error {
out, err := os.OpenFile("pack.go", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer out.Close()
_, err = fmt.Fprintf(out, `// DO NOT EDIT! This file was generated by wio-init.
package main
import(
"fmt"
"os"
"os/user"
"path/filepath"
"strings"
wio "github.com/Maki-Daisuke/go-whole-in-one"
)
var(
data = %q
hash = "%X"
)
func init() {
u, err := user.Current()
if err != nil {
panic(err)
}
unpackPath := filepath.Join(os.TempDir(), fmt.Sprintf("wiocache-%%s-%%s-%%s-%%s", wio.Name, wio.Version, u.Uid, hash))
err = os.Mkdir(unpackPath, 0700)
if os.IsExist(err) {
// Do nothing because the data is already unpacked
} else if err != nil {
panic(err)
} else if data != "" {
wio.Unpack(unpackPath, strings.NewReader(data), %q)
}
wio.SetEnv(unpackPath)
}
`, data, md5.Sum(data), codec)
return err
}
// WritePackingList is called by wio command to generate pack.go.
// You don't need to use this unless you want to implement your own generator.
func WritePackingList(name string) error {
out, err := os.OpenFile("packing-list", os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer out.Close()
_, err = fmt.Fprintf(out, `# This file was generated by wio-init.
%s-*
`, name)
return err
}