1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-10-30 23:58:09 +02:00

refactor: gio.Chtimes (#4191)

we're repeating this quite a bit

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
This commit is contained in:
Carlos Alexandro Becker
2023-07-15 16:33:40 -03:00
committed by GitHub
parent 3c7fca7504
commit b9a08c4dc9
4 changed files with 84 additions and 27 deletions

View File

@@ -8,13 +8,12 @@ import (
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/caarlos0/log"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/builders/buildtarget"
"github.com/goreleaser/goreleaser/internal/gio"
"github.com/goreleaser/goreleaser/internal/tmpl"
api "github.com/goreleaser/goreleaser/pkg/build"
"github.com/goreleaser/goreleaser/pkg/config"
@@ -205,20 +204,12 @@ func (*Builder) Build(ctx *context.Context, build config.Build, options api.Opti
return fmt.Errorf("failed to build for %s: %w", options.Target, err)
}
if build.ModTimestamp != "" {
modTimestamp, err := tmpl.New(ctx).WithEnvS(env).WithArtifact(a).Apply(build.ModTimestamp)
if err != nil {
return err
}
modUnix, err := strconv.ParseInt(modTimestamp, 10, 64)
if err != nil {
return err
}
modTime := time.Unix(modUnix, 0)
err = os.Chtimes(options.Path, modTime, modTime)
if err != nil {
return fmt.Errorf("failed to change times for %s: %w", options.Target, err)
}
modTimestamp, err := tmpl.New(ctx).WithEnvS(env).WithArtifact(a).Apply(build.ModTimestamp)
if err != nil {
return err
}
if err := gio.Chtimes(options.Path, modTimestamp); err != nil {
return err
}
ctx.Artifacts.Add(a)

24
internal/gio/chtimes.go Normal file
View File

@@ -0,0 +1,24 @@
package gio
import (
"fmt"
"os"
"strconv"
"time"
)
// Chtimes applies the given ts to the given path.
func Chtimes(path, ts string) error {
if ts == "" {
return nil
}
modUnix, err := strconv.ParseInt(ts, 10, 64)
if err != nil {
return fmt.Errorf("chtimes: %s: %w", path, err)
}
modTime := time.Unix(modUnix, 0)
if err := os.Chtimes(path, modTime, modTime); err != nil {
return fmt.Errorf("chtimes: %s: %w", path, err)
}
return nil
}

View File

@@ -0,0 +1,50 @@
package gio
import (
"fmt"
"os"
"path/filepath"
"strconv"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestChtimes(t *testing.T) {
modTime := time.Now().AddDate(-1, 0, 0).Round(1 * time.Second).UTC()
path := filepath.Join(t.TempDir(), "file")
require.NoError(t, os.WriteFile(path, nil, 0o644))
require.NoError(t, Chtimes(path, fmt.Sprintf("%d", modTime.Unix())))
stat, err := os.Stat(path)
require.NoError(t, err)
require.True(t, modTime.Equal(stat.ModTime()))
}
func TestChtimesFileDoesNotExist(t *testing.T) {
modTime := time.Now().AddDate(-1, 0, 0).Round(1 * time.Second).UTC()
path := filepath.Join(t.TempDir(), "file")
require.ErrorIs(t, Chtimes(path, fmt.Sprintf("%d", modTime.Unix())), os.ErrNotExist)
}
func TestChtimesInvalidTS(t *testing.T) {
path := filepath.Join(t.TempDir(), "file")
require.NoError(t, os.WriteFile(path, nil, 0o644))
require.ErrorIs(t, Chtimes(path, "fake"), strconv.ErrSyntax)
}
func TestChtimesEmpty(t *testing.T) {
modTime := time.Now().AddDate(-1, 0, 0).Round(1 * time.Second).UTC()
path := filepath.Join(t.TempDir(), "file")
require.NoError(t, os.WriteFile(path, nil, 0o644))
require.NoError(t, Chtimes(path, ""))
stat, err := os.Stat(path)
require.NoError(t, err)
require.False(t, modTime.Equal(stat.ModTime()))
}

View File

@@ -7,12 +7,11 @@ import (
"fmt"
"os"
"path/filepath"
"strconv"
"time"
"github.com/caarlos0/go-shellwords"
"github.com/caarlos0/log"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/gio"
"github.com/goreleaser/goreleaser/internal/ids"
"github.com/goreleaser/goreleaser/internal/pipe"
"github.com/goreleaser/goreleaser/internal/semerrgroup"
@@ -226,15 +225,8 @@ func makeUniversalBinary(ctx *context.Context, opts *build.Options, unibin confi
return fmt.Errorf("failed to close file: %w", err)
}
if unibin.ModTimestamp != "" {
modUnix, err := strconv.ParseInt(unibin.ModTimestamp, 10, 64)
if err != nil {
return err
}
modTime := time.Unix(modUnix, 0)
if err := os.Chtimes(path, modTime, modTime); err != nil {
return fmt.Errorf("failed to change times for %s: %w", path, err)
}
if err := gio.Chtimes(path, unibin.ModTimestamp); err != nil {
return err
}
extra := map[string]interface{}{}