-
Notifications
You must be signed in to change notification settings - Fork 20
/
main.go
174 lines (146 loc) · 3.47 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
// Create binary rpm package with ease
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/mh-cbon/go-bin-rpm/rpm"
"github.com/mh-cbon/verbose"
"github.com/urfave/cli"
)
// VERSION is the build version number.
var VERSION = "0.0.0"
var logger = verbose.Auto()
func main() {
app := cli.NewApp()
app.Name = "go-bin-rpm"
app.Version = VERSION
app.Usage = "Generate a binary rpm package"
app.UsageText = "go-bin-rpm <cmd> <options>"
app.Commands = []cli.Command{
{
Name: "generate-spec",
Usage: "Generate the SPEC file",
Action: generateSpec,
Flags: []cli.Flag{
cli.StringFlag{
Name: "file, f",
Value: "rpm.json",
Usage: "Path to the rpm.json file",
},
cli.StringFlag{
Name: "a, arch",
Value: "",
Usage: "Target architecture of the build",
},
cli.StringFlag{
Name: "version",
Value: "",
Usage: "Target version of the build",
},
},
},
{
Name: "generate",
Usage: "Generate the package",
Action: generatePkg,
Flags: []cli.Flag{
cli.StringFlag{
Name: "file, f",
Value: "rpm.json",
Usage: "Path to the rpm.json file",
},
cli.StringFlag{
Name: "b, build-area",
Value: "pkg-build",
Usage: "Path to the build area",
},
cli.StringFlag{
Name: "a, arch",
Value: "",
Usage: "Target architecture of the build",
},
cli.StringFlag{
Name: "o, output",
Value: "",
Usage: "File path to the resulting rpm file",
},
cli.StringFlag{
Name: "version",
Value: "",
Usage: "Target version of the build",
},
},
},
{
Name: "test",
Usage: "Test the package json file",
Action: testPkg,
Flags: []cli.Flag{
cli.StringFlag{
Name: "file, f",
Value: "rpm.json",
Usage: "Path to the rpm.json file",
},
},
},
}
app.Run(os.Args)
}
func generateSpec(c *cli.Context) error {
file := c.String("file")
arch := c.String("arch")
version := c.String("version")
rpmJSON := rpm.Package{}
if err := rpmJSON.Load(file); err != nil {
return cli.NewExitError(err.Error(), 1)
}
if err := rpmJSON.Normalize(arch, version); err != nil {
return cli.NewExitError(err.Error(), 1)
}
spec, err := rpmJSON.GenerateSpecFile("")
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
fmt.Printf("%s", spec)
return nil
}
func generatePkg(c *cli.Context) error {
var err error
file := c.String("file")
arch := c.String("arch")
version := c.String("version")
buildArea := c.String("build-area")
output := c.String("output")
if output == "" {
return cli.NewExitError("--output,-o argument is required", 1)
}
rpmJSON := rpm.Package{}
if err3 := rpmJSON.Load(file); err3 != nil {
return cli.NewExitError(err3.Error(), 1)
}
if buildArea, err = filepath.Abs(buildArea); err != nil {
return cli.NewExitError(err.Error(), 1)
}
if err2 := rpmJSON.Normalize(arch, version); err2 != nil {
return cli.NewExitError(err2.Error(), 1)
}
rpmJSON.InitializeBuildArea(buildArea)
if err = rpmJSON.WriteSpecFile("", buildArea); err != nil {
return cli.NewExitError(err.Error(), 1)
}
if err = rpmJSON.RunBuild(buildArea, output); err != nil {
return cli.NewExitError(err.Error(), 1)
}
fmt.Println("\n\nAll done!")
return nil
}
func testPkg(c *cli.Context) error {
file := c.String("file")
rpmJSON := rpm.Package{}
if err := rpmJSON.Load(file); err != nil {
return cli.NewExitError(err.Error(), 1)
}
fmt.Println("File is correct")
return nil
}