Skip to content

Commit

Permalink
fix: Deferring unsafe method on type
Browse files Browse the repository at this point in the history
  • Loading branch information
soulteary committed Jun 12, 2022
1 parent 20c1a16 commit 834b67b
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions pkgs/vfs/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -113,11 +114,17 @@ func TarBzip2(r io.Reader) (VFS, error) {
// - .tar.gz
// - .tar.bz2
func Open(filename string) (VFS, error) {
f, err := os.Open(filename)
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()

defer func() {
if err := file.Close(); err != nil {
log.Fatal(err)
}
}()

base := filepath.Base(filename)
ext := strings.ToLower(filepath.Ext(base))
nonExt := filename[:len(filename)-len(ext)]
Expand All @@ -126,17 +133,17 @@ func Open(filename string) (VFS, error) {
}
switch ext {
case ".zip":
st, err := f.Stat()
st, err := file.Stat()
if err != nil {
return nil, err
}
return Zip(f, st.Size())
return Zip(file, st.Size())
case ".tar":
return Tar(f)
return Tar(file)
case ".tar.gz":
return TarGzip(f)
return TarGzip(file)
case ".tar.bz2":
return TarBzip2(f)
return TarBzip2(file)
}
return nil, fmt.Errorf("can't open a VFS from a %s file", ext)
}

0 comments on commit 834b67b

Please sign in to comment.