friendly

package module
v2.1.1 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2022 License: GPL-3.0 Imports: 16 Imported by: 14

README

Friendly Go

License Go Version Lines Release Stars Contributors

A library with functions I use all the time in my projects.

Also useful if you are starting to learn Go and are annoyed by implementing basic things in your project all the time.

Install and Use

Warning: the v1 version is deprecated, please use friendly/v2 instead.

In your project directory, type

go get github.com/alexcoder04/friendly/v2
# or `go get github.com/alexcoder04/friendly/v2/...` depending on the sub-package you need

And then, in your code

package ...

import (
    ...
    "github.com/alexcoder04/friendly/v2"
    "github.com/alexcoder04/friendly/v2/ffiles"
)

...
folder, _ := friendly.Input()
if ffiles.IsDir(folder) {
    friendly.CompressFolder(folder, destination)
}
...

Documentation

See also the documentation at pkg.go.dev:

github.com/alexcoder04/friendly/v2

Please refer to pkg.go.dev for this section.

github.com/alexcoder04/friendly/v2/ffiles
// exist
func Exists(path string) (bool, error) { } // returns true error when cannot stat file (and error is not os.ErrNotExists)
func IsDir(path string) bool { } // true only when file exists and IS a directory
func IsFile(path string) bool { } // true only when file exists and is NOT a directory

// read-write
func ListFilesRecursively(folder string) ([]string, error) { }
func ReadLines(file string) ([]string, error) { }
func WriteLines(file string, lines []string) error { }
func WriteNewFile(file string, content string) error { }

// copy
func CopyFile(source string, destin string) error { }
func CopyFolder(source string, destination string) error { }

// locations
func GetConfigDirFor(program string) (string, error) { } // creates the directory if it doesn't exist
func GetCacheDirFor(program string) (string, error) { } // creates the directory if it doesn't exist
func GetLogDirFor(program string) (string, error) { } // creates the directory if it doesn't exist
func GetRuntimeDir() string { } // creates the directory if it doesn't exist
github.com/alexcoder04/friendly/v2/flinux
// desktop
func GetDisplayServer() string { }
func GuiRunning() bool { }

Contributing

If you use this library and are missing some feature - don't hesitate to open a pull request or an issue, I'm always looking forward to improve this project!

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ArrayContains

func ArrayContains[T comparable](arr []T, value T) bool

Checks if an element is present in an array. Can be used with any type thanks to generics.

func CapitalizeWord added in v2.1.0

func CapitalizeWord(word string) string

Capitalizes a first (makes first letter capital).

func CompressFolder

func CompressFolder(folder string, destination string) error

Zips a folder into a file at `destination`.

func Die added in v2.1.0

func Die(message string, args ...any)

Prints the provided error message and exits immediately. `Fatal error: <message>`

func DownloadFile

func DownloadFile(dlUrl string, path string) error

Downloads file from a url to given location.

func GetFromUrlBytes added in v2.1.0

func GetFromUrlBytes(dlUrl string) ([]byte, error)

Downloads data from a url and returns it as a bytes array.

func GetFromUrlString added in v2.1.0

func GetFromUrlString(dlUrl string) (string, error)

Downloads data from a url and returns it as a string.

func GetFullNameFromMailAddress added in v2.1.0

func GetFullNameFromMailAddress(address string) string

Capitalizes every part of between periods of the email address before the @-sign. Joins them with " ". Example: "john.doe@example.com" -> "John Doe".

func GetOutput

func GetOutput(commandLine []string, workingDir string) (string, error)

Runs a command and returns its output as a string.

func Getenv added in v2.1.1

func Getenv(key string, def string) string

Get environmental variable. If it is not defined (=""), return the provided default value.

func Getpwd

func Getpwd() string

Get current working directory without returning an error. If os.Getwd() throws an error, it tries os.UserHomeDir(). If it also throws an error, returns "".

func Input

func Input(prompt string) (string, error)

Gets user input from command-line (os.Stdin).

func IsInt

func IsInt(num string) bool

Checks whether a string represents a valid integer.

func Run

func Run(commandLine []string, workingDir string) error

Runs a command and prints its output to stdin.

func RunOnSignal added in v2.1.0

func RunOnSignal(s syscall.Signal, callback func())

Runs a function in new goroutine if gets an os signal.

func SafeCharsOnly added in v2.1.1

func SafeCharsOnly(str string, safe []byte) bool

Checks if a string contains only provided chars.

func SemVersionGreater

func SemVersionGreater(v1 string, v2 string) bool

Checks if first semantic version is greater than another.

func UncompressFolder

func UncompressFolder(source string, destination string) error

Unzips a file into `destination`.

func Warn added in v2.1.0

func Warn(message string, args ...any)

Prints a warning message. `Warning: <message>`

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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