This repository has been archived by the owner on May 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
156 lines (131 loc) · 3.54 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
package main
import (
"fmt"
"github.com/dawidd6/deber/pkg/docker"
"github.com/dawidd6/deber/pkg/log"
"github.com/dawidd6/deber/pkg/naming"
"github.com/dawidd6/deber/pkg/steps"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"os"
"path/filepath"
"pault.ag/go/debian/changelog"
"time"
)
const (
// Program is the name of program
Program = "deber"
// Version of program
Version = "1.1.1"
// Description of program
Description = "Debian packaging with Docker."
)
var (
buildDir = pflag.StringP("build-dir", "B", "/tmp", "where to place build stuff")
cacheDir = pflag.StringP("cache-dir", "C", "/tmp", "where to place cached stuff")
distribution = pflag.StringP("distribution", "d", "", "override target distribution")
packages = pflag.StringArrayP("package", "p", nil, "additional packages to be installed in container (either single .deb or a directory)")
age = pflag.DurationP("age", "a", time.Hour*24*14, "time after which image will be refreshed")
network = pflag.BoolP("network", "n", false, "allow network access during package build")
shell = pflag.BoolP("shell", "s", false, "launch interactive shell in container")
dpkgFlags = pflag.StringP("dpkg-flags", "D", "-tc", "additional flags to be passed to dpkg-buildpackage in container")
lintianFlags = pflag.StringP("lintian-flags", "L", "-i -I", "additional flags to be passed to lintian in container")
noLintian = pflag.BoolP("no-lintian", "l", false, "don't run lintian in container")
noLogColor = pflag.BoolP("no-log-color", "c", false, "do not colorize log output")
noRemove = pflag.BoolP("no-remove", "r", false, "do not remove container at the end of the process")
)
func main() {
cmd := &cobra.Command{
Use: fmt.Sprintf("%s [FLAGS ...]", Program),
Short: Description,
Version: Version,
RunE: run,
}
cmd.SetHelpCommand(&cobra.Command{Hidden: true})
cmd.DisableFlagsInUseLine = true
cmd.SilenceUsage = true
cmd.SilenceErrors = true
err := cmd.Execute()
if err != nil {
log.Error(err)
os.Exit(1)
}
}
func run(cmd *cobra.Command, args []string) error {
log.NoColor = *noLogColor
dock, err := docker.New()
if err != nil {
return err
}
cwd, err := os.Getwd()
if err != nil {
return err
}
home, err := os.UserHomeDir()
if err != nil {
return err
}
path := filepath.Join(cwd, "debian/changelog")
ch, err := changelog.ParseFileOne(path)
if err != nil {
return err
}
if *distribution == "" {
*distribution = ch.Target
}
namingArgs := naming.Args{
Prefix: Program,
Source: ch.Source,
Version: ch.Version.String(),
Upstream: ch.Version.Version,
Target: *distribution,
SourceBaseDir: cwd,
BuildBaseDir: *buildDir,
CacheBaseDir: *cacheDir,
ArchiveBaseDir: filepath.Join(home, Program),
}
n := naming.New(namingArgs)
err = steps.Build(dock, n, *age)
if err != nil {
return err
}
err = steps.Create(dock, n, *packages)
if err != nil {
return err
}
err = steps.Start(dock, n)
if err != nil {
return err
}
if *shell {
return steps.ShellOptional(dock, n)
}
err = steps.Tarball(n)
if err != nil {
return err
}
err = steps.Depends(dock, n, *packages)
if err != nil {
return err
}
err = steps.Package(dock, n, *dpkgFlags, *network)
if err != nil {
return err
}
err = steps.Test(dock, n, *lintianFlags, *noLintian)
if err != nil {
return err
}
err = steps.Archive(n)
if err != nil {
return err
}
err = steps.Stop(dock, n)
if err != nil {
return err
}
if *noRemove {
return nil
}
return steps.Remove(dock, n)
}