u

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

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

Go to latest
Published: Apr 10, 2022 License: MIT Imports: 24 Imported by: 10

README

This has moved to https://github.com/kjk/common (u)

Go utility functions that I use in multiple projects.

Documentation:

It contains functions to help writing equivalent of wc -l in Go:

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ServerIPAddress  string
	IdentityFilePath string
)
View Source
var (
	LogFile io.Writer
)

Functions

func AreFilesEuqalMust

func AreFilesEuqalMust(path1, path2 string) bool

func Assert

func Assert(ok bool, format string, args ...interface{})

func CdUpDir

func CdUpDir(dirName string)

we are executed for do/ directory so top dir is parent dir

func CloseNoError

func CloseNoError(f io.Closer)

CloseNoError is like io.Closer Close() but ignores an error use as: defer CloseNoError(f)

func CopyAndExecServerScript

func CopyAndExecServerScript(scriptLocalPath, user string)

CopyAndExecServerScript copies a given script to the server and executes it under a given user name

func CopyFile

func CopyFile(dst, src string) error

CopyFile copies a file from src to dst

func CopyFileMust

func CopyFileMust(dst, src string)

CopyFileMust copies a file from src to dst

func CreateDir

func CreateDir(dir string) error

CreateDir creates a directory if it doesn't exist

func CreateDirForFile

func CreateDirForFile(path string) error

CreateDirForFile creates intermediary directories for a file

func CreateDirForFileMust

func CreateDirForFileMust(path string) string

CreateDirForFileMust is like CreateDirForFile. Panics on error.

func CreateDirMust

func CreateDirMust(path string) string

CreateDirMust creates a directory. Panics on error

func CreateZipFile

func CreateZipFile(dst string, baseDir string, toZip ...string)

toZip is a list of files and directories in baseDir Directories are added recursively

func CreateZipWithDirContent

func CreateZipWithDirContent(zipFilePath, dirToZip string) error

CreateZipWithDirContent creates a zip file with the content of a directory. The names of files inside the zip file are relatitve to dirToZip e.g. if dirToZip is foo and there is a file foo/bar.txt, the name in the zip will be bar.txt

func CurrDirAbsMust

func CurrDirAbsMust() string

CurrDirAbsMust returns absolute path of the current directory

func DecodeBase64

func DecodeBase64(s string) (int, error)

DecodeBase64 decodes base64 string

func DeleteFilesIf

func DeleteFilesIf(dir string, shouldDelete func(os.FileInfo) bool) error

DeleteFilesIf deletes a files in a given directory if shouldDelete callback returns true

func DirCopyRecur

func DirCopyRecur(dstDir, srcDir string, shouldCopyFn func(path string) bool) ([]string, error)

func DirCopyRecurMust

func DirCopyRecurMust(dstDir, srcDir string, shouldCopyFn func(path string) bool) []string

func DirExists

func DirExists(path string) bool

DirExists returns true if a given path exists and is a directory

func DurationToString

func DurationToString(d time.Duration) string

DurationToString converts duration to a string

func EncodeBase64

func EncodeBase64(n int) string

EncodeBase64 encodes n as base64

func EnsureGitClean

func EnsureGitClean(dir string)

func ExpandTildeInPath

func ExpandTildeInPath(s string) string

ExpandTildeInPath converts ~ to $HOME

func FileExists

func FileExists(path string) bool

FileExists returns true if a given path exists and is a file

func FileLineCount

func FileLineCount(path string) (int, error)

FileLineCount returns number of lines in a file

func FilesSameSize

func FilesSameSize(path1, path2 string) bool

func FmtArgs

func FmtArgs(args ...interface{}) string

FmtArgs formats args as a string. First argument should be format string and the rest are arguments to the format

func FmtCmdShort

func FmtCmdShort(cmd exec.Cmd) string

FmtCmdShort formats exec.Cmd in a short way

func FmtSizeHuman

func FmtSizeHuman(size int64) string

func FmtSmart

func FmtSmart(format string, args ...interface{}) string

