Lindenii Project Forge
Lint misc
// SPDX-License-Identifier: AGPL-3.0-only // SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org> package misc import ( "io" "io/fs" "os" ) // DeployBinary copies the contents of a binary file to the target destination path. // The destination file is created with executable permissions. 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 }
package misc import (
"context"
"errors" "fmt" "net" "syscall" )
func ListenUnixSocket(path string) (listener net.Listener, replaced bool, err error) { listener, err = net.Listen("unix", path)
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
if unlinkErr := syscall.Unlink(path); unlinkErr != nil {
unlinkErr := syscall.Unlink(path) if unlinkErr != nil {
return listener, false, fmt.Errorf("remove existing socket %q: %w", path, unlinkErr) }
listener, err = net.Listen("unix", path)
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(net_, addr string) (listener net.Listener, err error) {
func Listen(ctx context.Context, net_, addr string) (listener net.Listener, err error) {
if net_ == "unix" {
listener, _, err = ListenUnixSocket(addr)
listener, _, err = ListenUnixSocket(ctx, addr)
if err != nil { return listener, fmt.Errorf("listen unix socket for web: %w", err) } } else {
listener, err = net.Listen(net_, addr)
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 }
// SPDX-License-Identifier: AGPL-3.0-only // SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org> package misc // FirstOrPanic returns the value or panics if the error is non-nil. func FirstOrPanic[T any](v T, err error) T { if err != nil { panic(err) } return v } // NoneOrPanic panics if the provided error is non-nil. func NoneOrPanic(err error) { if err != nil { panic(err) } }
// 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 {
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 } var z T return z } // Minus subtracts two numbers. func Minus(a, b int) int { return a - b }
// 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))
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
}