-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.go
146 lines (124 loc) · 3.12 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
// Package go-bin-deb creates binary package for debian system
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"github.com/mh-cbon/go-bin-deb/debian"
"github.com/mh-cbon/verbose"
"github.com/urfave/cli"
)
// VERSION is the last build number.
var VERSION = "0.0.0"
var logger = verbose.Auto()
func main() {
app := cli.NewApp()
app.Name = "go-bin-deb"
app.Version = VERSION
app.Usage = "Generate a binary debian package"
app.UsageText = "go-bin-deb <cmd> <options>"
app.Commands = []cli.Command{
{
Name: "generate",
Usage: "Generate the contents of the package",
Action: generateContents,
Flags: []cli.Flag{
cli.StringFlag{
Name: "wd, w",
Value: "pkg-build",
Usage: "Working directory to prepare the package",
},
cli.StringFlag{
Name: "output, o",
Value: "",
Usage: "Output directory for the debian package files",
},
cli.StringFlag{
Name: "file, f",
Value: "deb.json",
Usage: "Path to the deb.json file",
},
cli.StringFlag{
Name: "version",
Value: "",
Usage: "Version of the package",
},
cli.StringFlag{
Name: "arch, a",
Value: "",
Usage: "Arch of the package",
},
},
},
{
Name: "test",
Usage: "Test the package json file",
Action: testPkg,
Flags: []cli.Flag{
cli.StringFlag{
Name: "file, f",
Value: "deb.json",
Usage: "Path to the deb.json file",
},
},
},
}
app.Run(os.Args)
}
func generateContents(c *cli.Context) error {
output := c.String("output")
wd := c.String("wd")
file := c.String("file")
version := c.String("version")
arch := c.String("arch")
pkgDir := filepath.Join(wd)
if o, err := filepath.Abs(output); err != nil {
return cli.NewExitError(err.Error(), 1)
} else {
output = o
}
debJSON := debian.Package{}
// load the deb.json file
if err := debJSON.Load(file); err != nil {
return cli.NewExitError(err.Error(), 1)
}
logger.Println("deb.json loaded")
// normalize data
debJSON.Normalize(pkgDir, version, arch)
logger.Println("pkg data normalized")
logger.Printf("Generating files in %s", pkgDir)
if err := debJSON.GenerateFiles(filepath.Dir(file), pkgDir); err != nil {
return cli.NewExitError(err.Error(), 1)
}
logger.Printf("Building package in %s to %s", wd, output)
if err := buildPackage(pkgDir, output); err != nil {
return cli.NewExitError(err.Error(), 1)
}
logger.Printf("linting package in %s to %s", wd, output)
lintPackage(pkgDir, output) // it does not need to fail.
return nil
}
func testPkg(c *cli.Context) error {
file := c.String("file")
debJSON := debian.Package{}
if err := debJSON.Load(file); err != nil {
return cli.NewExitError(err.Error(), 1)
}
fmt.Println("File is correct")
return nil
}
func buildPackage(wd string, output string) error {
oCmd := exec.Command("fakeroot", "dpkg-deb", "--build", "debian", output)
oCmd.Dir = wd
oCmd.Stdout = os.Stdout
oCmd.Stderr = os.Stderr
return oCmd.Run()
}
func lintPackage(wd string, output string) error {
oCmd := exec.Command("lintian", output)
oCmd.Dir = wd
oCmd.Stdout = os.Stdout
oCmd.Stderr = os.Stderr
return oCmd.Run()
}