Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use goversion lib #685

Merged
merged 3 commits into from Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .goreleaser.yml
Expand Up @@ -27,7 +27,7 @@ builds:
flags:
- -trimpath
ldflags:
- -s -w -X main.version={{ .Version }} -X main.commit={{ .Commit }} -X main.date={{ .CommitDate }} -X main.builtBy=goreleaser
- -s -w -X main.version={{ .Version }} -X main.commit={{ .Commit }} -X main.date={{ .CommitDate }} -X main.builtBy=goreleaser -X main.treeState={{ .IsGitDirty }}

dockers:
- image_templates:
Expand Down
2 changes: 1 addition & 1 deletion README.md
@@ -1,7 +1,7 @@
<p align="center">
<img alt="GoReleaser Logo" src="https://avatars2.githubusercontent.com/u/24697112?v=3&s=200" height="140" />
<h3 align="center">nFPM</h3>
<p align="center">nFPM is a simple, 0-dependencies, deb, rpm and apk packager.</p>
<p align="center">nFPM is a simple and 0-dependencies deb, rpm, apk and arch linux packager written in Go</p>
<p align="center">
<a href="https://github.com/goreleaser/nfpm/releases/latest"><img alt="Release" src="https://img.shields.io/github/release/goreleaser/nfpm.svg?style=for-the-badge"></a>
<a href="/LICENSE.md"><img alt="Software License" src="https://img.shields.io/badge/license-MIT-brightgreen.svg?style=for-the-badge"></a>
Expand Down
5 changes: 5 additions & 0 deletions cmd/nfpm/art.txt
@@ -0,0 +1,5 @@
_____ ____ __ __
_ __ | ___| _ \| \/ |
| '_ \| |_ | |_) | |\/| |
| | | | _| | __/| | | |
|_| |_|_| |_| |_| |_|
58 changes: 36 additions & 22 deletions cmd/nfpm/main.go
@@ -1,42 +1,56 @@
package main

import (
"fmt"
"os"
"runtime/debug"

_ "embed"

goversion "github.com/caarlos0/go-version"
"github.com/goreleaser/nfpm/v2/internal/cmd"
)

// nolint: gochecknoglobals
var (
version = "dev"
commit = ""
date = ""
builtBy = ""
version = "dev"
treeState = ""
commit = ""
date = ""
builtBy = ""
)

const website = "https://nfpm.goreleaser.com"

//go:embed art.txt
var asciiArt string

func main() {
cmd.Execute(
buildVersion(version, commit, date, builtBy),
buildVersion(version, commit, date, builtBy, treeState),
os.Exit,
os.Args[1:],
)
}

func buildVersion(version, commit, date, builtBy string) string {
result := version
if commit != "" {
result = fmt.Sprintf("%s\ncommit: %s", result, commit)
}
if date != "" {
result = fmt.Sprintf("%s\nbuilt at: %s", result, date)
}
if builtBy != "" {
result = fmt.Sprintf("%s\nbuilt by: %s", result, builtBy)
}
if info, ok := debug.ReadBuildInfo(); ok && info.Main.Sum != "" {
result = fmt.Sprintf("%s\nmodule version: %s, checksum: %s", result, info.Main.Version, info.Main.Sum)
}
return result
func buildVersion(version, commit, date, builtBy, treeState string) goversion.Info {
return goversion.GetVersionInfo(
goversion.WithAppDetails("nfpm", "a simple and 0-dependencies deb, rpm, apk and arch linux packager written in Go", website),
goversion.WithASCIIName(asciiArt),
func(i *goversion.Info) {
if commit != "" {
i.GitCommit = commit
}
if treeState != "" {
i.GitTreeState = treeState
}
if date != "" {
i.BuildDate = date
}
if version != "" {
i.GitVersion = version
}
if builtBy != "" {
i.BuiltBy = builtBy
}
},
)
}
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -9,6 +9,7 @@ require (
github.com/ProtonMail/gopenpgp/v2 v2.7.1
github.com/blakesmith/ar v0.0.0-20190502131153-809d4375e1fb
github.com/caarlos0/go-rpmutils v0.2.1-0.20211112020245-2cd62ff89b11
github.com/caarlos0/go-version v0.1.1
github.com/cavaliergopher/cpio v1.0.1
github.com/google/go-cmp v0.5.9
github.com/goreleaser/chglog v0.5.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Expand Up @@ -29,6 +29,8 @@ github.com/bwesterb/go-ristretto v1.2.0/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7N
github.com/bwesterb/go-ristretto v1.2.3/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0=
github.com/caarlos0/go-rpmutils v0.2.1-0.20211112020245-2cd62ff89b11 h1:IRrDwVlWQr6kS1U8/EtyA1+EHcc4yl8pndcqXWrEamg=
github.com/caarlos0/go-rpmutils v0.2.1-0.20211112020245-2cd62ff89b11/go.mod h1:je2KZ+LxaCNvCoKg32jtOIULcFogJKcL1ZWUaIBjKj0=
github.com/caarlos0/go-version v0.1.1 h1:1bikKHkGGVIIxqCmufhSSs3hpBScgHGacrvsi8FuIfc=
github.com/caarlos0/go-version v0.1.1/go.mod h1:Ze5Qx4TsBBi5FyrSKVg1Ibc44KGV/llAaKGp86oTwZ0=
github.com/caarlos0/testfs v0.4.4 h1:3PHvzHi5Lt+g332CiShwS8ogTgS3HjrmzZxCm6JCDr8=
github.com/caarlos0/testfs v0.4.4/go.mod h1:bRN55zgG4XCUVVHZCeU+/Tz1Q6AxEJOEJTliBy+1DMk=
github.com/cavaliergopher/cpio v1.0.1 h1:KQFSeKmZhv0cr+kawA3a0xTQCU4QxXF1vhU7P7av2KM=
Expand Down
12 changes: 7 additions & 5 deletions internal/cmd/root.go
Expand Up @@ -3,14 +3,15 @@ package cmd
import (
"fmt"

goversion "github.com/caarlos0/go-version"
_ "github.com/goreleaser/nfpm/v2/apk" // apk packager
_ "github.com/goreleaser/nfpm/v2/arch" // archlinux packager
_ "github.com/goreleaser/nfpm/v2/deb" // deb packager
_ "github.com/goreleaser/nfpm/v2/rpm" // rpm packager
"github.com/spf13/cobra"
)

func Execute(version string, exit func(int), args []string) {
func Execute(version goversion.Info, exit func(int), args []string) {
newRootCmd(version, exit).Execute(args)
}

Expand All @@ -28,20 +29,21 @@ func (cmd *rootCmd) Execute(args []string) {
}
}

func newRootCmd(version string, exit func(int)) *rootCmd {
func newRootCmd(version goversion.Info, exit func(int)) *rootCmd {
root := &rootCmd{
exit: exit,
}
cmd := &cobra.Command{
Use: "nfpm",
Short: "Packages apps on RPM, Deb and APK formats based on a YAML configuration file",
Long: `nFPM is a simple, 0-dependencies, deb, rpm and apk packager.`,
Version: version,
Short: "Packages apps on RPM, Deb, APK and Arch Linux formats based on a YAML configuration file",
Long: `nFPM is a simple and 0-dependencies deb, rpm, apk and arch linux packager written in Go.`,
Version: version.String(),
SilenceUsage: true,
SilenceErrors: true,
Args: cobra.NoArgs,
ValidArgsFunction: cobra.NoFileCompletions,
}
cmd.SetVersionTemplate("{{.Version}}")

cmd.AddCommand(
newInitCmd().cmd,
Expand Down