aboutsummaryrefslogtreecommitdiff
path: root/forged/internal/common/misc
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--forged/internal/common/misc/back.go (renamed from forged/internal/misc/back.go)0
-rw-r--r--forged/internal/common/misc/iter.go (renamed from forged/internal/misc/iter.go)0
-rw-r--r--forged/internal/common/misc/misc.go (renamed from forged/internal/render/render.go)4
-rw-r--r--forged/internal/common/misc/net.go42
-rw-r--r--forged/internal/common/misc/slices.go (renamed from forged/internal/misc/misc.go)1
-rw-r--r--forged/internal/common/misc/trivial.go (renamed from forged/internal/misc/trivial.go)4
-rw-r--r--forged/internal/common/misc/unsafe.go (renamed from forged/internal/misc/unsafe.go)4
-rw-r--r--forged/internal/common/misc/url.go (renamed from forged/internal/misc/url.go)0
8 files changed, 48 insertions, 7 deletions
diff --git a/forged/internal/misc/back.go b/forged/internal/common/misc/back.go
index 5351359..5351359 100644
--- a/forged/internal/misc/back.go
+++ b/forged/internal/common/misc/back.go
diff --git a/forged/internal/misc/iter.go b/forged/internal/common/misc/iter.go
index 61a96f4..61a96f4 100644
--- a/forged/internal/misc/iter.go
+++ b/forged/internal/common/misc/iter.go
diff --git a/forged/internal/render/render.go b/forged/internal/common/misc/misc.go
index 465e410..e9e10ab 100644
--- a/forged/internal/render/render.go
+++ b/forged/internal/common/misc/misc.go
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: AGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org>
-// Package render provides functions to render code and READMEs.
-package render
+// Package misc provides miscellaneous functions and other definitions.
+package misc
diff --git a/forged/internal/common/misc/net.go b/forged/internal/common/misc/net.go
new file mode 100644
index 0000000..967ea77
--- /dev/null
+++ b/forged/internal/common/misc/net.go
@@ -0,0 +1,42 @@
+package misc
+
+import (
+ "context"
+ "errors"
+ "fmt"
+ "net"
+ "syscall"
+)
+
+func ListenUnixSocket(ctx context.Context, path string) (listener net.Listener, replaced bool, err error) {
+ listenConfig := net.ListenConfig{} //exhaustruct:ignore
+ listener, err = listenConfig.Listen(ctx, "unix", path)
+ if errors.Is(err, syscall.EADDRINUSE) {
+ replaced = true
+ unlinkErr := syscall.Unlink(path)
+ if unlinkErr != nil {
+ return listener, false, fmt.Errorf("remove existing socket %q: %w", path, unlinkErr)
+ }
+ listener, err = listenConfig.Listen(ctx, "unix", path)
+ }
+ if err != nil {
+ return listener, replaced, fmt.Errorf("listen on unix socket %q: %w", path, err)
+ }
+ return listener, replaced, nil
+}
+
+func Listen(ctx context.Context, net_, addr string) (listener net.Listener, err error) {
+ if net_ == "unix" {
+ listener, _, err = ListenUnixSocket(ctx, addr)
+ if err != nil {
+ return listener, fmt.Errorf("listen unix socket for web: %w", err)
+ }
+ } else {
+ listenConfig := net.ListenConfig{} //exhaustruct:ignore
+ listener, err = listenConfig.Listen(ctx, net_, addr)
+ if err != nil {
+ return listener, fmt.Errorf("listen %s for web: %w", net_, err)
+ }
+ }
+ return listener, nil
+}
diff --git a/forged/internal/misc/misc.go b/forged/internal/common/misc/slices.go
index 398020a..3ad0211 100644
--- a/forged/internal/misc/misc.go
+++ b/forged/internal/common/misc/slices.go
@@ -1,7 +1,6 @@
// 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"
diff --git a/forged/internal/misc/trivial.go b/forged/internal/common/misc/trivial.go
index e59c17e..83901e0 100644
--- a/forged/internal/misc/trivial.go
+++ b/forged/internal/common/misc/trivial.go
@@ -28,13 +28,13 @@ func QueryEscape(s string) string {
}
// Dereference dereferences a pointer.
-func Dereference[T any](p *T) T {
+func Dereference[T any](p *T) T { //nolint:ireturn
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 {
+func DereferenceOrZero[T any](p *T) T { //nolint:ireturn
if p != nil {
return *p
}
diff --git a/forged/internal/misc/unsafe.go b/forged/internal/common/misc/unsafe.go
index 6c2192f..d827e7f 100644
--- a/forged/internal/misc/unsafe.go
+++ b/forged/internal/common/misc/unsafe.go
@@ -9,12 +9,12 @@ import "unsafe"
// 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))
+ return unsafe.Slice(unsafe.StringData(s), len(s)) //#nosec G103
}
// 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))
+ return unsafe.String(unsafe.SliceData(b), len(b)) //#nosec G103
}
diff --git a/forged/internal/misc/url.go b/forged/internal/common/misc/url.go
index 346ff76..346ff76 100644
--- a/forged/internal/misc/url.go
+++ b/forged/internal/common/misc/url.go