util

package
v0.6.3 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2024 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrFileWithThisDirNameExist occurs when file already exist.
	ErrFileWithThisDirNameExist = fmt.Errorf("file with the same name exists")
	// ErrFileNotFound occurs when file is not found.
	ErrFileNotFound = func(s string) error {
		return fmt.Errorf("file not found: %s", s)
	}
)
View Source
var FS = &afero.Afero{Fs: afero.NewOsFs()}

FS is the implementation of Afero Filesystem. It's a filesystem wrapper and used for testing.

Functions

func AppendToFileOrCreateDirAndWriteToFile

func AppendToFileOrCreateDirAndWriteToFile(bytes []byte, file string, perms ...os.FileMode) error

AppendToFileOrCreateDirAndWriteToFile creates the base directory and writes to a file in it. Perms are optional. The first permission will be the file permission, the second will be the directory permission.

func AskForConfirmation

func AskForConfirmation(msg string) bool

AskForConfirmation ask msg from the user and returns the answer.

func BoolPtr

func BoolPtr(b bool) *bool

func CheckFileExistsAndRecreate

func CheckFileExistsAndRecreate(file string) bool

CheckFileExistsAndRecreate checks if the file already exists and ask the user if he'd like to recreate it. If user input is yes - return false (as if the file does not existing).

func CheckRegexInFile

func CheckRegexInFile(regex, filePath string) (bool, error)

CheckRegexInFile checks if the file contains content.

func CheckRegexInString

func CheckRegexInString(regex, str string) bool

CheckRegexInString checks if the string contains content.

func CommandAvailable

func CommandAvailable(name string) bool

CommandAvailable returns if the parameter can be find in $PATH.

func ContainsString

func ContainsString(slice []string, vals ...string) bool

ContainsString checks if a slice of string contains a string.

func CreateDir

func CreateDir(dir string, perm *os.FileMode) error

CreateDir creates the directory if not exist.

func CreateDirAndWriteToFile

func CreateDirAndWriteToFile(bytes []byte, file string, perms ...os.FileMode) error

CreateDirAndWriteToFile creates the base directory and writes bytes to a file in it. Perms are optional. The first permission will be the file permission, the second will be the directory permission.

func DecompressFileFromArchive

func DecompressFileFromArchive(src io.Reader, archive, filename string) (io.Reader, error)

func DockerHost

func DockerHost() string

func EvalSymlinkPath

func EvalSymlinkPath(file string) (string, error)

EvalSymlinkPath checks if file exists and returns the resolved path if a symlink if file is a symlink, else it returns the filepath.

func ExtractUnknownArgs

func ExtractUnknownArgs(flags *pflag.FlagSet, args []string) []string

ExtractUnknownArgs returns []string arguments which are not used by pflags.

func FileExists

func FileExists(file string) bool

FileExists checks if the file already exists and ask the user if he'd like to recreate it.

func HomeDir

func HomeDir() string

HomeDir returns the invoking user's home directory.

func Insert

func Insert(array []string, index int, value string) []string

func InsertStringAfterOccurrence

func InsertStringAfterOccurrence(args []string, insertStr, searchStr string) []string

InsertStringAfterOccurrence inserts insertStr after the occurrence of searchStr to args and returns args. If searchStr does not exist it will append to the end of args.

func InsertStringBeforeOccurrence

func InsertStringBeforeOccurrence(args []string, insertStr, searchStr string) []string

InsertStringBeforeOccurrence inserts insertStr before occurrence of searchStr (if exist) to args and returns args. If searchStr doesn't exist it will append to the end of args.

func IsAdmin

func IsAdmin() bool

IsAdmin returns true if the user who runs the command is root.

func OSDistro

func OSDistro() string

OSDistro returns the linux distro name if GOOS is linux, else "darwin" or "windows".

func Quote

func Quote(s string) string

Quote puts a quote around s string in Unix-like systems and returns it, while it just returns s as-is on Windows.

func RemoveStringFromSlice

func RemoveStringFromSlice(s []string, r string) []string

func RunMeElevated

func RunMeElevated()

RunMeElevated does nothing on unix systems.

func Unzip

func Unzip(src io.Reader, dest string) ([]string, error)

Unzip will decompress a zip archive, moving all files and folders within the zip file (parameter 1) to an output directory (parameter 2).

Types

This section is empty.

Jump to

Keyboard shortcuts

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