diff --git a/Makefile b/Makefile index 19a49789d..a87f94113 100644 --- a/Makefile +++ b/Makefile @@ -25,20 +25,7 @@ fmt: # Run all the linters lint: - gometalinter --vendor --disable-all \ - --enable=deadcode \ - --enable=ineffassign \ - --enable=gosimple \ - --enable=staticcheck \ - --enable=gofmt \ - --enable=goimports \ - --enable=dupl \ - --enable=misspell \ - --enable=errcheck \ - --enable=vet \ - --enable=vetshadow \ - --deadline=10m \ - ./... + gometalinter --vendor ./... # Run all the tests and code checks ci: test lint diff --git a/checksum/checksum.go b/checksum/checksum.go index 0684d2e12..6a32ce58a 100644 --- a/checksum/checksum.go +++ b/checksum/checksum.go @@ -24,7 +24,7 @@ func calculate(hash hash.Hash, path string) (result string, err error) { return doCalculate(hash, file) } -func doCalculate(hash hash.Hash, file *os.File) (result string, err error) { +func doCalculate(hash hash.Hash, file io.Reader) (result string, err error) { _, err = io.Copy(hash, file) if err != nil { return diff --git a/internal/name/name_test.go b/internal/name/name_test.go index 8c106c695..9addf22c1 100644 --- a/internal/name/name_test.go +++ b/internal/name/name_test.go @@ -116,9 +116,6 @@ func TestNameDefaultTemplate(t *testing.T) { }, Version: "1.2.3", } - type buildTarget struct { - goos, goarch, goarm string - } for key, target := range map[string]buildtarget.Target{ "test_1.2.3_darwin_amd64": buildtarget.New("darwin", "amd64", ""), "test_1.2.3_linux_arm64": buildtarget.New("linux", "arm64", ""), diff --git a/pipeline/defaults/defaults.go b/pipeline/defaults/defaults.go index b3715f7c6..8cd339a85 100644 --- a/pipeline/defaults/defaults.go +++ b/pipeline/defaults/defaults.go @@ -57,26 +57,31 @@ func (Pipe) Run(ctx *context.Context) error { } ctx.Config.Brew.Install = strings.Join(installs, "\n") } - if len(ctx.Config.Dockers) == 1 { - if ctx.Config.Dockers[0].Goos == "" { - ctx.Config.Dockers[0].Goos = "linux" - } - if ctx.Config.Dockers[0].Goarch == "" { - ctx.Config.Dockers[0].Goarch = "amd64" - } - if ctx.Config.Dockers[0].Binary == "" { - ctx.Config.Dockers[0].Binary = ctx.Config.Builds[0].Binary - } - if ctx.Config.Dockers[0].Dockerfile == "" { - ctx.Config.Dockers[0].Dockerfile = "Dockerfile" - } - } err := setArchiveDefaults(ctx) + setDockerDefaults(ctx) log.WithField("config", ctx.Config).Debug("defaults set") return err } +func setDockerDefaults(ctx *context.Context) { + if len(ctx.Config.Dockers) != 1 { + return + } + if ctx.Config.Dockers[0].Goos == "" { + ctx.Config.Dockers[0].Goos = "linux" + } + if ctx.Config.Dockers[0].Goarch == "" { + ctx.Config.Dockers[0].Goarch = "amd64" + } + if ctx.Config.Dockers[0].Binary == "" { + ctx.Config.Dockers[0].Binary = ctx.Config.Builds[0].Binary + } + if ctx.Config.Dockers[0].Dockerfile == "" { + ctx.Config.Dockers[0].Dockerfile = "Dockerfile" + } +} + func isBrewBuild(build config.Build) bool { for _, ignore := range build.Ignore { if ignore.Goos == "darwin" && ignore.Goarch == "amd64" { diff --git a/pipeline/docker/docker.go b/pipeline/docker/docker.go index 6f28113ba..f0728537b 100644 --- a/pipeline/docker/docker.go +++ b/pipeline/docker/docker.go @@ -36,6 +36,10 @@ func (Pipe) Run(ctx *context.Context) error { if err != nil { return ErrNoDocker } + return doRun(ctx) +} + +func doRun(ctx *context.Context) error { for _, docker := range ctx.Config.Dockers { var imagePlatform = docker.Goos + docker.Goarch + docker.Goarm for platform, groups := range ctx.Binaries { @@ -47,7 +51,7 @@ func (Pipe) Run(ctx *context.Context) error { if binary.Name != docker.Binary { continue } - var err = doRun(ctx, folder, docker, binary) + var err = process(ctx, folder, docker, binary) if err != nil && !pipeline.IsSkip(err) { return err } @@ -58,7 +62,7 @@ func (Pipe) Run(ctx *context.Context) error { return nil } -func doRun(ctx *context.Context, folder string, docker config.Docker, binary context.Binary) error { +func process(ctx *context.Context, folder string, docker config.Docker, binary context.Binary) error { var root = filepath.Join(ctx.Config.Dist, folder) var dockerfile = filepath.Join(root, filepath.Base(docker.Dockerfile)) var image = fmt.Sprintf("%s:%s", docker.Image, ctx.Version) diff --git a/pipeline/fpm/fpm.go b/pipeline/fpm/fpm.go index 4618a4aec..4efa2c1ea 100644 --- a/pipeline/fpm/fpm.go +++ b/pipeline/fpm/fpm.go @@ -35,7 +35,10 @@ func (Pipe) Run(ctx *context.Context) error { if err != nil { return ErrNoFPM } + return doRun(ctx) +} +func doRun(ctx *context.Context) error { var g errgroup.Group for _, format := range ctx.Config.FPM.Formats { for platform, groups := range ctx.Binaries { @@ -61,6 +64,41 @@ func create(ctx *context.Context, format, folder, arch string, binaries []contex var log = log.WithField("format", format).WithField("arch", arch) log.WithField("file", file).Info("creating fpm archive") + var options = basicOptions(ctx, format, arch, file) + + for _, binary := range binaries { + // This basically tells fpm to put the binary in the /usr/local/bin + // 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("/usr/local/bin", binary.Name), + )) + } + + 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") + if out, err := exec.Command("fpm", options...).CombinedOutput(); err != nil { + return errors.New(string(out)) + } + ctx.AddArtifact(file) + return nil +} + +func basicOptions(ctx *context.Context, format, arch, file string) []string { var options = []string{ "--input-type", "dir", "--output-type", format, @@ -97,35 +135,5 @@ func create(ctx *context.Context, format, folder, arch string, binaries []contex if format == "rpm" { options = append(options, "--rpm-os", "linux") } - - for _, binary := range binaries { - // This basically tells fpm to put the binary in the /usr/local/bin - // 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("/usr/local/bin", binary.Name), - )) - } - - 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") - if out, err := exec.Command("fpm", options...).CombinedOutput(); err != nil { - return errors.New(string(out)) - } - ctx.AddArtifact(file) - return nil + return options } diff --git a/pipeline/snapcraft/snapcraft.go b/pipeline/snapcraft/snapcraft.go index 87e446bed..ba853bf7c 100644 --- a/pipeline/snapcraft/snapcraft.go +++ b/pipeline/snapcraft/snapcraft.go @@ -27,8 +27,8 @@ 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") -// SnapcraftMetadata to generate the snap package -type SnapcraftMetadata struct { +// Metadata to generate the snap package +type Metadata struct { Name string Version string Summary string @@ -99,7 +99,7 @@ func create(ctx *context.Context, folder, arch string, binaries []context.Binary var file = filepath.Join(primeDir, "meta", "snap.yaml") log.WithField("file", file).Debug("creating snap metadata") - var metadata = &SnapcraftMetadata{ + var metadata = &Metadata{ Version: ctx.Version, Summary: ctx.Config.Snapcraft.Summary, Description: ctx.Config.Snapcraft.Description, diff --git a/pipeline/snapcraft/snapcraft_test.go b/pipeline/snapcraft/snapcraft_test.go index a73d61936..d5bd555db 100644 --- a/pipeline/snapcraft/snapcraft_test.go +++ b/pipeline/snapcraft/snapcraft_test.go @@ -85,10 +85,10 @@ func TestRunPipeWithName(t *testing.T) { assert.NoError(Pipe{}.Run(ctx)) yamlFile, err := ioutil.ReadFile(filepath.Join(dist, "testprojectname_linuxamd64", "prime", "meta", "snap.yaml")) assert.NoError(err) - var snapcraftMetadata SnapcraftMetadata - err = yaml.Unmarshal(yamlFile, &snapcraftMetadata) + var metadata Metadata + err = yaml.Unmarshal(yamlFile, &metadata) assert.NoError(err) - assert.Equal(snapcraftMetadata.Name, "testsnapname") + assert.Equal(metadata.Name, "testsnapname") } func TestRunPipeWithPlugsAndDaemon(t *testing.T) { @@ -119,11 +119,11 @@ func TestRunPipeWithPlugsAndDaemon(t *testing.T) { assert.NoError(Pipe{}.Run(ctx)) yamlFile, err := ioutil.ReadFile(filepath.Join(dist, "mybin_linuxamd64", "prime", "meta", "snap.yaml")) assert.NoError(err) - var snapcraftMetadata SnapcraftMetadata - err = yaml.Unmarshal(yamlFile, &snapcraftMetadata) + var metadata Metadata + err = yaml.Unmarshal(yamlFile, &metadata) assert.NoError(err) - assert.Equal(snapcraftMetadata.Apps["mybin"].Plugs, []string{"home", "network"}) - assert.Equal(snapcraftMetadata.Apps["mybin"].Daemon, "simple") + assert.Equal(metadata.Apps["mybin"].Plugs, []string{"home", "network"}) + assert.Equal(metadata.Apps["mybin"].Daemon, "simple") } func TestNoSnapcraftInPath(t *testing.T) {