Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
frantjc committed Jan 16, 2024
1 parent bc4f9a2 commit 2cb9b30
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 29 deletions.
8 changes: 3 additions & 5 deletions command/sindri.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package command

import (
"archive/tar"
"compress/gzip"
"context"
"fmt"
Expand Down Expand Up @@ -113,6 +114,7 @@ func NewSindri() *cobra.Command {
if err != nil {
return err
}
defer moddedValheimTar.Close()

runDir, err := os.MkdirTemp(state, "")
if err != nil {
Expand All @@ -121,11 +123,7 @@ func NewSindri() *cobra.Command {

log.Info("installing Valheim to " + runDir)

if err = xtar.Extract(moddedValheimTar, runDir); err != nil {
return err
}

if err = moddedValheimTar.Close(); err != nil {
if err = xtar.Extract(tar.NewReader(moddedValheimTar), runDir); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/adrg/xdg v0.4.0
github.com/frantjc/go-ingress v0.1.1
github.com/frantjc/go-steamcmd v0.0.0-20240112014424-08f6a8624b2d
github.com/frantjc/x v0.0.0-20240110023118-cfbb48930f03
github.com/frantjc/x v0.0.0-20240116045108-479cf61015f9
github.com/go-logr/logr v1.4.1
github.com/go-logr/zapr v1.3.0
github.com/google/go-containerregistry v0.17.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ github.com/frantjc/go-steamcmd v0.0.0-20240112014424-08f6a8624b2d h1:MuGx3DH9jy/
github.com/frantjc/go-steamcmd v0.0.0-20240112014424-08f6a8624b2d/go.mod h1:g/OfOB32TF9mlBL+4OIgHST0LhV22drWOFNXng4NHwg=
github.com/frantjc/x v0.0.0-20240110023118-cfbb48930f03 h1:QntnRxeOe/z31ss8g8HJfQ4MAxUPmuS8BUYdgUKuVu0=
github.com/frantjc/x v0.0.0-20240110023118-cfbb48930f03/go.mod h1:jzQwn2fyR0LRUO37WetnmJkZWmVR2GjhCD1QuaAJSPo=
github.com/frantjc/x v0.0.0-20240116045108-479cf61015f9 h1:Hkp6KIoLLq/QEWjCOoToKYa45NXbAvgaK5Om4OEv7k8=
github.com/frantjc/x v0.0.0-20240116045108-479cf61015f9/go.mod h1:jzQwn2fyR0LRUO37WetnmJkZWmVR2GjhCD1QuaAJSPo=
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/zapr v1.3.0 h1:XGdV8XW8zdwFiwOA2Dryh1gj2KRQyOOoNmBy4EplIcQ=
Expand Down
61 changes: 53 additions & 8 deletions sindri.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ import (
"io"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"

"github.com/frantjc/go-steamcmd"
"github.com/frantjc/sindri/thunderstore"
"github.com/frantjc/sindri/valheim"
xcontainerregistry "github.com/frantjc/sindri/x/containerregistry"
xtar "github.com/frantjc/x/archive/tar"
xzip "github.com/frantjc/x/archive/zip"
xio "github.com/frantjc/x/io"
xslice "github.com/frantjc/x/slice"
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
Expand Down Expand Up @@ -125,6 +127,29 @@ func (s *Sindri) Mods() ([]thunderstore.Package, error) {
return pkgs, nil
}

type readCloser struct {
io.Reader
io.Closer
}

func reproducibleBuildLayerFromDir(dir string) (v1.Layer, error) {
return tarball.LayerFromOpener(func() (io.ReadCloser, error) {
var (
rc1 = xtar.Compress(dir)
rc2 = xtar.ModifyHeaders(tar.NewReader(rc1), func(h *tar.Header) {
h.ModTime = sourceDateEpoch
})
)

return &readCloser{
Reader: rc2,
Closer: xio.CloserFunc(func() error {
return errors.Join(rc2.Close(), rc1.Close())
}),
}, nil
})
}

// AppUpdate uses `steamcmd` to installed or update
// the game that *Sindri is managing.
func (s *Sindri) AppUpdate(ctx context.Context) error {
Expand Down Expand Up @@ -153,7 +178,7 @@ func (s *Sindri) AppUpdate(ctx context.Context) error {
return err
}

steamAppLayer, err := xcontainerregistry.LayerFromDir(steamcmdForceInstallDir)
steamAppLayer, err := reproducibleBuildLayerFromDir(steamcmdForceInstallDir)
if err != nil {
return err
}
Expand Down Expand Up @@ -246,9 +271,7 @@ func (s *Sindri) AddMods(ctx context.Context, mods ...string) error {
return err
}

modLayer, err := tarball.LayerFromOpener(func() (io.ReadCloser, error) {
return xtar.Compress(pkgUnzipDir), nil
})
modLayer, err := reproducibleBuildLayerFromDir(pkgUnzipDir)
if err != nil {
return err
}
Expand Down Expand Up @@ -353,6 +376,20 @@ func (s *Sindri) RemoveMods(_ context.Context, mods ...string) error {
return s.save()
}

var sourceDateEpoch = time.Date(1970, time.January, 1, 0, 0, 0, 0, time.UTC).
Add(func() time.Duration {
sourceDateEpoch := os.Getenv("SOURCE_DATE_EPOCH")
if sourceDateEpoch == "" {
return 0
}

if seconds, err := strconv.Atoi(sourceDateEpoch); err == nil {
return time.Second * time.Duration(seconds)
}

return 0
}())

// Extract returns an io.ReadCloser of a tarball
// containing the files of the game and the given mods.
func (s *Sindri) Extract(mods ...string) (io.ReadCloser, error) {
Expand All @@ -376,7 +413,11 @@ func (s *Sindri) Extract(mods ...string) (io.ReadCloser, error) {
return nil, err
}

return mutate.Extract(img), nil
now := time.Now()

return xtar.ModifyHeaders(tar.NewReader(mutate.Extract(img)), func(h *tar.Header) {
h.ModTime = now
}), nil
}

// ExtractMods returns an io.ReadCloser containing a tarball
Expand All @@ -395,7 +436,11 @@ func (s *Sindri) ExtractMods(mods ...string) (io.ReadCloser, error) {
return nil, err
}

return mutate.Extract(img), nil
now := time.Now()

return xtar.ModifyHeaders(tar.NewReader(mutate.Extract(img)), func(h *tar.Header) {
h.ModTime = now
}), nil
}

func (s *Sindri) save() error {
Expand All @@ -415,7 +460,7 @@ func (s *Sindri) save() error {
Name: s.metadataName(),
Size: int64(len(b)),
Mode: 0644,
ModTime: xtar.ModTime,
ModTime: sourceDateEpoch,
}); err != nil {
return nil, err
}
Expand Down
15 changes: 0 additions & 15 deletions x/containerregistry/layer.go

This file was deleted.

0 comments on commit 2cb9b30

Please sign in to comment.