1
1
Fork 0
mirror of https://github.com/goreleaser/nfpm synced 2024-06-01 22:16:25 +02:00
nfpm/internal/cmd/package.go
Arsen6331 a18661b627
feat: add support for Arch Linux packages (#543)
* feat: add support for Arch Linux packages

* test: Add initial tests

* test: Increase coverage by modifying example info

* test: Add test for ArchLinux.ConventionalFileName()

* docs: Return error if package name is invalid

* fix: Make empty name invalid

* fix: Add replaces field to .PKGINFO generator

* test: Add additional tests

* test: Test for added replaces field

* docs: Add more comments

* style: Run gofumpt

* fix: Handle errors as recommended by linter

* fix: Allow changing the pkgbase

* style: Resolve semgrep findings

* docs: Change docs to reflect new Arch Linux packager

* docs: Fix spelling mistake in comment

Co-authored-by: Dj Gilcrease <digitalxero@gmail.com>

* docs: use aspell to fix all spelling mistakes

* feat: Handle packaging formats with non-distinct file extensions as described in #546

* fix: Add newline to generated .INSTALL file

* fix: Take into account provided info for non-symlink files

* docs: Fix names for arch-specific scripts in documentation

* fix: Only consider files with the correct packager field

* fix: Use correct scripts field for post_remove script

* test: Implement archlinux acceptance tests

* test: Add archlinux to acceptance_test.go

* test: Add archlinux to github test matrix

* test: Use updated build.yml from main branch

* Fix ConventionalExtension() for apk

* fix: Take epoch value into account

* fix: Add arm5 and arm6 architectures

Co-authored-by: Dj Gilcrease <digitalxero@gmail.com>
Co-authored-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
2022-10-15 14:54:36 -03:00

108 lines
2.4 KiB
Go

package cmd
import (
"errors"
"fmt"
"os"
"path"
"path/filepath"
"github.com/goreleaser/nfpm/v2"
"github.com/spf13/cobra"
)
type packageCmd struct {
cmd *cobra.Command
config string
target string
packager string
}
func newPackageCmd() *packageCmd {
root := &packageCmd{}
cmd := &cobra.Command{
Use: "package",
Aliases: []string{"pkg", "p"},
Short: "Creates a package based on the given config file and flags",
SilenceUsage: true,
SilenceErrors: true,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return doPackage(root.config, root.target, root.packager)
},
}
cmd.Flags().StringVarP(&root.config, "config", "f", "nfpm.yaml", "config file to be used")
cmd.Flags().StringVarP(&root.target, "target", "t", "", "where to save the generated package (filename, folder or empty for current folder)")
cmd.Flags().StringVarP(&root.packager, "packager", "p", "", "which packager implementation to use [apk|deb|rpm|archlinux]")
root.cmd = cmd
return root
}
var errInsufficientParams = errors.New("a packager must be specified if target is a directory or blank")
// nolint:funlen
func doPackage(configPath, target, packager string) error {
targetIsADirectory := false
stat, err := os.Stat(target)
if err == nil && stat.IsDir() {
targetIsADirectory = true
}
if packager == "" {
ext := filepath.Ext(target)
if targetIsADirectory || ext == "" {
return errInsufficientParams
}
packager = ext[1:]
fmt.Println("guessing packager from target file extension...")
}
config, err := nfpm.ParseFile(configPath)
if err != nil {
return err
}
info, err := config.Get(packager)
if err != nil {
return err
}
info = nfpm.WithDefaults(info)
fmt.Printf("using %s packager...\n", packager)
pkg, err := nfpm.Get(packager)
if err != nil {
return err
}
if target == "" {
// if no target was specified create a package in
// current directory with a conventional file name
target = pkg.ConventionalFileName(info)
} else if targetIsADirectory {
// if a directory was specified as target, create
// a package with conventional file name there
target = path.Join(target, pkg.ConventionalFileName(info))
}
f, err := os.Create(target)
if err != nil {
return err
}
info.Target = target
err = pkg.Package(info, f)
_ = f.Close()
if err != nil {
os.Remove(target)
return err
}
fmt.Printf("created package: %s\n", target)
return nil
}