/
main.go
271 lines (233 loc) · 6.13 KB
/
main.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package main
import (
"bytes"
"flag"
"fmt"
"html/template"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"github.com/linuxerwang/ar"
"github.com/linuxerwang/confish"
)
// DebControl specifies the attributes of a deb control file.
type DebControl struct {
PkgName string `cfg-attr:"pkg-name"`
Maintainer string `cfg-attr:"maintainer"`
Desc string `cfg-attr:"description"`
Attrs map[string]string `cfg-attr:"other-attrs"`
Arch string
Version string
}
// FileEntry specifies a input file and it's path in the debian package.
type FileEntry struct {
Path string `cfg-attr:"path"` // input file path
DebPath string `cfg-attr:"deb-path"` // path in deb file
FileInfo os.FileInfo
Md5sum string
}
// Symlink specifies an absolute symbolic link for data files.
type Symlink struct {
From string `cfg-attr:"from"`
To string `cfg-attr:"to"`
}
// DebSpec specifies the whole deb structure: control files and data files.
type DebSpec struct {
DebCtrl *DebControl `cfg-attr:"control"`
Debian []*FileEntry `cfg-attr:"debian"`
Content []*FileEntry `cfg-attr:"content"`
Link []*Symlink `cfg-attr:"link"`
}
type tmplVars struct {
PkgName string
PostInst string
PreRm string
Files []*FileEntry
}
var (
outputDir = flag.String("output-dir", ".", "The output directory for the deb file, defaults to current working directory.")
spec = flag.String("spec-file", "", "The spec file in confish format. If not specified, read from stdin.")
version = flag.String("version", "", "The version of the deb file.")
arch = flag.String("arch", "", "The architecture of the deb file.")
verbose = flag.Bool("v", false, "Output verbose message.")
pkgName = flag.String("pkg-name", "", "The deb package name.")
postinst = flag.String("postinst", "", "The postinst file.")
prerm = flag.String("prerm", "", "The prerm file.")
files stringSlice
dirs stringSlice
tmpDir string
)
func init() {
tmpDir = filepath.Join(os.TempDir(), "debmaker")
if err := os.MkdirAll(tmpDir, os.ModePerm); err != nil {
fmt.Printf("Failed to create tempory directory %s, %v.\n", tmpDir, err)
os.Exit(1)
}
}
func usage() {
fmt.Println("The deb file maker.")
fmt.Println()
fmt.Println("Usage:")
fmt.Println(" debmaker [-output-dir output_dir] [-spec-file spec_file] -version version [-arch architecture]")
fmt.Println()
flag.PrintDefaults()
os.Exit(0)
}
func checkFlags() {
if *version == "" {
fmt.Println("FATAL: flag version is required.")
os.Exit(1)
}
if *arch == "" {
out, err := exec.Command("dpkg", "--print-architecture").Output()
if err != nil {
fmt.Println("Failed to run \"dpkg --print-architecture\"")
os.Exit(1)
}
*arch = strings.TrimSpace(string(out))
}
}
func loadSpec() (*DebSpec, error) {
var input io.Reader
if *spec == "" {
input = os.Stdin
} else {
h, err := os.Open(*spec)
if err != nil {
return nil, err
}
defer h.Close()
input = h
}
b, err := ioutil.ReadAll(input)
if err != nil {
return nil, err
}
vars := &tmplVars{
PkgName: *pkgName,
PostInst: *postinst,
PreRm: *prerm,
Files: []*FileEntry{},
}
for _, s := range files {
it := strings.Split(s, "=")
vars.Files = append(vars.Files, &FileEntry{
DebPath: strings.TrimSpace(it[0]),
Path: strings.TrimSpace(it[1]),
})
}
for _, s := range dirs {
it := strings.Split(s, "=")
dir := strings.TrimSpace(it[0])
files := strings.TrimSpace(it[1])
for _, file := range strings.Split(files, " ") {
file = strings.TrimSpace(file)
if len(file) == 0 {
continue
}
vars.Files = append(vars.Files, &FileEntry{
DebPath: filepath.Join(dir, filepath.Base(file)),
Path: file,
})
}
}
var buf bytes.Buffer
t := template.Must(template.New("spec").Parse(string(b)))
if err := t.Execute(&buf, vars); err != nil {
return nil, err
}
if *verbose {
fmt.Println(buf.String())
}
debSpec := DebSpec{}
err = confish.Parse(strings.NewReader(buf.String()), &debSpec)
if err != nil {
return nil, err
}
debSpec.DebCtrl.Arch = *arch
debSpec.DebCtrl.Version = *version
return &debSpec, nil
}
func addDebBinary(arw *ar.Writer) error {
header := &ar.Header{
Size: 4,
Name: "debian-binary",
ModTime: time.Now(),
Mode: 0777,
}
if err := arw.WriteHeader(header); err != nil {
if *verbose {
fmt.Println("Failed to write header for debian-binary.")
}
return err
}
if _, err := arw.Write([]byte("2.0\n")); err != nil {
if *verbose {
fmt.Println("Failed to add debian-binary to deb file.")
}
return err
}
if *verbose {
fmt.Println("Added debian-binary to deb file.")
}
return nil
}
func main() {
flag.Var(&files, "file", "The file to be added in deb file, repeatable.")
flag.Var(&dirs, "dir", "The dir to be added in deb file, repeatable.")
flag.Usage = usage
flag.Parse()
checkFlags()
if *verbose {
fmt.Println("Arguments: ", os.Args)
for _, s := range os.Args {
fmt.Println("Arg: ", s)
}
}
debSpec, err := loadSpec()
if err != nil {
fmt.Printf("Failed to load spec file, %v.\n", err)
os.Exit(1)
}
dfn := fmt.Sprintf("%s_%s_%s.deb", debSpec.DebCtrl.PkgName, *version, *arch)
deb, err := os.Create(filepath.Join(*outputDir, dfn))
if err != nil {
fmt.Printf("Failed to create the deb file, %v.\n", err)
os.Exit(1)
}
defer deb.Close()
// Create ar
arw := ar.NewWriter(deb)
err = arw.WriteGlobalHeader()
if err != nil {
fmt.Printf("Failed to write global header, %v.\n", err)
os.Exit(1)
}
// Add debian-binary file
if err = addDebBinary(arw); err != nil {
fmt.Printf("Failed to debian-binary to deb file, %v.\n", err)
os.Exit(1)
}
// Fill file info
if c, err := fillFileInfo(debSpec.Content); err != nil {
fmt.Printf("Failed to collect the content tree, %v.\n", err)
os.Exit(1)
} else {
debSpec.Content = c
}
// Add control.tar.gz
if err = addControlFiles(arw, debSpec); err != nil {
fmt.Printf("Failed to add control.tar.gz to deb file, %v.\n", err)
os.Exit(1)
}
// Add data.tar.gz
if err = addContentFiles(arw, debSpec); err != nil {
fmt.Printf("Failed to add data.tar.gz to deb file, %v.\n", err)
os.Exit(1)
}
fmt.Printf("Created %s.\n", dfn)
}