aboutsummaryrefslogtreecommitdiff
path: root/forged/internal/misc
diff options
context:
space:
mode:
authorRunxi Yu <me@runxiyu.org>2025-04-06 09:26:46 +0800
committerRunxi Yu <me@runxiyu.org>2025-04-06 09:27:53 +0800
commitda1d8f4e7c332c7109427915e6459b10209cedce (patch)
tree280b921be3b51f93d82d916b4eaa89387b7102cc /forged/internal/misc
parentgit2c, git2d: Rename cmd1 and cmd2 descriptively (diff)
downloadforge-0.1.32.tar.gz
forge-0.1.32.tar.zst
forge-0.1.32.zip
Move the Go stuff to ./forged/v0.1.32
Diffstat (limited to 'forged/internal/misc')
-rw-r--r--forged/internal/misc/back.go9
-rw-r--r--forged/internal/misc/deploy.go20
-rw-r--r--forged/internal/misc/iter.go23
-rw-r--r--forged/internal/misc/misc.go18
-rw-r--r--forged/internal/misc/panic.go17
-rw-r--r--forged/internal/misc/trivial.go48
-rw-r--r--forged/internal/misc/unsafe.go20
-rw-r--r--forged/internal/misc/url.go154
8 files changed, 309 insertions, 0 deletions
diff --git a/forged/internal/misc/back.go b/forged/internal/misc/back.go
new file mode 100644
index 0000000..ef4ed22
--- /dev/null
+++ b/forged/internal/misc/back.go
@@ -0,0 +1,9 @@
+// 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/forged/internal/misc/deploy.go b/forged/internal/misc/deploy.go
new file mode 100644
index 0000000..0f24f49
--- /dev/null
+++ b/forged/internal/misc/deploy.go
@@ -0,0 +1,20 @@
+// 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/forged/internal/misc/iter.go b/forged/internal/misc/iter.go
new file mode 100644
index 0000000..61a96f4
--- /dev/null
+++ b/forged/internal/misc/iter.go
@@ -0,0 +1,23 @@
+// 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/forged/internal/misc/misc.go b/forged/internal/misc/misc.go
new file mode 100644
index 0000000..398020a
--- /dev/null
+++ b/forged/internal/misc/misc.go
@@ -0,0 +1,18 @@
+// 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/forged/internal/misc/panic.go b/forged/internal/misc/panic.go
new file mode 100644
index 0000000..94cd47a
--- /dev/null
+++ b/forged/internal/misc/panic.go
@@ -0,0 +1,17 @@
+// 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/forged/internal/misc/trivial.go b/forged/internal/misc/trivial.go
new file mode 100644
index 0000000..e59c17e
--- /dev/null
+++ b/forged/internal/misc/trivial.go
@@ -0,0 +1,48 @@
+// 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/forged/internal/misc/unsafe.go b/forged/internal/misc/unsafe.go
new file mode 100644
index 0000000..6c2192f
--- /dev/null
+++ b/forged/internal/misc/unsafe.go
@@ -0,0 +1,20 @@
+// 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/forged/internal/misc/url.go b/forged/internal/misc/url.go
new file mode 100644
index 0000000..b77d8ce
--- /dev/null
+++ b/forged/internal/misc/url.go
@@ -0,0 +1,154 @@
+// 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
+}