utils

package
v0.1.18 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2020 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContainsInt

func ContainsInt(needle int, haystack []int) bool

func DedupeStrings added in v0.1.1

func DedupeStrings(values []string, keyGetter ...StringKeyGetter) []string

func Download

func Download(options DownloadOptions) error

Download downloads a file from the url in options.URL and places the content in the file at options.FilePath

Made possible with guidance from: https://golangcode.com/download-a-file-with-progress/

func EnsureDirectoryExists added in v0.1.18

func EnsureDirectoryExists(pathToDirectory string) error

EnsureDirectoryExists ensures that a directory exists at :pathToDirectory, returning an error only if a directory cannot be ensured to exist there

func FindParentContainingChildDirectory added in v0.0.4

func FindParentContainingChildDirectory(targetDirectoryName, startingFrom string, levels ...int) (string, error)

FindParentContainingChildDirectory continously ascends from the initial directory path at `startingFrom` and checks for the existence of a child directory named `targetDirectoryName` for `levels[0]` levels.

On successfully finding such a child directory, it returns the path of the parent directory as the first argument, on failing to find, it returns an empty string. In both cases, the returned `error`-typed argument will be nil.

If the function failed to complete, the second `error`-typed argument will be non-nil.

func GetHttpCloneUrlFromHttpLink(httpLinkUrl string) (string, error)

func GetHttpLinkFromHttpCloneUrl

func GetHttpLinkFromHttpCloneUrl(httpCloneUrl string) (string, error)

func GetHttpLinkFromSshCloneUrl

func GetHttpLinkFromSshCloneUrl(sshCloneUrl string) (string, error)

func GetLocalSSHKeys added in v0.0.13

func GetLocalSSHKeys(keysDirectory string) ([]string, map[string]string, error)

func GetNKeyValuePairsStringMap added in v0.0.18

func GetNKeyValuePairsStringMap(kvd map[string]string) int

GetNKeyValuePairsStringMap returns the number of key-value mappings stored in the provided `kvd` (key-value dictionary)

func GetSshCloneUrlFromHttpLinkUrl

func GetSshCloneUrlFromHttpLinkUrl(httpLinkUrl string) (string, error)

func GitClone added in v0.1.9

func GitClone(cloneURL, localPath string) error

GitClone does a `git clone` on the provided :cloneURL into the directory at :localPath. If the remote is a bare repository, a new repository will be initialised at :localPath with the `origin` remote set to :cloneURL

func GitInit added in v0.1.18

func GitInit(cloneURL, localPath string) error

func HTTPGet

func HTTPGet(targetURL url.URL, headers map[string]string) (*http.Response, error)

func IsDirectoryEmpty added in v0.1.18

func IsDirectoryEmpty(pathToDirectory string) (bool, error)

IsDirectoryEmpty returns whether the provided directory located at :pathToDirectory is empty or not. If the error return is not nil, this indicates a system-type error

func IsEmptyString added in v0.0.16

func IsEmptyString(test string) bool

func IsPathType added in v0.0.16

func IsPathType(pathInfo os.FileInfo, pathType PathType) bool

func OpenURIWithDefaultApplication

func OpenURIWithDefaultApplication(targetURI string) error

OpenURIWithDefaultApplication runs xdg-open on linux, open on macos, and rundll32.exe on windows with the provided :targetURI as an argument

func PathExists added in v0.0.16

func PathExists(asType PathType, pathFragments ...string) (bool, error)

func ResolvePath added in v0.0.14

func ResolvePath(relativePathFragments ...string) (string, error)

func Untar

func Untar(options UntarOptions) []error

Untar takes the .zip file located at options.InputPath and untars its contents to options.OutputPath; returns an error if something unexpected happened, or nil if all is well

Made possible with guidance from: https://medium.com/@skdomino/taring-untaring-files-in-go-6b07cf56bc07

func Unzip

func Unzip(options UnzipOptions) []error

Unzip takes the .zip file located at options.InputPath and unzips its contents to options.OutputPath; returns an error if something unexpected happened, or nil if all is well

Made possible with guidance from: https://golangcode.com/unzip-files-in-go/

Types

type DownloadEvent

type DownloadEvent struct {
	State        DownloadState   `json:"download_state"`
	URL          string          `json:"url"`
	FilePath     string          `json:"file_path"`
	TempFilePath string          `json:"temp_file_path"`
	Status       *DownloadStatus `json:"status"`
}

func (DownloadEvent) String

func (de DownloadEvent) String() string

type DownloadOptions

type DownloadOptions struct {
	Events               chan DownloadEvent
	EventsUpdateInterval time.Duration
	FilePath             string
	URL                  string
	Overwrite            bool
}

type DownloadState

