Skip to content

Commit

Permalink
refactor: split function
Browse files Browse the repository at this point in the history
  • Loading branch information
suzuki-shunsuke committed Sep 22, 2024
1 parent 4a72978 commit 79da05c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
9 changes: 6 additions & 3 deletions pkg/installpackage/checksum.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,16 @@ func (is *Installer) dlAndExtractChecksum(ctx context.Context, logE *logrus.Entr
}
}

if err := is.verifyChecksumWithMinisign(ctx, logE, pkg, assetName, tempFilePath, b); err != nil {
if err := is.verifyChecksumWithMinisign(ctx, logE, pkg, tempFilePath, b); err != nil {
return "", err
}

return checksum.GetChecksum(logE, assetName, string(b), pkg.PackageInfo.Checksum) //nolint:wrapcheck
}

func (is *Installer) verifyChecksumWithMinisign(ctx context.Context, logE *logrus.Entry, pkg *config.Package, assetName, tempFilePath string, b []byte) error {
// b is a content of the checksum file.
// tempFilePath is a path of the checksum file.
func (is *Installer) verifyChecksumWithMinisign(ctx context.Context, logE *logrus.Entry, pkg *config.Package, tempFilePath string, b []byte) error {
ms := pkg.PackageInfo.Checksum.GetMinisign()
if !ms.GetEnabled() {
return nil
Expand All @@ -88,7 +90,8 @@ func (is *Installer) verifyChecksumWithMinisign(ctx context.Context, logE *logru
return fmt.Errorf("write a checksum to a temporary file: %w", err)
}
}
art := pkg.TemplateArtifact(is.runtime, assetName)

art := pkg.TemplateArtifact(is.runtime, "")
logE.Info("verifing a checksum file with Minisign")
if err := is.minisignInstaller.install(ctx, logE); err != nil {
return err
Expand Down
24 changes: 16 additions & 8 deletions pkg/minisign/verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,33 @@ type ParamVerify struct {
}

func (v *Verifier) Verify(ctx context.Context, logE *logrus.Entry, rt *runtime.Runtime, m *registry.Minisign, art *template.Artifact, file *download.File, param *ParamVerify) error {
sigFile, err := v.downloadSignature(ctx, logE, rt, m, art, file)
if err != nil {
return err
}
defer v.fs.Remove(sigFile) //nolint:errcheck
return v.exe.Verify(ctx, logE, param, sigFile) //nolint:wrapcheck
}

func (v *Verifier) downloadSignature(ctx context.Context, logE *logrus.Entry, rt *runtime.Runtime, m *registry.Minisign, art *template.Artifact, file *download.File) (string, error) {
f, err := download.ConvertDownloadedFileToFile(m.ToDownloadedFile(), file, rt, art)
if err != nil {
return err //nolint:wrapcheck
return "", err //nolint:wrapcheck
}

rc, _, err := v.downloader.ReadCloser(ctx, logE, f)
if err != nil {
return fmt.Errorf("download a Minisign signature: %w", err)
return "", fmt.Errorf("download a Minisign signature: %w", err)
}
defer rc.Close()

signatureFile, err := afero.TempFile(v.fs, "", "")
if err != nil {
return fmt.Errorf("create a temporary file: %w", err)
return "", fmt.Errorf("create a temporary file: %w", err)
}
defer signatureFile.Close()
defer v.fs.Remove(signatureFile.Name()) //nolint:errcheck
defer signatureFile.Close() //nolint:errcheck

Check failure on line 60 in pkg/minisign/verifier.go

View workflow job for this annotation

GitHub Actions / test / test

directive `//nolint:errcheck` is unused for linter "errcheck" (nolintlint)
if _, err := io.Copy(signatureFile, rc); err != nil {
return fmt.Errorf("copy a signature to a temporary file: %w", err)
return signatureFile.Name(), fmt.Errorf("copy a signature to a temporary file: %w", err)
}

return v.exe.Verify(ctx, logE, param, signatureFile.Name()) //nolint:wrapcheck
return signatureFile.Name(), nil
}

0 comments on commit 79da05c

Please sign in to comment.