forked from goreleaser/goreleaser
/
snapcraft.go
187 lines (165 loc) · 5.17 KB
/
snapcraft.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
// Package snapcraft implements the Pipe interface providing Snapcraft bindings.
package snapcraft
import (
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/apex/log"
yaml "gopkg.in/yaml.v2"
"github.com/goreleaser/goreleaser/context"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/linux"
"github.com/goreleaser/goreleaser/internal/semerrgroup"
"github.com/goreleaser/goreleaser/internal/tmpl"
"github.com/goreleaser/goreleaser/pipeline"
)
// ErrNoSnapcraft is shown when snapcraft cannot be found in $PATH
var ErrNoSnapcraft = errors.New("snapcraft not present in $PATH")
// ErrNoDescription is shown when no description provided
var ErrNoDescription = errors.New("no description provided for snapcraft")
// ErrNoSummary is shown when no summary provided
var ErrNoSummary = errors.New("no summary provided for snapcraft")
// Metadata to generate the snap package
type Metadata struct {
Name string
Version string
Summary string
Description string
Grade string `yaml:",omitempty"`
Confinement string `yaml:",omitempty"`
Architectures []string
Apps map[string]AppMetadata
}
// AppMetadata for the binaries that will be in the snap package
type AppMetadata struct {
Command string
Plugs []string `yaml:",omitempty"`
Daemon string `yaml:",omitempty"`
}
const defaultNameTemplate = "{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}"
// Pipe for snapcraft packaging
type Pipe struct{}
func (Pipe) String() string {
return "creating Linux packages with snapcraft"
}
// Default sets the pipe defaults
func (Pipe) Default(ctx *context.Context) error {
var snap = &ctx.Config.Snapcraft
if snap.NameTemplate == "" {
snap.NameTemplate = defaultNameTemplate
}
return nil
}
// Run the pipe
func (Pipe) Run(ctx *context.Context) error {
if ctx.Config.Snapcraft.Summary == "" && ctx.Config.Snapcraft.Description == "" {
return pipeline.Skip("no summary nor description were provided")
}
if ctx.Config.Snapcraft.Summary == "" {
return ErrNoSummary
}
if ctx.Config.Snapcraft.Description == "" {
return ErrNoDescription
}
_, err := exec.LookPath("snapcraft")
if err != nil {
return ErrNoSnapcraft
}
var g = semerrgroup.New(ctx.Parallelism)
for platform, binaries := range ctx.Artifacts.Filter(
artifact.And(
artifact.ByGoos("linux"),
artifact.ByType(artifact.Binary),
),
).GroupByPlatform() {
arch := linux.Arch(platform)
if arch == "armel" {
log.WithField("arch", arch).Warn("ignored unsupported arch")
continue
}
binaries := binaries
g.Go(func() error {
return create(ctx, arch, binaries)
})
}
return g.Wait()
}
func create(ctx *context.Context, arch string, binaries []artifact.Artifact) error {
var log = log.WithField("arch", arch)
folder, err := tmpl.New(ctx).
WithArtifact(binaries[0], ctx.Config.Snapcraft.Replacements).
Apply(ctx.Config.Snapcraft.NameTemplate)
if err != nil {
return err
}
// prime is the directory that then will be compressed to make the .snap package.
var folderDir = filepath.Join(ctx.Config.Dist, folder)
var primeDir = filepath.Join(folderDir, "prime")
var metaDir = filepath.Join(primeDir, "meta")
// #nosec
if err = os.MkdirAll(metaDir, 0755); err != nil {
return err
}
var file = filepath.Join(primeDir, "meta", "snap.yaml")
log.WithField("file", file).Debug("creating snap metadata")
var metadata = &Metadata{
Version: ctx.Version,
Summary: ctx.Config.Snapcraft.Summary,
Description: ctx.Config.Snapcraft.Description,
Grade: ctx.Config.Snapcraft.Grade,
Confinement: ctx.Config.Snapcraft.Confinement,
Architectures: []string{arch},
Apps: make(map[string]AppMetadata),
}
metadata.Name = ctx.Config.ProjectName
if ctx.Config.Snapcraft.Name != "" {
metadata.Name = ctx.Config.Snapcraft.Name
}
for _, binary := range binaries {
log.WithField("path", binary.Path).
WithField("name", binary.Name).
Debug("passed binary to snapcraft")
appMetadata := AppMetadata{
Command: binary.Name,
}
if configAppMetadata, ok := ctx.Config.Snapcraft.Apps[binary.Name]; ok {
appMetadata.Plugs = configAppMetadata.Plugs
appMetadata.Daemon = configAppMetadata.Daemon
appMetadata.Command = strings.Join([]string{
appMetadata.Command,
configAppMetadata.Args,
}, " ")
}
metadata.Apps[binary.Name] = appMetadata
destBinaryPath := filepath.Join(primeDir, filepath.Base(binary.Path))
if err = os.Link(binary.Path, destBinaryPath); err != nil {
return err
}
}
out, err := yaml.Marshal(metadata)
if err != nil {
return err
}
if err = ioutil.WriteFile(file, out, 0644); err != nil {
return err
}
var snap = filepath.Join(ctx.Config.Dist, folder+".snap")
/* #nosec */
var cmd = exec.CommandContext(ctx, "snapcraft", "pack", primeDir, "--output", snap)
if out, err = cmd.CombinedOutput(); err != nil {
return fmt.Errorf("failed to generate snap package: %s", string(out))
}
ctx.Artifacts.Add(artifact.Artifact{
Type: artifact.LinuxPackage,
Name: folder + ".snap",
Path: snap,
Goos: binaries[0].Goos,
Goarch: binaries[0].Goarch,
Goarm: binaries[0].Goarm,
})
return nil
}