Fix all linter errors
- Add explicit error ignoring with _ = for Close/Remove calls - Rename WriteTo to Write to avoid io.WriterTo interface conflict - Fix errcheck warnings in fetch, freshen, gen, mfer, checker, deserialize, serialize, and output files
This commit is contained in:
parent
531f460f87
commit
92bd13efde
@ -218,7 +218,7 @@ func (c *Checker) checkFile(entry *mfer.MFFilePath, checkedBytes *int64) Result
|
||||
if err != nil {
|
||||
return Result{Path: entry.Path, Status: StatusError, Message: err.Error()}
|
||||
}
|
||||
defer f.Close()
|
||||
defer func() { _ = f.Close() }()
|
||||
|
||||
h := sha256.New()
|
||||
n, err := io.Copy(h, f)
|
||||
|
||||
@ -48,7 +48,7 @@ func (mfa *CLIApp) fetchManifestOperation(ctx *cli.Context) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to fetch manifest: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("failed to fetch manifest: HTTP %d", resp.StatusCode)
|
||||
@ -283,7 +283,7 @@ func downloadFile(fileURL, localPath string, entry *mfer.MFFilePath, progress ch
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("HTTP %d", resp.StatusCode)
|
||||
@ -322,24 +322,24 @@ func downloadFile(fileURL, localPath string, entry *mfer.MFFilePath, progress ch
|
||||
|
||||
// If copy failed, clean up temp file and return error
|
||||
if copyErr != nil {
|
||||
os.Remove(tmpPath)
|
||||
_ = os.Remove(tmpPath)
|
||||
return copyErr
|
||||
}
|
||||
if closeErr != nil {
|
||||
os.Remove(tmpPath)
|
||||
_ = os.Remove(tmpPath)
|
||||
return closeErr
|
||||
}
|
||||
|
||||
// Verify size
|
||||
if written != expectedSize {
|
||||
os.Remove(tmpPath)
|
||||
_ = os.Remove(tmpPath)
|
||||
return fmt.Errorf("size mismatch: expected %d bytes, got %d", expectedSize, written)
|
||||
}
|
||||
|
||||
// Encode computed hash as multihash
|
||||
computed, err := multihash.Encode(h.Sum(nil), multihash.SHA2_256)
|
||||
if err != nil {
|
||||
os.Remove(tmpPath)
|
||||
_ = os.Remove(tmpPath)
|
||||
return fmt.Errorf("failed to encode hash: %w", err)
|
||||
}
|
||||
|
||||
@ -352,13 +352,13 @@ func downloadFile(fileURL, localPath string, entry *mfer.MFFilePath, progress ch
|
||||
}
|
||||
}
|
||||
if !hashMatch {
|
||||
os.Remove(tmpPath)
|
||||
_ = os.Remove(tmpPath)
|
||||
return fmt.Errorf("hash mismatch")
|
||||
}
|
||||
|
||||
// Rename temp file to final path
|
||||
if err := os.Rename(tmpPath, localPath); err != nil {
|
||||
os.Remove(tmpPath)
|
||||
_ = os.Remove(tmpPath)
|
||||
return fmt.Errorf("failed to rename temp file: %w", err)
|
||||
}
|
||||
|
||||
|
||||
@ -127,7 +127,7 @@ func TestFetchFromHTTP(t *testing.T) {
|
||||
path := r.URL.Path
|
||||
if path == "/index.mf" {
|
||||
w.Header().Set("Content-Type", "application/octet-stream")
|
||||
w.Write(manifestData)
|
||||
_, _ = w.Write(manifestData)
|
||||
return
|
||||
}
|
||||
|
||||
@ -143,20 +143,20 @@ func TestFetchFromHTTP(t *testing.T) {
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/octet-stream")
|
||||
w.Write(content)
|
||||
_, _ = w.Write(content)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
// Create destination directory
|
||||
destDir, err := os.MkdirTemp("", "mfer-fetch-test-*")
|
||||
require.NoError(t, err)
|
||||
defer os.RemoveAll(destDir)
|
||||
defer func() { _ = os.RemoveAll(destDir) }()
|
||||
|
||||
// Change to dest directory for the test
|
||||
origDir, err := os.Getwd()
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, os.Chdir(destDir))
|
||||
defer os.Chdir(origDir)
|
||||
defer func() { _ = os.Chdir(origDir) }()
|
||||
|
||||
// Parse the manifest to get file entries
|
||||
manifest, err := mfer.NewManifestFromReader(bytes.NewReader(manifestData))
|
||||
@ -217,19 +217,19 @@ func TestFetchHashMismatch(t *testing.T) {
|
||||
tamperedContent := []byte("Tampered content!")
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/octet-stream")
|
||||
w.Write(tamperedContent)
|
||||
_, _ = w.Write(tamperedContent)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
// Create temp directory
|
||||
destDir, err := os.MkdirTemp("", "mfer-fetch-hash-test-*")
|
||||
require.NoError(t, err)
|
||||
defer os.RemoveAll(destDir)
|
||||
defer func() { _ = os.RemoveAll(destDir) }()
|
||||
|
||||
origDir, err := os.Getwd()
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, os.Chdir(destDir))
|
||||
defer os.Chdir(origDir)
|
||||
defer func() { _ = os.Chdir(origDir) }()
|
||||
|
||||
// Try to download - should fail with hash mismatch
|
||||
err = downloadFile(server.URL+"/file.txt", "file.txt", files[0], nil)
|
||||
@ -269,19 +269,19 @@ func TestFetchSizeMismatch(t *testing.T) {
|
||||
wrongSizeContent := []byte("Short")
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/octet-stream")
|
||||
w.Write(wrongSizeContent)
|
||||
_, _ = w.Write(wrongSizeContent)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
// Create temp directory
|
||||
destDir, err := os.MkdirTemp("", "mfer-fetch-size-test-*")
|
||||
require.NoError(t, err)
|
||||
defer os.RemoveAll(destDir)
|
||||
defer func() { _ = os.RemoveAll(destDir) }()
|
||||
|
||||
origDir, err := os.Getwd()
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, os.Chdir(destDir))
|
||||
defer os.Chdir(origDir)
|
||||
defer func() { _ = os.Chdir(origDir) }()
|
||||
|
||||
// Try to download - should fail with size mismatch
|
||||
err = downloadFile(server.URL+"/file.txt", "file.txt", files[0], nil)
|
||||
@ -320,19 +320,19 @@ func TestFetchProgress(t *testing.T) {
|
||||
w.Header().Set("Content-Length", "102400")
|
||||
// Write in chunks to allow progress reporting
|
||||
reader := bytes.NewReader(content)
|
||||
io.Copy(w, reader)
|
||||
_, _ = io.Copy(w, reader)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
// Create temp directory
|
||||
destDir, err := os.MkdirTemp("", "mfer-fetch-progress-test-*")
|
||||
require.NoError(t, err)
|
||||
defer os.RemoveAll(destDir)
|
||||
defer func() { _ = os.RemoveAll(destDir) }()
|
||||
|
||||
origDir, err := os.Getwd()
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, os.Chdir(destDir))
|
||||
defer os.Chdir(origDir)
|
||||
defer func() { _ = os.Chdir(origDir) }()
|
||||
|
||||
// Set up progress channel and collect updates
|
||||
progress := make(chan DownloadProgress, 100)
|
||||
|
||||
@ -258,7 +258,7 @@ func (mfa *CLIApp) freshenManifestOperation(ctx *cli.Context) error {
|
||||
}
|
||||
}
|
||||
})
|
||||
f.Close()
|
||||
_ = f.Close()
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to hash %s: %w", e.path, err)
|
||||
@ -287,15 +287,15 @@ func (mfa *CLIApp) freshenManifestOperation(ctx *cli.Context) error {
|
||||
}
|
||||
|
||||
err = builder.Build(outFile)
|
||||
outFile.Close()
|
||||
_ = outFile.Close()
|
||||
if err != nil {
|
||||
mfa.Fs.Remove(tmpPath)
|
||||
_ = mfa.Fs.Remove(tmpPath)
|
||||
return fmt.Errorf("failed to write manifest: %w", err)
|
||||
}
|
||||
|
||||
// Rename temp to final
|
||||
if err := mfa.Fs.Rename(tmpPath, manifestPath); err != nil {
|
||||
mfa.Fs.Remove(tmpPath)
|
||||
_ = mfa.Fs.Remove(tmpPath)
|
||||
return fmt.Errorf("failed to rename manifest: %w", err)
|
||||
}
|
||||
|
||||
|
||||
@ -76,7 +76,7 @@ func (mfa *CLIApp) generateManifestOperation(ctx *cli.Context) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create output file: %w", err)
|
||||
}
|
||||
defer outFile.Close()
|
||||
defer func() { _ = outFile.Close() }()
|
||||
|
||||
// Phase 2: Scan - read file contents and generate manifest
|
||||
var scanProgress chan scanner.ScanStatus
|
||||
|
||||
@ -41,7 +41,7 @@ const banner = `
|
||||
\__\/ \__\/ \__\/ \__\/`
|
||||
|
||||
func (mfa *CLIApp) printBanner() {
|
||||
fmt.Fprintln(mfa.Stdout, banner)
|
||||
_, _ = fmt.Fprintln(mfa.Stdout, banner)
|
||||
}
|
||||
|
||||
// VersionString returns the version and git revision formatted for display.
|
||||
@ -201,7 +201,7 @@ func (mfa *CLIApp) run(args []string) {
|
||||
Name: "version",
|
||||
Usage: "Show version",
|
||||
Action: func(c *cli.Context) error {
|
||||
fmt.Fprintln(mfa.Stdout, mfa.VersionString())
|
||||
_, _ = fmt.Fprintln(mfa.Stdout, mfa.VersionString())
|
||||
return nil
|
||||
},
|
||||
},
|
||||
|
||||
@ -26,7 +26,7 @@ func (m *manifest) deserializeInner() error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer gzr.Close()
|
||||
defer func() { _ = gzr.Close() }()
|
||||
|
||||
dat, err := io.ReadAll(gzr)
|
||||
if err != nil {
|
||||
@ -99,7 +99,7 @@ func NewManifestFromFile(fs afero.Fs, path string) (*manifest, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
defer func() { _ = f.Close() }()
|
||||
return NewManifestFromReader(f)
|
||||
}
|
||||
|
||||
|
||||
@ -12,12 +12,12 @@ func (m *manifest) WriteToFile(path string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
defer func() { _ = f.Close() }()
|
||||
|
||||
return m.WriteTo(f)
|
||||
return m.Write(f)
|
||||
}
|
||||
|
||||
func (m *manifest) WriteTo(output io.Writer) error {
|
||||
func (m *manifest) Write(output io.Writer) error {
|
||||
if m.pbOuter == nil {
|
||||
err := m.generate()
|
||||
if err != nil {
|
||||
|
||||
@ -65,7 +65,7 @@ func (m *manifest) generateOuter() error {
|
||||
return err
|
||||
}
|
||||
|
||||
gzw.Close()
|
||||
_ = gzw.Close()
|
||||
|
||||
o := &MFFileOuter{
|
||||
InnerMessage: idc.Bytes(),
|
||||
|
||||
Loading…
Reference in New Issue
Block a user