FmtSmart avoids formatting if only format is given

func FormatDuration

func FormatDuration(d time.Duration) string

FormatDuration formats duration in a more human friendly way than time.Duration.String()

func GetFileSize

func GetFileSize(path string) (int64, error)

GetFileSize returns size of the file

func GitPullMust

func GitPullMust(dir string)

func GitStatusMust

func GitStatusMust(dir string) string

func GzipFile

func GzipFile(dstPath, srcPath string) error

GzipFile compresses srcPath with gzip and saves as dstPath

func IndentStr

func IndentStr(n int) string

IndentStr returns an indentation string which has (2*n) spaces

func IsGitClean

func IsGitClean(dir string) bool

func IsLinux

func IsLinux() bool

IsLinux returns true if running on linux

func IsMac

func IsMac() bool

IsMac returns true if running on mac

func IsMinioNotExistsError

func IsMinioNotExistsError(err error) bool

IsMinioNotExistsError returns true if an error indicates that a key doesn't exist in storage

func ListFilesInDir

func ListFilesInDir(dir string, recursive bool) []string

ListFilesInDir returns a list of files in a directory

func Logf

func Logf(format string, args ...interface{})

a centralized place allows us to tweak logging, if need be

func LoginAsRoot

func LoginAsRoot()

func MakeExecScript

func MakeExecScript(name string) string

func MimeTypeFromFileName

func MimeTypeFromFileName(path string) string

func Must

func Must(err error)

func NormalizeNewlines

func NormalizeNewlines(d []byte) []byte

NormalizeNewLines changes CR and CRLF into LF

func OpenBrowser

func OpenBrowser(url string) error

