Use the Afero source fs where relevant

Fixes #2319
This commit is contained in:
Bjørn Erik Pedersen 2016-07-30 15:37:03 +02:00
parent a0859dc672
commit 4ddd5361c1
4 changed files with 13 additions and 34 deletions

View file

@ -17,7 +17,6 @@ package commands
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
@ -971,19 +970,7 @@ func isThemeVsHugoVersionMismatch() (mismatch bool, requiredMinVersion string) {
return
}
f, err := fs.Open(path)
if err != nil {
return
}
defer f.Close()
b, err := ioutil.ReadAll(f)
if err != nil {
return
}
b, err := afero.ReadFile(fs, path)
c, err := parser.HandleTOMLMetaData(b)

View file

@ -20,6 +20,7 @@ import (
"time"
"github.com/spf13/cobra"
"github.com/spf13/hugo/hugofs"
"github.com/spf13/hugo/parser"
)
@ -46,7 +47,7 @@ func Undraft(cmd *cobra.Command, args []string) error {
location := args[0]
// open the file
f, err := os.Open(location)
f, err := hugofs.Source().Open(location)
if err != nil {
return err
}
@ -63,7 +64,7 @@ func Undraft(cmd *cobra.Command, args []string) error {
return newSystemErrorF("an error occurred while undrafting %q: %s", location, err)
}
f, err = os.OpenFile(location, os.O_WRONLY|os.O_TRUNC, 0644)
f, err = hugofs.Source().OpenFile(location, os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
return newSystemErrorF("%q not be undrafted due to error opening file to save changes: %q\n", location, err)
}

View file

@ -16,7 +16,6 @@ package source
import (
"bytes"
"io"
"io/ioutil"
"os"
"testing"
@ -60,7 +59,7 @@ func TestRead(t *testing.T) {
b, err := afero.ReadFile(fs, filename)
if err != nil {
t.Fatalf("ioutil.ReadFile: %v", err)
t.Fatalf("afero.ReadFile: %v", err)
}
rd, err := NewLazyFileReader(fs, filename)
@ -101,7 +100,7 @@ func TestSeek(t *testing.T) {
filename := "lazy_file_reader_test.go"
b, err := afero.ReadFile(fs, filename)
if err != nil {
t.Fatalf("ioutil.ReadFile: %v", err)
t.Fatalf("afero.ReadFile: %v", err)
}
// no cache case
@ -192,14 +191,14 @@ func TestSeek(t *testing.T) {
func TestWriteTo(t *testing.T) {
fs := afero.NewOsFs()
filename := "lazy_file_reader_test.go"
fi, err := os.Stat(filename)
fi, err := fs.Stat(filename)
if err != nil {
t.Fatalf("os.Stat: %v", err)
}
b, err := ioutil.ReadFile(filename)
b, err := afero.ReadFile(fs, filename)
if err != nil {
t.Fatalf("ioutil.ReadFile: %v", err)
t.Fatalf("afero.ReadFile: %v", err)
}
rd, err := NewLazyFileReader(fs, filename)

View file

@ -83,12 +83,8 @@ func resGetCache(id string, fs afero.Fs, ignoreCache bool) ([]byte, error) {
return nil, nil
}
f, err := fs.Open(fID)
if err != nil {
return nil, err
}
defer f.Close()
return ioutil.ReadAll(f)
return afero.ReadFile(fs, fID)
}
// resWriteCache writes bytes to an ID into the file cache
@ -165,12 +161,8 @@ func resGetLocal(url string, fs afero.Fs) ([]byte, error) {
return nil, err
}
f, err := fs.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()
return ioutil.ReadAll(f)
return afero.ReadFile(fs, filename)
}
// resGetResource loads the content of a local or remote file