type DownloadState string
const (
	DefaultEventsUpdateInterval               = 500 * time.Millisecond
	DownloadStateStarting       DownloadState = "download_starting"
	DownloadStateReport         DownloadState = "download_report"
	DownloadStateError          DownloadState = "download_error"
	DownloadStateFailed         DownloadState = "download_failed"
	DownloadStateSuccess        DownloadState = "download_success"
)

type DownloadStatus

type DownloadStatus struct {
	TotalBytes     uint64 `json:"total_bytes"`
	ProcessedBytes uint64 `json:"processed_bytes"`
}

func (DownloadStatus) GetPercentage

func (ds DownloadStatus) GetPercentage() float64

func (*DownloadStatus) Write

func (ds *DownloadStatus) Write(content []byte) (int, error)

Write implements io.Writer

type PathType added in v0.0.16

type PathType string

func (PathType) String added in v0.0.16

func (pt PathType) String() string

type StringKeyGetter added in v0.1.1

type StringKeyGetter func(string) string

type UntarEvent

type UntarEvent struct {
	// State is a string code that indicates the underlying operation
	State UntarState
	// Path is a string indicating path to a file if it's non-empty
	Path string
	// Message is an arbitrary string
	Message string
	// Status should provide metadata on the underlying operation
	Status *UntarStatus
}

UntarEvent is an object that is passed to the events stream for the consumer to know what's going on inside

type UntarOptions

type UntarOptions struct {
	// Events, if populated, receives events for logging purposes
	Events chan UntarEvent
	// InputPath defines the path to the .zip file we want to untar
	InputPath string
	// OutputPath defines the path to a directory where the untarred files
	// should go
	OutputPath string
	// ReturnOnFileError indicates whether to return an error instantly
	// when an error is encountered
	ReturnOnFileError bool
}

type UntarState

type UntarState string
const (
	UntarStateStarting   UntarState = "untar_starting"
	UntarStateProcessing UntarState = "untar_processing"
	UntarStateError      UntarState = "untar_error"
	UntarStateOK         UntarState = "untar_ok"
	UntarStateStatus     UntarState = "untar_status"
	UntarStateSuccess    UntarState = "untar_success"
	UntarStateFailed     UntarState = "untar_failed"
)

type UntarStatus

type UntarStatus struct {
	// BytesTotal is the total number of bytes to extract
	BytesTotal int64
	// BytesProcessed is the number of bytes processed so far
	BytesProcessed int64
	// FilesTotalCount is the total number of files to extract
	FilesTotalCount int
	// FilesProcessedCount is the number of files processed so far
	FilesProcessedCount int
}

UntarStatus stores the status of the untarring process and is returned through the UntarEvent object

func (UntarStatus) GetPercentDoneByBytes

func (us UntarStatus) GetPercentDoneByBytes() float64

func (UntarStatus) GetPercentDoneByFiles

func (us UntarStatus) GetPercentDoneByFiles() float64

type UnzipEvent

type UnzipEvent struct {
	// State is a string code that indicates the underlying operation
	State UnzipState
	// Path is a string indicating path to a file if it's non-empty
	Path string
	// Message is an arbitrary string
	Message string
	// Status should provide metadata on the underlying operation
	Status *UnzipStatus
}

UnzipEvent is an object that is passed to the events stream for the consumer to know what's going on inside

type UnzipOptions

type UnzipOptions struct {
	// Events, if populated, receives events for logging purposes
	Events chan UnzipEvent
	// InputPath defines the path to the .zip file we want to unzip
	InputPath string
	// OutputPath defines the path to a directory where the unzipped files
	// should go
	OutputPath string
	// ReturnOnFileError indicates whether to return an error instantly
	// when an error is encountered
	ReturnOnFileError bool
}

type UnzipState

type UnzipState string
const (
	UnzipStateStarting   UnzipState = "unzip_starting"
	UnzipStateProcessing UnzipState = "unzip_processing"
	UnzipStateError      UnzipState = "unzip_error"
	UnzipStateOK         UnzipState = "unzip_ok"
	UnzipStateStatus     UnzipState = "unzip_status"
	UnzipStateSuccess    UnzipState = "unzip_success"
	UnzipStateFailed     UnzipState = "unzip_failed"
)

type UnzipStatus

type UnzipStatus struct {
	// BytesTotal is the total number of bytes to extract
	BytesTotal int64
	// BytesProcessed is the number of bytes processed so far
	BytesProcessed int64
	// FilesTotalCount is the total number of files to extract
	FilesTotalCount int
	// FilesProcessedCount is the number of files processed so far
	FilesProcessedCount int
}

UnzipStatus stores the status of the unzipping process and is returned through the UnzipEvent object

func (UnzipStatus) GetPercentDoneByBytes

func (us UnzipStatus) GetPercentDoneByBytes() float64

func (UnzipStatus) GetPercentDoneByFiles

func (us UnzipStatus) GetPercentDoneByFiles() float64

Jump to

Keyboard shortcuts

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