1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-10 03:47:03 +02:00
goreleaser/pipeline/fpm/fpm.go

176 lines
4.6 KiB
Go
Raw Normal View History

2017-04-14 20:39:32 +02:00
// Package fpm implements the Pipe interface providing FPM bindings.
2017-01-30 01:55:32 +02:00
package fpm
import (
2017-06-28 00:36:36 +02:00
"fmt"
"io/ioutil"
2017-01-30 01:55:32 +02:00
"os/exec"
"path/filepath"
2017-06-22 05:09:14 +02:00
"github.com/apex/log"
2017-12-17 21:11:08 +02:00
"github.com/pkg/errors"
"golang.org/x/sync/errgroup"
2017-01-30 01:55:32 +02:00
"github.com/goreleaser/goreleaser/context"
2017-12-17 21:11:08 +02:00
"github.com/goreleaser/goreleaser/internal/artifact"
2017-08-27 18:18:23 +02:00
"github.com/goreleaser/goreleaser/internal/linux"
2017-12-17 21:11:08 +02:00
"github.com/goreleaser/goreleaser/internal/nametemplate"
2017-08-20 21:35:46 +02:00
"github.com/goreleaser/goreleaser/pipeline"
2017-01-30 01:55:32 +02:00
)
// ErrNoFPM is shown when fpm cannot be found in $PATH
var ErrNoFPM = errors.New("fpm not present in $PATH")
// Pipe for fpm packaging
type Pipe struct{}
func (Pipe) String() string {
return "creating Linux packages with fpm"
}
// Default sets the pipe defaults
func (Pipe) Default(ctx *context.Context) error {
if ctx.Config.FPM.Bindir == "" {
ctx.Config.FPM.Bindir = "/usr/local/bin"
}
return nil
2017-01-30 01:55:32 +02:00
}
// Run the pipe
func (Pipe) Run(ctx *context.Context) error {
2017-01-30 02:01:26 +02:00
if len(ctx.Config.FPM.Formats) == 0 {
2017-08-20 21:35:46 +02:00
return pipeline.Skip("no output formats configured")
2017-01-30 02:01:26 +02:00
}
2017-01-30 11:53:05 +02:00
_, err := exec.LookPath("fpm")
if err != nil {
2017-01-30 01:55:32 +02:00
return ErrNoFPM
}
2017-09-15 02:19:56 +02:00
return doRun(ctx)
}
2017-01-30 02:33:08 +02:00
2017-09-15 02:19:56 +02:00
func doRun(ctx *context.Context) error {
var g errgroup.Group
sem := make(chan bool, ctx.Parallelism)
2017-01-30 01:55:32 +02:00
for _, format := range ctx.Config.FPM.Formats {
2017-12-17 21:11:08 +02:00
for platform, artifacts := range ctx.Artifacts.Filter(
artifact.And(
artifact.ByType(artifact.Binary),
artifact.ByGoos("linux"),
),
).GroupByPlatform() {
sem <- true
format := format
2017-12-17 21:11:08 +02:00
arch := linux.Arch(platform) // TODO: could probably pass artifact.Goarch here
artifacts := artifacts
g.Go(func() error {
defer func() {
<-sem
}()
return create(ctx, format, arch, artifacts)
})
2017-01-30 01:55:32 +02:00
}
}
return g.Wait()
2017-01-30 01:55:32 +02:00
}
2017-12-17 21:11:08 +02:00
func create(ctx *context.Context, format, arch string, binaries []artifact.Artifact) error {
2017-12-18 01:31:34 +02:00
// TODO: should add template support here probably... for now, let's use archive's template
2017-12-17 22:01:58 +02:00
folder, err := nametemplate.Apply(ctx, binaries[0], ctx.Config.ProjectName)
2017-12-17 21:11:08 +02:00
if err != nil {
return err
}
2017-07-01 17:58:48 +02:00
var path = filepath.Join(ctx.Config.Dist, folder)
var file = path + "." + format
2017-08-19 00:45:44 +02:00
var log = log.WithField("format", format).WithField("arch", arch)
dir, err := ioutil.TempDir("", "fpm")
if err != nil {
return err
}
log.WithField("file", file).WithField("workdir", dir).Info("creating fpm archive")
var options = basicOptions(ctx, dir, format, arch, file)
2017-09-15 02:19:56 +02:00
for _, binary := range binaries {
// This basically tells fpm to put the binary in the bindir, e.g. /usr/local/bin
2017-09-15 02:19:56 +02:00
// binary=/usr/local/bin/binary
log.WithField("path", binary.Path).
WithField("name", binary.Name).
Debug("added binary to fpm package")
options = append(options, fmt.Sprintf(
"%s=%s",
binary.Path,
filepath.Join(ctx.Config.FPM.Bindir, binary.Name),
2017-09-15 02:19:56 +02:00
))
}
for src, dest := range ctx.Config.FPM.Files {
log.WithField("src", src).
WithField("dest", dest).
Debug("added an extra file to the fpm package")
options = append(options, fmt.Sprintf(
"%s=%s",
src,
dest,
))
}
log.WithField("args", options).Debug("creating fpm package")
/* #nosec */
2017-09-15 02:19:56 +02:00
if out, err := exec.Command("fpm", options...).CombinedOutput(); err != nil {
return errors.Wrap(err, string(out))
2017-09-15 02:19:56 +02:00
}
2017-12-17 21:11:08 +02:00
ctx.Artifacts.Add(artifact.Artifact{
Type: artifact.LinuxPackage,
Name: folder + "." + format,
Path: file,
Goos: binaries[0].Goos,
Goarch: binaries[0].Goarch,
Goarm: binaries[0].Goarm,
})
2017-09-15 02:19:56 +02:00
return nil
}
func basicOptions(ctx *context.Context, workdir, format, arch, file string) []string {
2017-01-30 01:55:32 +02:00
var options = []string{
2017-02-01 19:40:27 +02:00
"--input-type", "dir",
"--output-type", format,
2017-07-02 03:42:10 +02:00
"--name", ctx.Config.ProjectName,
2017-02-01 19:40:27 +02:00
"--version", ctx.Version,
"--architecture", arch,
"--package", file,
2017-01-30 01:55:32 +02:00
"--force",
"--workdir", workdir,
}
if ctx.Debug {
options = append(options, "--debug")
2017-01-30 01:55:32 +02:00
}
2017-04-21 22:02:28 +02:00
if ctx.Config.FPM.Vendor != "" {
options = append(options, "--vendor", ctx.Config.FPM.Vendor)
}
2017-04-21 22:02:28 +02:00
if ctx.Config.FPM.Homepage != "" {
options = append(options, "--url", ctx.Config.FPM.Homepage)
}
2017-04-21 22:02:28 +02:00
if ctx.Config.FPM.Maintainer != "" {
options = append(options, "--maintainer", ctx.Config.FPM.Maintainer)
}
2017-04-21 22:02:28 +02:00
if ctx.Config.FPM.Description != "" {
options = append(options, "--description", ctx.Config.FPM.Description)
}
2017-04-21 22:02:28 +02:00
if ctx.Config.FPM.License != "" {
options = append(options, "--license", ctx.Config.FPM.License)
}
2017-01-30 02:33:08 +02:00
for _, dep := range ctx.Config.FPM.Dependencies {
2017-02-01 19:40:27 +02:00
options = append(options, "--depends", dep)
2017-01-30 01:55:32 +02:00
}
2017-02-01 19:40:27 +02:00
for _, conflict := range ctx.Config.FPM.Conflicts {
options = append(options, "--conflicts", conflict)
}
// FPM requires --rpm-os=linux if your rpm target is linux
2017-08-18 23:44:34 +02:00
if format == "rpm" {
options = append(options, "--rpm-os", "linux")
}
2017-09-15 02:19:56 +02:00
return options
2017-01-30 01:55:32 +02:00
}