os.go (hugo-0.80.0) | : | os.go (hugo-0.81.0) | ||
---|---|---|---|---|
skipping to change at line 21 | skipping to change at line 21 | |||
// See the License for the specific language governing permissions and | // See the License for the specific language governing permissions and | |||
// limitations under the License. | // limitations under the License. | |||
// Package os provides template functions for interacting with the operating | // Package os provides template functions for interacting with the operating | |||
// system. | // system. | |||
package os | package os | |||
import ( | import ( | |||
"errors" | "errors" | |||
"fmt" | "fmt" | |||
"os" | ||||
_os "os" | _os "os" | |||
"github.com/gohugoio/hugo/deps" | "github.com/gohugoio/hugo/deps" | |||
"github.com/spf13/afero" | "github.com/spf13/afero" | |||
"github.com/spf13/cast" | "github.com/spf13/cast" | |||
) | ) | |||
// New returns a new instance of the os-namespaced template functions. | // New returns a new instance of the os-namespaced template functions. | |||
func New(d *deps.Deps) *Namespace { | func New(d *deps.Deps) *Namespace { | |||
var rfs afero.Fs | var rfs afero.Fs | |||
skipping to change at line 65 | skipping to change at line 64 | |||
skey, err := cast.ToStringE(key) | skey, err := cast.ToStringE(key) | |||
if err != nil { | if err != nil { | |||
return "", nil | return "", nil | |||
} | } | |||
return _os.Getenv(skey), nil | return _os.Getenv(skey), nil | |||
} | } | |||
// readFile reads the file named by filename in the given filesystem | // readFile reads the file named by filename in the given filesystem | |||
// and returns the contents as a string. | // and returns the contents as a string. | |||
// There is a upper size limit set at 1 megabytes. | ||||
func readFile(fs afero.Fs, filename string) (string, error) { | func readFile(fs afero.Fs, filename string) (string, error) { | |||
if filename == "" { | if filename == "" { | |||
return "", errors.New("readFile needs a filename") | return "", errors.New("readFile needs a filename") | |||
} | } | |||
if info, err := fs.Stat(filename); err == nil { | ||||
if info.Size() > 1000000 { | ||||
return "", fmt.Errorf("file %q is too big", filename) | ||||
} | ||||
} else { | ||||
if os.IsNotExist(err) { | ||||
return "", fmt.Errorf("file %q does not exist", filename) | ||||
} | ||||
return "", err | ||||
} | ||||
b, err := afero.ReadFile(fs, filename) | b, err := afero.ReadFile(fs, filename) | |||
if err != nil { | if err != nil { | |||
return "", err | return "", err | |||
} | } | |||
return string(b), nil | return string(b), nil | |||
} | } | |||
// ReadFile reads the file named by filename relative to the configured WorkingD ir. | // ReadFile reads the file named by filename relative to the configured WorkingD ir. | |||
// It returns the contents as a string. | // It returns the contents as a string. | |||
End of changes. 3 change blocks. | ||||
12 lines changed or deleted | 0 lines changed or added |