OpenBrowsers open web browser with a given url (can be http:// or file://)

func OpenCodeDiffMust

func OpenCodeDiffMust(path1, path2 string)

func OpenFileMaybeCompressed

func OpenFileMaybeCompressed(path string) (io.ReadCloser, error)

OpenFileMaybeCompressed opens a file that might be compressed with gzip or bzip2. TODO: could sniff file content instead of checking file extension

func OpenNotepadWithFileMust

func OpenNotepadWithFileMust(path string)

func PanicIf

func PanicIf(cond bool, args ...interface{})

PanicIf panics if cond is true

func PanicIfErr

func PanicIfErr(err error, args ...interface{})

PanicIfErr panics if err is not nil

func PathExists

func PathExists(path string) bool

PathExists returns true if a filesystem path exists Treats any error (e.g. lack of access due to permissions) as non-existence

func PathIsDir

func PathIsDir(path string) (isDir bool, err error)

PathIsDir returns true if a path exists and is a directory Returns false, nil if a path exists and is not a directory (e.g. a file) Returns undefined, error if there was an error e.g. because a path doesn't exists

func PathMatchesExtensions

func PathMatchesExtensions(path string, extensions []string) bool

PathMatchesExtensions returns true if path matches any of the extensions

func PrintFileSize

func PrintFileSize(path string)

func PrintLineStats

func PrintLineStats(stats *LineStats)

func ReadFileMaybeCompressed

func ReadFileMaybeCompressed(path string) ([]byte, error)

ReadFileMaybeCompressed reads file. Ungzips if it's gzipped.

func ReadFileMust

func ReadFileMust(path string) []byte

ReadFileMust reads data from a file

func ReadLinesFromFile

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

ReadLinesFromFile reads all lines from a file. Newlines are not included.

func ReadLinesFromReader

func ReadLinesFromReader(r io.Reader) ([]string, error)

ReadLinesFromReader reads all lines from io.Reader. Newlines are not included.

func ReadZipFileMust

func ReadZipFileMust(path string) map[string][]byte

func RemoveDuplicateStrings

func RemoveDuplicateStrings(a []string) []string

RemoveDuplicateStrings removes duplicate strings from an array of strings. It's optimized for the case of no duplicates. It modifes a in place.

func RemoveFileLogged

func RemoveFileLogged(path string)

RemoveFileLogged removes a file and logs the action

func RemoveFilesInDirMust

func RemoveFilesInDirMust(dir string)

RemoveFilesInDirMust removes all files in a directory (but not sub-directories)

func RequestGetFullHost

func RequestGetFullHost(r *http.Request) string

RequestGetFullHost returns full host name e.g. "https://blog.kowalczyk.info/"

func RequestGetProtocol

func RequestGetProtocol(r *http.Request) string

RequestGetProtocol returns protocol under which the request is being served i.e. "http" or "https"

func RequestGetRemoteAddress

func RequestGetRemoteAddress(r *http.Request) string

RequestGetRemoteAddress returns ip address of the client making the request, taking into account http proxies

func RunCmdLoggedMust

func RunCmdLoggedMust(cmd *exec.Cmd) string

RunCmdLoggedMust runs a command and returns its stdout Shows output as it happens

func RunCmdMust

func RunCmdMust(cmd *exec.Cmd) string

RunCmdMust runs a command and returns its stdout

func ScpCopy

func ScpCopy(localSrcPath string, serverDstPath string)

"-o StrictHostKeyChecking=no" is for the benefit of CI which start fresh environment

func SetPublicObjectMetadata

func SetPublicObjectMetadata(opts *minio.PutObjectOptions)

SetPublicObjectMetadata sets options that mark object as public for doing put operation

func Sha1HexOfBytes

func Sha1HexOfBytes(data []byte) string

Sha1HexOfBytes returns 40-byte hex sha1 of bytes

func Sha1HexOfFile

func Sha1HexOfFile(path string) (string, error)

Sha1HexOfFile returns 40-byte hex sha1 of file content

func Sha1OfBytes

func Sha1OfBytes(data []byte) []byte

Sha1OfBytes returns 20-byte sha1 of bytes

func Sha1OfFile

func Sha1OfFile(path string) ([]byte, error)

Sha1OfFile returns 20-byte sha1 of file content

func SshExec

func SshExec(user string, script string)

"-o StrictHostKeyChecking=no" is for the benefit of CI which start fresh environment

func SshInteractive

func SshInteractive(user string)

func StringInSlice

func StringInSlice(a []string, toCheck string) bool

StringInSlice returns true if a string is present in slice

func StringRemoveFromSlice

func StringRemoveFromSlice(a []string, toRemove string) ([]string, bool)

StringRemoveFromSlice removes a given string from a slice of strings returns a (potentially) new slice and true if was removed

func StringsRemoveFirst

func StringsRemoveFirst(a []string) []string

StringsRemoveFirst removes first sstring from the slice

func TimeSinceNowAsString

func TimeSinceNowAsString(t time.Time) string

TimeSinceNowAsString returns string version of time since a ginve timestamp

func UserHomeDirMust

func UserHomeDirMust() string

UserHomeDir returns $HOME diretory of the user

func UtcNow

func UtcNow() time.Time

UtcNow returns current time in UTC

func WaitForCtrlC

func WaitForCtrlC()

WaitForCtrlC waits until a user presses Ctrl-C

func WriteFileCreateDirMust

func WriteFileCreateDirMust(d []byte, path string) error

WriteFileCreateDirMust is like ioutil.WriteFile() but also creates intermediary directories

func WriteFileGzipped

func WriteFileGzipped(path string, data []byte) error

WriteFileGzipped writes data to a path, using best gzip compression

func WriteFileMust

func WriteFileMust(path string, data []byte)

WriteFileMust writes data to a file

Types

type FileWalk

type FileWalk struct {
	FilesChan chan *FileWalkEntry
	// contains filtered or unexported fields
}

FileWalk describes a file traversal

func StartFileWalk

func StartFileWalk(startDir string) *FileWalk

StartFileWalk starts a file traversal from startDir

func (*FileWalk) Stop

func (ft *FileWalk) Stop()

Stop stops file traversal

type FileWalkEntry

type FileWalkEntry struct {
	Dir      string
	FileInfo os.FileInfo
}

FileWalkEntry describes a single file from FileWalk

func (*FileWalkEntry) Path

func (e *FileWalkEntry) Path() string

Path returns full path of the file

type FilterFunc

type FilterFunc func(string) bool

func MakeAllowedFileFilterForExts

func MakeAllowedFileFilterForExts(exts ...string) FilterFunc

func MakeExcludeDirsFilter

func MakeExcludeDirsFilter(dirs ...string) FilterFunc

func MakeFilterAnd

func MakeFilterAnd(filters ...FilterFunc) FilterFunc

func MakeFilterOr

func MakeFilterOr(filters ...FilterFunc) FilterFunc

type FormattedDuration

type FormattedDuration time.Duration

time.Duration with a better string representation

func (FormattedDuration) String

func (d FormattedDuration) String() string

type LineCount

type LineCount struct {
	Name      string
	Ext       string
	LineCount int
}

LineCount describes line count for a file

type LineStats

type LineStats struct {
	FileToCount map[string]*LineCount
}

LineStats gathers line count info for files

func NewLineStats

func NewLineStats() *LineStats

NewLineStats returns new LineStats

func (*LineStats) CalcInDir

func (s *LineStats) CalcInDir(dir string, allowedFileFilter func(name string) bool, recur bool) error

type MinioClient

type MinioClient struct {
	StorageKey    string
	StorageSecret string
	Bucket        string
	Endpoint      string // e.g. "nyc3.digitaloceanspaces.com"
	Secure        bool
	// contains filtered or unexported fields
}

MinioClient represents s3/spaces etc. client

func (*MinioClient) Delete

func (c *MinioClient) Delete(remotePath string) error

func (*MinioClient) DownloadFileAsData

func (c *MinioClient) DownloadFileAsData(remotePath string) ([]byte, error)

func (*MinioClient) DownloadFileAtomically

func (c *MinioClient) DownloadFileAtomically(dstPath string, remotePath string) error

func (*MinioClient) EnsureConfigured

func (c *MinioClient) EnsureConfigured()

EnsureCondfigured will panic if client not configured

func (*MinioClient) GetClient

func (c *MinioClient) GetClient() (*minio.Client, error)

GetClient returns a (cached) minio client

func (*MinioClient) ListRemoteFiles

func (c *MinioClient) ListRemoteFiles(prefix string) ([]*minio.ObjectInfo, error)

ListRemoveFiles returns a list of files under a given prefix

func (*MinioClient) StatObject

func (c *MinioClient) StatObject(remotePath string) (minio.ObjectInfo, error)

func (*MinioClient) URLBase

func (c *MinioClient) URLBase() string

URLBase returns base of url under which files are accesible (if public it's a publicly available file)

func (*MinioClient) UploadData

func (c *MinioClient) UploadData(remotePath string, d []byte, opts minio.PutObjectOptions) error

func (*MinioClient) UploadDataPublic

func (c *MinioClient) UploadDataPublic(remotePath string, d []byte) error

func (*MinioClient) UploadFile

func (c *MinioClient) UploadFile(remotePath string, filePath string, public bool) error

func (*MinioClient) UploadFilePrivate

func (c *MinioClient) UploadFilePrivate(remotePath string, filePath string) error

func (*MinioClient) UploadFilePublic

func (c *MinioClient) UploadFilePublic(remotePath string, filePath string) error

func (*MinioClient) UploadReader

func (c *MinioClient) UploadReader(remotePath string, r io.Reader, size int64, public bool, contentType string) error

func (*MinioClient) UploadReaderPrivate

func (c *MinioClient) UploadReaderPrivate(remotePath string, r io.Reader, size int64, contentType string) error

func (*MinioClient) UploadReaderPublic

func (c *MinioClient) UploadReaderPublic(remotePath string, r io.Reader, size int64, contentType string) error

func (*MinioClient) UploadStringPrivate

func (c *MinioClient) UploadStringPrivate(remotePath string, s string) error

func (*MinioClient) UploadStringPublic

func (c *MinioClient) UploadStringPublic(remotePath string, s string) error

Jump to

Keyboard shortcuts

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