Skip to content

Commit

Permalink
feat: set file info for binaries inside archives (#3618)
Browse files Browse the repository at this point in the history
closes #3582

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
  • Loading branch information
caarlos0 committed Dec 14, 2022
1 parent f05b211 commit 9370676
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .goreleaser.yaml
Expand Up @@ -139,6 +139,9 @@ archives:
format_overrides:
- goos: windows
format: zip
builds_info:
group: root
owner: root
files:
- README.md
- LICENSE.md
Expand Down
1 change: 1 addition & 0 deletions internal/pipe/archive/archive.go
Expand Up @@ -206,6 +206,7 @@ func doCreate(ctx *context.Context, arch config.Archive, binaries []*artifact.Ar
if err := a.Add(config.File{
Source: binary.Path,
Destination: dst,
Info: arch.BuildsInfo,
}); err != nil {
return fmt.Errorf("failed to add: '%s' -> '%s': %w", binary.Path, dst, err)
}
Expand Down
34 changes: 32 additions & 2 deletions internal/pipe/archive/archive_test.go
Expand Up @@ -82,8 +82,12 @@ func TestRunPipe(t *testing.T) {
ProjectName: "foobar",
Archives: []config.Archive{
{
ID: "myid",
Builds: []string{"default"},
ID: "myid",
Builds: []string{"default"},
BuildsInfo: config.FileInfo{
Owner: "root",
Group: "root",
},
NameTemplate: defaultNameTemplate,
StripParentBinaryFolder: dets.Strip,
Files: []config.File{
Expand Down Expand Up @@ -233,6 +237,11 @@ func TestRunPipe(t *testing.T) {
},
tarFiles(t, filepath.Join(dist, name)),
)

header := tarInfo(t, filepath.Join(dist, name), expectBin)
require.Equal(t, "root", header.Uname)
require.Equal(t, "root", header.Gname)

}
}
if format == "zip" {
Expand Down Expand Up @@ -363,6 +372,27 @@ func zipFiles(t *testing.T, path string) []string {
return paths
}

func tarInfo(t *testing.T, path, name string) *tar.Header {
t.Helper()
f, err := os.Open(path)
require.NoError(t, err)
defer f.Close()
gr, err := gzip.NewReader(f)
require.NoError(t, err)
defer gr.Close()
r := tar.NewReader(gr)
for {
next, err := r.Next()
if err == io.EOF {
break
}
if next.Name == name {
return next
}
}
return nil
}

func tarFiles(t *testing.T, path string) []string {
t.Helper()
f, err := os.Open(path)
Expand Down
1 change: 1 addition & 0 deletions pkg/config/config.go
Expand Up @@ -480,6 +480,7 @@ type UniversalBinary struct {
type Archive struct {
ID string `yaml:"id,omitempty" json:"id,omitempty"`
Builds []string `yaml:"builds,omitempty" json:"builds,omitempty"`
BuildsInfo FileInfo `yaml:"builds_info,omitempty" json:"builds_info,omitempty"`
NameTemplate string `yaml:"name_template,omitempty" json:"name_template,omitempty"`
Replacements map[string]string `yaml:"replacements,omitempty" json:"replacements,omitempty"` // Deprecated: use templates instead
Format string `yaml:"format,omitempty" json:"format,omitempty"`
Expand Down
13 changes: 13 additions & 0 deletions www/docs/customization/archive.md
Expand Up @@ -40,6 +40,19 @@ archives:
# - `{{ .Binary }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ with .Arm }}v{{ . }}{{ end }}{{ with .Mips }}_{{ . }}{{ end }}{{ if not (eq .Amd64 "v1") }}{{ .Amd64 }}{{ end }}`
name_template: "{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}"

# Sets the given file info to all the binaries included from the `builds`.
#
# Default is to use the actual binary properties.
#
# Since: v1.14.0.
builds_info:
group: root
owner: root
mode: 0644
# format is `time.RFC3339Nano`
mtime: 2008-01-02T15:04:05Z


# Set this to true if you want all files in the archive to be in a single directory.
# If set to true and you extract the archive 'goreleaser_Linux_arm64.tar.gz',
# you'll get a folder 'goreleaser_Linux_arm64'.
Expand Down
3 changes: 3 additions & 0 deletions www/docs/static/schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9370676

Please sign in to comment.