helper

package module
v0.0.0-...-be74403 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2023 License: MIT Imports: 28 Imported by: 25

README

Helper

This project is serve helper functions for Golang project.

License

MIT

Documentation

Overview

Package helper handles common functions for the waybackk application in Golang.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Byte2String

func Byte2String(b []byte) string

Byte2String converts byte slice to a string without memory allocation.

func CheckContext

func CheckContext(ctx context.Context, t leaktest.ErrorReporter)

CheckContext calls `leaktest.CheckContext` which is same as CheckTest, but uses a context.Context for cancellation and timeout control.

func CheckTest

func CheckTest(t leaktest.ErrorReporter)

CheckTest calls `leaktest.Check` to snapshots the currently-running goroutines to be run at the end of tests to see whether any goroutines leaked, waiting up to 5 seconds in error conditions.

func CheckTimeout

func CheckTimeout(t leaktest.ErrorReporter, d time.Duration)

CheckTimeout calls `leaktest.CheckTimeout` which is same as Check, but with a configurable timeout

func Exists

func Exists(name string) bool

Exists reports whether the named file or directory exists.

func FileName

func FileName(link, contentType string) string

FileName returns filename from webpage's link and content type.

func FileSize

func FileSize(filepath string) int64

FileSeze returns file attritubes of size about an inode, and it's unit alway is bytes.

func FindChromeExecPath

func FindChromeExecPath() string

FindChromeExecPath tries to find the Chrome browser somewhere in the current system. It finds in different locations on different OS systems. It could perform a rather aggressive search. That may make it a bit slow, but it will only be run when creating a new ExecAllocator. Fork from: https://github.com/chromedp/chromedp/blob/4ea2300cf7c7065242867bdcb8772533e0a66ea7/allocate.go#L352-L383

func HasStdin

func HasStdin() bool

HasStdin determines if the user has piped input

func IsDir

func IsDir(path string) bool

IsDir ensures directory of given path

func IsURL

func IsURL(str string) bool

IsURL returns a result of validation for string.

func MatchURL

func MatchURL(text string) []string

MatchURL is extract URL from text, returns []string always.

func MatchURLFallback

func MatchURLFallback(text string) []string

MatchURLFallback is extract URL from text, and convert to Google cache endpoint if not found, returns []string always.

func MockServer

func MockServer() (*http.Client, *http.ServeMux, *httptest.Server)

MockServer returns an http Client, ServeMux, and Server. The client proxies requests to the server and handlers can be registered on the mux to handle requests. The caller must close the test server.

func MoveFile

func MoveFile(src, dst string) error

MoveFile move file to another directory.

func NotFound

func NotFound(uri string) bool

NotFound returns a result of URI status is 404

func RandString

func RandString(length int, letter string) string

func ReadStdin

func ReadStdin() (stdin []string)

ReadStdin reads stdin to slice.

func RealURI

func RealURI(u *url.URL) *url.URL

RealURI returns final URL

func RetryRemoveAll

func RetryRemoveAll(path string, retries int) error

RetryRemoveAll will attempt to remove an item or directory up to the given number of retries. NOTE: This function is necessary because of a Windows bug with removing files that have been recently used: https://github.com/golang/go/issues/51442

func SetField

func SetField(v interface{}, name string, value string) error

SetField sets field of v with given name to given value.

func String2Byte

func String2Byte(s string) (b []byte)

String2Byte converts string to a byte slice without memory allocation.

func TinyURL

func TinyURL(link string) string

func UTF8Encoding

func UTF8Encoding(s string) (r io.Reader, err error)

func Unsetenv

func Unsetenv(envs ...string)

Unsetenv unsets given envs

func ViaTor

func ViaTor() (addr string, err error)

ViaTor checks the Tor proxy whether running. Host and port listening by Tor defaults to 127.0.0.1 and 9050, they can be specific with `TOR_HOST` and `TOR_SOCKS_PORT` environments.

ViaTor returns address used by Tor, and an error if Tor proxy missing.

func WebPToPNG

func WebPToPNG(src, dst string) error

WebPToPNG convert WebP to PNG

func Writable

func Writable(dir string) error

Writable ensures the directory exists and is writable

func WriteFile

func WriteFile(path string, data []byte, mode os.FileMode) error

WriteFile writes byte slices to a specified path; it will be created if it does not exist. It returns an error.

Types

type RewriteTransport

type RewriteTransport struct {
	Transport http.RoundTripper
}

RewriteTransport rewrites https requests to http to avoid TLS cert issues during testing.

func (*RewriteTransport) RoundTrip

func (t *RewriteTransport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip rewrites the request scheme to http and calls through to the composed RoundTripper or if it is nil, to the http.DefaultTransport.

Jump to

Keyboard shortcuts

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