mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-21 20:46:30 -05:00
helpers: apply some Golint rules
This commit is contained in:
parent
e685dfc0c6
commit
bd2b679eaf
4 changed files with 39 additions and 46 deletions
|
@ -31,6 +31,7 @@ import (
|
||||||
// Filepath separator defined by os.Separator.
|
// Filepath separator defined by os.Separator.
|
||||||
const FilePathSeparator = string(filepath.Separator)
|
const FilePathSeparator = string(filepath.Separator)
|
||||||
|
|
||||||
|
// FindAvailablePort returns an available and valid TCP port.
|
||||||
func FindAvailablePort() (*net.TCPAddr, error) {
|
func FindAvailablePort() (*net.TCPAddr, error) {
|
||||||
l, err := net.Listen("tcp", ":0")
|
l, err := net.Listen("tcp", ":0")
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
@ -100,6 +101,7 @@ func BytesToReader(in []byte) io.Reader {
|
||||||
return bytes.NewReader(in)
|
return bytes.NewReader(in)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ThemeSet checks whether a theme is in use or not.
|
||||||
func ThemeSet() bool {
|
func ThemeSet() bool {
|
||||||
return viper.GetString("theme") != ""
|
return viper.GetString("theme") != ""
|
||||||
}
|
}
|
||||||
|
@ -193,9 +195,8 @@ func DoArithmetic(a, b interface{}, op rune) (interface{}, error) {
|
||||||
if bv.Kind() == reflect.String && op == '+' {
|
if bv.Kind() == reflect.String && op == '+' {
|
||||||
bs := bv.String()
|
bs := bv.String()
|
||||||
return as + bs, nil
|
return as + bs, nil
|
||||||
} else {
|
|
||||||
return nil, errors.New("Can't apply the operator to the values")
|
|
||||||
}
|
}
|
||||||
|
return nil, errors.New("Can't apply the operator to the values")
|
||||||
default:
|
default:
|
||||||
return nil, errors.New("Can't apply the operator to the values")
|
return nil, errors.New("Can't apply the operator to the values")
|
||||||
}
|
}
|
||||||
|
|
|
@ -110,13 +110,13 @@ func TestSliceToLowerNonDestructive(t *testing.T) {
|
||||||
// This assignment actually copies the content
|
// This assignment actually copies the content
|
||||||
// of input into a new object.
|
// of input into a new object.
|
||||||
// Otherwise, the test would not make sense...
|
// Otherwise, the test would not make sense...
|
||||||
input_copy := input
|
inputCopy := input
|
||||||
|
|
||||||
SliceToLower(input)
|
SliceToLower(input)
|
||||||
|
|
||||||
for i, e := range input {
|
for i, e := range input {
|
||||||
if e != input_copy[i] {
|
if e != inputCopy[i] {
|
||||||
t.Errorf("TestSliceToLowerNonDestructive failed. Expected element #%d of input slice to be %s. Found %s.", i, input_copy[i], input[i])
|
t.Errorf("TestSliceToLowerNonDestructive failed. Expected element #%d of input slice to be %s. Found %s.", i, inputCopy[i], input[i])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,7 +28,7 @@ import (
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Bridge for common functionality in filepath vs path
|
// FilepathPathBridge is a bridge for common functionality in filepath vs path
|
||||||
type FilepathPathBridge interface {
|
type FilepathPathBridge interface {
|
||||||
Base(in string) string
|
Base(in string) string
|
||||||
Clean(in string) string
|
Clean(in string) string
|
||||||
|
@ -149,9 +149,8 @@ func IsEmpty(path string, fs afero.Fs) (bool, error) {
|
||||||
list, err := f.Readdir(-1)
|
list, err := f.Readdir(-1)
|
||||||
// f.Close() - see bug fix above
|
// f.Close() - see bug fix above
|
||||||
return len(list) == 0, nil
|
return len(list) == 0, nil
|
||||||
} else {
|
|
||||||
return fi.Size() == 0, nil
|
|
||||||
}
|
}
|
||||||
|
return fi.Size() == 0, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if a file or directory exists.
|
// Check if a file or directory exists.
|
||||||
|
@ -317,9 +316,8 @@ func GuessSection(in string) string {
|
||||||
if parts[0] == "content" {
|
if parts[0] == "content" {
|
||||||
if len(parts) > 2 {
|
if len(parts) > 2 {
|
||||||
return parts[1]
|
return parts[1]
|
||||||
} else {
|
|
||||||
return ""
|
|
||||||
}
|
}
|
||||||
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
return parts[0]
|
return parts[0]
|
||||||
|
@ -328,9 +326,8 @@ func GuessSection(in string) string {
|
||||||
func PathPrep(ugly bool, in string) string {
|
func PathPrep(ugly bool, in string) string {
|
||||||
if ugly {
|
if ugly {
|
||||||
return Uglify(in)
|
return Uglify(in)
|
||||||
} else {
|
|
||||||
return PrettifyPath(in)
|
|
||||||
}
|
}
|
||||||
|
return PrettifyPath(in)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Same as PrettifyUrlPath() but for file paths.
|
// Same as PrettifyUrlPath() but for file paths.
|
||||||
|
@ -348,16 +345,14 @@ func PrettiyPath(in string, b FilepathPathBridge) string {
|
||||||
return b.Separator()
|
return b.Separator()
|
||||||
}
|
}
|
||||||
return b.Join(b.Clean(in), "index.html")
|
return b.Join(b.Clean(in), "index.html")
|
||||||
} else {
|
|
||||||
name, ext := FileAndExt(in, b)
|
|
||||||
if name == "index" {
|
|
||||||
// /section/name/index.html -> /section/name/index.html
|
|
||||||
return b.Clean(in)
|
|
||||||
} else {
|
|
||||||
// /section/name.html -> /section/name/index.html
|
|
||||||
return b.Join(b.Dir(in), name, "index"+ext)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
name, ext := FileAndExt(in, b)
|
||||||
|
if name == "index" {
|
||||||
|
// /section/name/index.html -> /section/name/index.html
|
||||||
|
return b.Clean(in)
|
||||||
|
}
|
||||||
|
// /section/name.html -> /section/name/index.html
|
||||||
|
return b.Join(b.Dir(in), name, "index"+ext)
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindCWD returns the current working directory from where the Hugo
|
// FindCWD returns the current working directory from where the Hugo
|
||||||
|
|
|
@ -172,18 +172,17 @@ func UrlPrep(ugly bool, in string) string {
|
||||||
if ugly {
|
if ugly {
|
||||||
x := Uglify(SanitizeUrl(in))
|
x := Uglify(SanitizeUrl(in))
|
||||||
return x
|
return x
|
||||||
} else {
|
|
||||||
x := PrettifyUrl(SanitizeUrl(in))
|
|
||||||
if path.Ext(x) == ".xml" {
|
|
||||||
return x
|
|
||||||
}
|
|
||||||
url, err := purell.NormalizeURLString(x, purell.FlagAddTrailingSlash)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("ERROR returned by NormalizeURLString. Returning in = %q\n", in)
|
|
||||||
return in
|
|
||||||
}
|
|
||||||
return url
|
|
||||||
}
|
}
|
||||||
|
x := PrettifyUrl(SanitizeUrl(in))
|
||||||
|
if path.Ext(x) == ".xml" {
|
||||||
|
return x
|
||||||
|
}
|
||||||
|
url, err := purell.NormalizeURLString(x, purell.FlagAddTrailingSlash)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("ERROR returned by NormalizeURLString. Returning in = %q\n", in)
|
||||||
|
return in
|
||||||
|
}
|
||||||
|
return url
|
||||||
}
|
}
|
||||||
|
|
||||||
// PrettifyUrl takes a URL string and returns a semantic, clean URL.
|
// PrettifyUrl takes a URL string and returns a semantic, clean URL.
|
||||||
|
@ -221,19 +220,17 @@ func Uglify(in string) string {
|
||||||
}
|
}
|
||||||
// /section/name/ -> /section/name.html
|
// /section/name/ -> /section/name.html
|
||||||
return path.Clean(in) + ".html"
|
return path.Clean(in) + ".html"
|
||||||
} else {
|
|
||||||
name, ext := FileAndExt(in, pathBridge)
|
|
||||||
if name == "index" {
|
|
||||||
// /section/name/index.html -> /section/name.html
|
|
||||||
d := path.Dir(in)
|
|
||||||
if len(d) > 1 {
|
|
||||||
return d + ext
|
|
||||||
} else {
|
|
||||||
return in
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// /section/name.html -> /section/name.html
|
|
||||||
return path.Clean(in)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
name, ext := FileAndExt(in, pathBridge)
|
||||||
|
if name == "index" {
|
||||||
|
// /section/name/index.html -> /section/name.html
|
||||||
|
d := path.Dir(in)
|
||||||
|
if len(d) > 1 {
|
||||||
|
return d + ext
|
||||||
|
}
|
||||||
|
return in
|
||||||
|
}
|
||||||
|
// /section/name.html -> /section/name.html
|
||||||
|
return path.Clean(in)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue