utils

package
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 8, 2025 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AtomicWriteFile

func AtomicWriteFile(name string, data []byte, perm os.FileMode) error

AtomicWriteFile writes data to the named file quasi-atomically, creating it if necessary. On unix-like systems, the function uses github.com/google/renameio. On Windows, it has a simpler implementation using os.Rename(), which is believed to be atomic on NTFS, but there is no hard guarantee from Microsoft on that.

func ConvertToNativeLineEndings

func ConvertToNativeLineEndings(b []byte) []byte

ConvertToNativeLineEndings converts all instances of '\n' to native line endings for the platform. Assumes that line endings are normalized, i.e. there are no '\r' or "\r\n" line endings in the data See NormalizeLineEndings

func DetectMediaType

func DetectMediaType(userGivenType string, filename string, getReader ReadCloserGetter) string

DetectMediaType detects the media type of the file. The type provided by the user always takes precedence over automatic detection, unless it is empty. The type is detected by http.DetectContentType. If that returns the generic 'application/octet-stream', then the type is guessed from the filename extension. If all of the above fails, it returns 'application/octet-stream'

func ExpandHome

func ExpandHome(path string) (string, error)

ExpandHome expands ~ in path with user's home directory, but only if path begins with ~ or /~ Otherwise, returns path unchanged

func JsGetArray

func JsGetArray(js map[string]any, key string) []any

func JsGetBool

func JsGetBool(js map[string]any, key string) *bool

func JsGetMap

func JsGetMap(js map[string]any, key string) map[string]any

func JsGetString

func JsGetString(js map[string]any, key string) *string

func NormalizeLineEndings

func NormalizeLineEndings(bytes []byte) []byte

func ParseAsList

func ParseAsList(list, separator string, trim bool) []string

func ReadFileLines

func ReadFileLines(path string) ([]string, error)

ReadFileLines reads a whole file into memory and returns its lines.

func ReadRequiredFile

func ReadRequiredFile(name string) (string, []byte, error)

ReadRequiredFile reads the file. Returns expanded absolute representation of the filename and file contents. Removes Byte-Order-Mark from the content

func SanitizeName

func SanitizeName(name string) string

func ToTrimmedLower

func ToTrimmedLower(s string) string

func WriteFileLines

func WriteFileLines(lines []string, path string, mode os.FileMode) error

WriteFileLines writes the lines to the given file.

Types

type ReadCloserGetter

type ReadCloserGetter func() (io.ReadCloser, error)

func ReadCloserGetterFromBytes

func ReadCloserGetterFromBytes(raw []byte) ReadCloserGetter

func ReadCloserGetterFromFilename

func ReadCloserGetterFromFilename(name string) ReadCloserGetter

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL