diff options
Diffstat (limited to 'internal/misc')
-rw-r--r-- | internal/misc/back.go | 9 | ||||
-rw-r--r-- | internal/misc/deploy.go | 20 | ||||
-rw-r--r-- | internal/misc/iter.go | 23 | ||||
-rw-r--r-- | internal/misc/misc.go | 18 | ||||
-rw-r--r-- | internal/misc/panic.go | 17 | ||||
-rw-r--r-- | internal/misc/trivial.go | 48 | ||||
-rw-r--r-- | internal/misc/unsafe.go | 20 | ||||
-rw-r--r-- | internal/misc/url.go | 154 |
8 files changed, 0 insertions, 309 deletions
diff --git a/internal/misc/back.go b/internal/misc/back.go deleted file mode 100644 index ef4ed22..0000000 --- a/internal/misc/back.go +++ /dev/null @@ -1,9 +0,0 @@ -// SPDX-License-Identifier: AGPL-3.0-only -// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org> - -package misc - -type ErrorBack[T any] struct { - Content T - ErrorChan chan error -} diff --git a/internal/misc/deploy.go b/internal/misc/deploy.go deleted file mode 100644 index 0f24f49..0000000 --- a/internal/misc/deploy.go +++ /dev/null @@ -1,20 +0,0 @@ -// SPDX-License-Identifier: AGPL-3.0-only -// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org> - -package misc - -import ( - "io" - "io/fs" - "os" -) - -func DeployBinary(src fs.File, dst string) (err error) { - var dstFile *os.File - if dstFile, err = os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o755); err != nil { - return err - } - defer dstFile.Close() - _, err = io.Copy(dstFile, src) - return err -} diff --git a/internal/misc/iter.go b/internal/misc/iter.go deleted file mode 100644 index 61a96f4..0000000 --- a/internal/misc/iter.go +++ /dev/null @@ -1,23 +0,0 @@ -// SPDX-License-Identifier: AGPL-3.0-only -// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org> - -package misc - -import "iter" - -// iterSeqLimit returns an iterator equivalent to the supplied one, but stops -// after n iterations. -func IterSeqLimit[T any](s iter.Seq[T], n uint) iter.Seq[T] { - return func(yield func(T) bool) { - var iterations uint - for v := range s { - if iterations > n-1 { - return - } - if !yield(v) { - return - } - iterations++ - } - } -} diff --git a/internal/misc/misc.go b/internal/misc/misc.go deleted file mode 100644 index 398020a..0000000 --- a/internal/misc/misc.go +++ /dev/null @@ -1,18 +0,0 @@ -// SPDX-License-Identifier: AGPL-3.0-only -// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org> - -// Package misc provides miscellaneous functions and other definitions. -package misc - -import "strings" - -// sliceContainsNewlines returns true if and only if the given slice contains -// one or more strings that contains newlines. -func SliceContainsNewlines(s []string) bool { - for _, v := range s { - if strings.Contains(v, "\n") { - return true - } - } - return false -} diff --git a/internal/misc/panic.go b/internal/misc/panic.go deleted file mode 100644 index 94cd47a..0000000 --- a/internal/misc/panic.go +++ /dev/null @@ -1,17 +0,0 @@ -// SPDX-License-Identifier: AGPL-3.0-only -// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org> - -package misc - -func FirstOrPanic[T any](v T, err error) T { - if err != nil { - panic(err) - } - return v -} - -func NoneOrPanic(err error) { - if err != nil { - panic(err) - } -} diff --git a/internal/misc/trivial.go b/internal/misc/trivial.go deleted file mode 100644 index e59c17e..0000000 --- a/internal/misc/trivial.go +++ /dev/null @@ -1,48 +0,0 @@ -// SPDX-License-Identifier: AGPL-3.0-only -// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org> - -package misc - -import ( - "net/url" - "strings" -) - -// These are all trivial functions that are intended to be used in HTML -// templates. - -// FirstLine returns the first line of a string. -func FirstLine(s string) string { - before, _, _ := strings.Cut(s, "\n") - return before -} - -// PathEscape escapes the input as an URL path segment. -func PathEscape(s string) string { - return url.PathEscape(s) -} - -// QueryEscape escapes the input as an URL query segment. -func QueryEscape(s string) string { - return url.QueryEscape(s) -} - -// Dereference dereferences a pointer. -func Dereference[T any](p *T) T { - return *p -} - -// DereferenceOrZero dereferences a pointer. If the pointer is nil, the zero -// value of its associated type is returned instead. -func DereferenceOrZero[T any](p *T) T { - if p != nil { - return *p - } - var z T - return z -} - -// Minus subtracts two numbers. -func Minus(a, b int) int { - return a - b -} diff --git a/internal/misc/unsafe.go b/internal/misc/unsafe.go deleted file mode 100644 index 6c2192f..0000000 --- a/internal/misc/unsafe.go +++ /dev/null @@ -1,20 +0,0 @@ -// SPDX-License-Identifier: AGPL-3.0-only -// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org> - -package misc - -import "unsafe" - -// StringToBytes converts a string to a byte slice without copying the string. -// Memory is borrowed from the string. -// The resulting byte slice must not be modified in any form. -func StringToBytes(s string) (bytes []byte) { - return unsafe.Slice(unsafe.StringData(s), len(s)) -} - -// BytesToString converts a byte slice to a string without copying the bytes. -// Memory is borrowed from the byte slice. -// The source byte slice must not be modified. -func BytesToString(b []byte) string { - return unsafe.String(unsafe.SliceData(b), len(b)) -} diff --git a/internal/misc/url.go b/internal/misc/url.go deleted file mode 100644 index b77d8ce..0000000 --- a/internal/misc/url.go +++ /dev/null @@ -1,154 +0,0 @@ -// SPDX-License-Identifier: AGPL-3.0-only -// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org> - -package misc - -import ( - "errors" - "net/http" - "net/url" - "strings" -) - -var ( - ErrDupRefSpec = errors.New("duplicate ref spec") - ErrNoRefSpec = errors.New("no ref spec") -) - -// getParamRefTypeName looks at the query parameters in an HTTP request and -// returns its ref name and type, if any. -func GetParamRefTypeName(request *http.Request) (retRefType, retRefName string, err error) { - rawQuery := request.URL.RawQuery - queryValues, err := url.ParseQuery(rawQuery) - if err != nil { - return - } - done := false - for _, refType := range []string{"commit", "branch", "tag"} { - refName, ok := queryValues[refType] - if ok { - if done { - err = ErrDupRefSpec - return - } - done = true - if len(refName) != 1 { - err = ErrDupRefSpec - return - } - retRefName = refName[0] - retRefType = refType - } - } - if !done { - err = ErrNoRefSpec - } - return -} - -// ParseReqURI parses an HTTP request URL, and returns a slice of path segments -// and the query parameters. It handles %2F correctly. -func ParseReqURI(requestURI string) (segments []string, params url.Values, err error) { - path, paramsStr, _ := strings.Cut(requestURI, "?") - - segments, err = PathToSegments(path) - if err != nil { - return - } - - params, err = url.ParseQuery(paramsStr) - return -} - -func PathToSegments(path string) (segments []string, err error) { - segments = strings.Split(strings.TrimPrefix(path, "/"), "/") - - for i, segment := range segments { - segments[i], err = url.PathUnescape(segment) - if err != nil { - return - } - } - - return -} - -// RedirectDir returns true and redirects the user to a version of the URL with -// a trailing slash, if and only if the request URL does not already have a -// trailing slash. -func RedirectDir(writer http.ResponseWriter, request *http.Request) bool { - requestURI := request.RequestURI - - pathEnd := strings.IndexAny(requestURI, "?#") - var path, rest string - if pathEnd == -1 { - path = requestURI - } else { - path = requestURI[:pathEnd] - rest = requestURI[pathEnd:] - } - - if !strings.HasSuffix(path, "/") { - http.Redirect(writer, request, path+"/"+rest, http.StatusSeeOther) - return true - } - return false -} - -// RedirectNoDir returns true and redirects the user to a version of the URL -// without a trailing slash, if and only if the request URL has a trailing -// slash. -func RedirectNoDir(writer http.ResponseWriter, request *http.Request) bool { - requestURI := request.RequestURI - - pathEnd := strings.IndexAny(requestURI, "?#") - var path, rest string - if pathEnd == -1 { - path = requestURI - } else { - path = requestURI[:pathEnd] - rest = requestURI[pathEnd:] - } - - if strings.HasSuffix(path, "/") { - http.Redirect(writer, request, strings.TrimSuffix(path, "/")+rest, http.StatusSeeOther) - return true - } - return false -} - -// RedirectUnconditionally unconditionally redirects the user back to the -// current page while preserving query parameters. -func RedirectUnconditionally(writer http.ResponseWriter, request *http.Request) { - requestURI := request.RequestURI - - pathEnd := strings.IndexAny(requestURI, "?#") - var path, rest string - if pathEnd == -1 { - path = requestURI - } else { - path = requestURI[:pathEnd] - rest = requestURI[pathEnd:] - } - - http.Redirect(writer, request, path+rest, http.StatusSeeOther) -} - -// SegmentsToURL joins URL segments to the path component of a URL. -// Each segment is escaped properly first. -func SegmentsToURL(segments []string) string { - for i, segment := range segments { - segments[i] = url.PathEscape(segment) - } - return strings.Join(segments, "/") -} - -// AnyContain returns true if and only if ss contains a string that contains c. -func AnyContain(ss []string, c string) bool { - for _, s := range ss { - if strings.Contains(s, c) { - return true - } - } - return false -} |