Skip to content

Commit

Permalink
refactor: fix lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
suzuki-shunsuke committed Jan 4, 2025
1 parent a8872af commit c6ff999
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
16 changes: 11 additions & 5 deletions pkg/unarchive/archives.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ import (
"github.com/mholt/archives"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
"github.com/suzuki-shunsuke/logrus-error/logerr"
)

type handler struct {
fs afero.Fs
dest string
filename string
logE *logrus.Entry
}

const readOnlyPerm = 0o200
Expand All @@ -29,7 +31,7 @@ func allowWrite(fs afero.Fs, path string) (func() error, error) {
}

if originalMode.Mode().Perm()&readOnlyPerm != 0 {
return nil, nil
return nil, nil //nolint:nilnil
}

if err := os.Chmod(path, originalMode.Mode()|readOnlyPerm); err != nil {
Expand All @@ -45,7 +47,7 @@ func (h *handler) HandleFile(_ context.Context, f archives.FileInfo) error {

parentDir := filepath.Dir(dstPath)
if err := osfile.MkdirAll(h.fs, parentDir); err != nil {
return err
return fmt.Errorf("create a directory: %w", err)
}

if f.IsDir() {
Expand All @@ -64,7 +66,11 @@ func (h *handler) HandleFile(_ context.Context, f archives.FileInfo) error {
return err
}
if fn != nil {
defer fn()
defer func() {
if err := fn(); err != nil {
logerr.WithError(h.logE, err).Warn("failed to restore the original permission")
}
}()
}

reader, err := f.Open()
Expand All @@ -85,7 +91,7 @@ func (h *handler) HandleFile(_ context.Context, f archives.FileInfo) error {
return nil
}

func (h *handler) Unarchive(ctx context.Context, logE *logrus.Entry, src *File) error {
func (h *handler) Unarchive(ctx context.Context, _ *logrus.Entry, src *File) error {
tempFilePath, err := src.Body.Path()
if err != nil {
return fmt.Errorf("get a temporary file path: %w", err)
Expand Down Expand Up @@ -124,7 +130,7 @@ func (h *handler) unarchive(ctx context.Context, tarball string) error {
func (h *handler) decompress(input io.Reader, decomp archives.Decompressor) error {
rc, err := decomp.OpenReader(input)
if err != nil {
return err
return fmt.Errorf("open a decompressed file: %w", err)
}
defer rc.Close()
if err := osfile.MkdirAll(h.fs, h.dest); err != nil {
Expand Down
22 changes: 8 additions & 14 deletions pkg/unarchive/unarchive.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package unarchive
import (
"context"
"errors"
"fmt"
"io"
"path/filepath"

Expand Down Expand Up @@ -42,12 +41,7 @@ func New(executor Executor, fs afero.Fs) *Unarchiver {
}

func (u *Unarchiver) Unarchive(ctx context.Context, logE *logrus.Entry, src *File, dest string) error {
arc, err := u.getUnarchiver(src, dest)
if err != nil {
return fmt.Errorf("get the unarchiver or decompressor by the file extension: %w", err)
}

return arc.Unarchive(ctx, logE, src) //nolint:wrapcheck
return u.getUnarchiver(src, dest).Unarchive(ctx, logE, src) //nolint:wrapcheck
}

func IsUnarchived(archiveType, assetName string) bool {
Expand All @@ -61,46 +55,46 @@ func IsUnarchived(archiveType, assetName string) bool {
return ext == "" || ext == ".exe"
}

func (u *Unarchiver) getUnarchiver(src *File, dest string) (coreUnarchiver, error) {
func (u *Unarchiver) getUnarchiver(src *File, dest string) coreUnarchiver {
filename := filepath.Base(src.Filename)
if IsUnarchived(src.Type, filename) {
return &rawUnarchiver{
dest: filepath.Join(dest, filename),
fs: u.fs,
}, nil
}
}
if src.Type == "dmg" {
return &dmgUnarchiver{
dest: dest,
executor: u.executor,
fs: u.fs,
}, nil
}
}
if src.Type == "pkg" {
return &pkgUnarchiver{
dest: dest,
executor: u.executor,
fs: u.fs,
}, nil
}
}
switch ext := filepath.Ext(filename); ext {
case ".dmg":
return &dmgUnarchiver{
dest: dest,
executor: u.executor,
fs: u.fs,
}, nil
}
case ".pkg":
return &pkgUnarchiver{
dest: dest,
executor: u.executor,
fs: u.fs,
}, nil
}
}

return &handler{
fs: u.fs,
dest: dest,
filename: filename,
}, nil
}
}

0 comments on commit c6ff999

Please sign in to comment.