pathhelper

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2021 License: MIT Imports: 23 Imported by: 0

README

pathhelper

path helper utility tool

Git Clone

git clone https://gitlab.com/evatix-go/pathhelper.git

Prerequisites
  • Either add your ssh key to your gitlab account
  • Or, use your access token to clone it.

Installation

go get gitlab.com/evatix-go/pathhelper

Why pathhelper?

Package pathhelper provides an easy and fast way to get your desired OS(operating system) functionality without the hassle of considering the OS you are on, what GO packages you may need to decode a path or simply find out if the path or file exists etc. We have brought features of different packages and injected some new features to make this package a complete solution for obtaining information regarding filepath independent of platform.

Examples


func main() {
	// Checking if path is empty
	ispath.Empty("") // returns true

	samplePath := "C:\\users\\"

	// Checking if path exists
	exists := ispath.Exists(samplePath) // .File / .Directory / .AnyExists ...
	fmt.Println(exists) // returns true if directory or file exist on that path

	// Getting path as URI
	fmt.Println(pathhelper.GetPathAsUri(samplePath, true)) // file:///c:/users

	// Normalize path
	pathToNormalize := "file:///C:/something/otherthing"	
	fmt.Println(normalize.Path(pathToNormalize)) // C:\something\otherthing if OS is windows; C:/something/otherthing if OS is Unix

	// Create Directory
	createdir.New("SampleNewDir", filemodes.AllRead) // Creates a directory named "SampleNewDir" with AllRead access at the folder where the function is called

	// Environmental variables
	envpath.GetExecutableEnvironmentPathCollection() // Outputs a struct containing the environment variable paths

	// Check directory validity
	fmt.Println(dirinfo.New("c:\\windows\\py.exe").IsValidDir) // outputs a bool

	// Gets all paths recursively and continues on error as per provided argument
	fmt.Println(recursivepaths.All("D:\\SampleDir", true)) // &[\\?\D:\SampleDir \\?\D:\SampleDir\sampleFile.txt] # Error Wrappers - Collection - Length[0]
	fmt.Println(recursivepaths.All(" ", false))		// panics with detailed message

	// Getting path as wrapper
	fmt.Println(unipath.New("\\").Add("c://windows").Add("\\sys32").GetAsPathWrapper().String()) // \\?\c:\windows\sys32 on windows based on separator given
	// "\\?\c:\windows\sys32\sys64\somethingnew" because "\\" separator given
	// "c:/windows/sys32/sys64/somethingnew" because "/" separator given
	fmt.Println(unipath.
		New("\\").
		Add("c://windows").
		Add("\\sys32//").
		Add("\\sys64//").
		Add("somethingnew").
		String()) // .ToString(...) Can change separator for new path
	
	// "c:/windows/sys32/sys64", 
	// "somethingnew" not printed because we skipped it
	fmt.Println(unipath.
		New("/").
		Add("c://windows").
		Add("\\sys32//").
		Add("//sys64\\").
		Add("somethingnew").
		ToStringUptoLastMinus(1, "/",true))
}

Acknowledgement

For this package we have mainly used OS and filepath packages of GO. For testing the package we have used the very convenient package Go Convey.

Notes

Contributors

License

Evatix MIT License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateSymbolicLinkLinux

func CreateSymbolicLinkLinux(sourcePath, destinationPath string) *errbool.Result

Creates symbolicLink of the source at the provided destination path for linux system. If destination doesn't exist it will panic. sourcePath example: "/home/a/test.txt"; destinationPath example: "/home/a/go/test.txt" destination need to have read and write permission for the user.

func GetAbsoluteFromExecutableDirectoryPath

func GetAbsoluteFromExecutableDirectoryPath(relativePath string, isLongPathFix, isNormalize bool) string

func GetAbsolutePath

func GetAbsolutePath(
	basePath,
	relativePath string,
	isLongPathFix, isNormalize bool,
) string

func GetAbsolutePaths

func GetAbsolutePaths(
	basePath string,
	isLongPathFix, isNormalize bool,
	relativePaths ...string,
) *[]string

func GetAbsolutePathsOfPtr

func GetAbsolutePathsOfPtr(
	basePath string,
	isLongPathFix, isNormalize bool,
	relativePaths *[]string,
) *[]string

func GetAsyncProcessed

func GetAsyncProcessed(
	processingPaths *[]string,
	processor pathfuncs.Processor,
) *[]string

Don't modify existing paths and creates new one.

func GetBaseDir

func GetBaseDir(currentPath string) (baseDir string)

No slash at the end reference example : https://play.golang.org/p/EMbLKv5Jyqe

func GetCombinePathFromGoPath

func GetCombinePathFromGoPath(
	pathConfig *pathhelpercore.PathConfig,
	givenPaths ...string,
) string

By defaultPathConfig: Separator = os.PathSeparator IsNormalize = true IsIgnoreEmptyPath = false

func GetCombinePathsWith

func GetCombinePathsWith(paths ...string) string

Returns path as string after combining all provided paths By default isIgnorePath: true, isNormalize: true, and Path separator depends on the OS

func GetCombinePathsWithPtr

func GetCombinePathsWithPtr(paths *[]string) string

Returns path as string after combining all provided paths By default isIgnorePath: true, isNormalize: true, and Path separator depends on the OS

func GetCombinedOfNonEmptyPaths

func GetCombinedOfNonEmptyPaths(separator string, paths []string) string

func GetCombinedPath

func GetCombinedPath(
	separator string,
	isIgnoreEmptyPath bool,
	isLongPathFix bool,
	isNormalize bool,
	paths ...string,
) string

@isIgnoreEmptyPath if true then ignore empty string (nil, "", or any empty spaces " ")

func GetCombinedPathUsingConfig

func GetCombinedPathUsingConfig(pathConfig *pathhelpercore.PathConfig, paths ...string) string

if pathConfig is nil then pathConfig gets created pathhelpercore.NewDefaultPathConfig() By NewDefaultPathConfig: Separator = os.PathSeparator IsNormalize = true IsIgnoreEmptyPath = false

func GetCompiledPath

func GetCompiledPath(
	pathTemplate string,
	compilingMap *map[string]string,
) string

func GetDotExtension added in v0.0.4

func GetDotExtension(
	currentPath string,
) string

func GetEachPathConvertedToFileInfoWrapper

func GetEachPathConvertedToFileInfoWrapper(fullPaths *[]string) *[]*fileinfo.Wrapper

For each path converted to file info wrapper and finally returns as an array.

func GetExecutableCombinePath

func GetExecutableCombinePath(paths ...string) string

Get the final path exepath + ....

func GetExecutableDirectory

func GetExecutableDirectory() string

Represents the directory where the application is running from. Returns without slash

func GetExecutablePath

func GetExecutablePath() string

Represents the exact path to the executable file Exact path to the executable not the path but the file.

func GetExtensions added in v0.0.4

func GetExtensions(
	currentPath string,
) (
	dotExt, ext string,
)

func GetExtensionsWithoutDot added in v0.0.4

func GetExtensionsWithoutDot(
	currentPath string,
) string

func GetFileInfoWrapper

func GetFileInfoWrapper(path string) *fileinfo.Wrapper

func GetFileInfoWrappersArrayOf

func GetFileInfoWrappersArrayOf(fullPaths *[]string) *[]*fileinfo.Wrappers

Each path can give number wrappers from GetFileInfoWrappersFrom This method call GetFileInfoWrappersFrom for each path given and returns as an array of it.

func GetFileInfoWrappersFrom

func GetFileInfoWrappersFrom(path string) *fileinfo.Wrappers

func GetFileNameWithExt

func GetFileNameWithExt(currentPath string) (fileName string)

reference example : https://play.golang.org/p/EMbLKv5Jyqe

func GetFileNameWithoutExt

func GetFileNameWithoutExt(currentPath string) (filename string)

invalid ext should return empty string. reference example : https://play.golang.org/p/EMbLKv5Jyqe

func GetFileNames

func GetFileNames(path string) *fileinfo.FileNamesCollection

returns file names on the path (non-lazy execution).

func GetFilenamePlusBothExtensions added in v0.0.4

func GetFilenamePlusBothExtensions(
	currentPath string,
) (
	fileName,
	dotExt,
	ext string,
)

func GetLongPathFixed

func GetLongPathFixed(isForce bool, givenAbsolutePath string) string

Adds constants.LongPathQuestionMarkPrefix if path is longer than 255 and doesn't already contains it. Ignores prefix apply if already has it (constants.LongPathUncPrefix or constants.LongPathQuestionMarkPrefix ) or path is empty or length less than 255 if path starts with `\\` then replaces with constants.LongPathUncPrefix if forced then if Windows then fix added anyway.

func GetLongPathsFixedOf

func GetLongPathsFixedOf(
	isForce bool,
	pathSeparator string,
	givenAbsolutePaths ...string,
) *[]string

Adds constants.LongPathQuestionMarkPrefix if path is longer than 255 and doesn't already contains it. Ignores prefix apply if already has it (constants.LongPathUncPrefix or constants.LongPathQuestionMarkPrefix ) or path is empty or length less than 255 if path starts with `\\` then replaces with constants.LongPathUncPrefix if forced then if Windows then fix added anyway.

func GetLongPathsFixedOfPtr

func GetLongPathsFixedOfPtr(
	isForce bool,
	pathSeparator string,
	givenAbsolutePaths *[]string,
) *[]string

Adds constants.LongPathQuestionMarkPrefix if path is longer than 255 and doesn't already contains it. Ignores prefix apply if already has it (constants.LongPathUncPrefix or constants.LongPathQuestionMarkPrefix ) or path is empty or length less than 255 if path starts with `\\` then replaces with constants.LongPathUncPrefix if forced then if Windows then fix added anyway.

func GetPathAsUri

func GetPathAsUri(path string, isNormalizePath bool) string

func GetPathAsUrisOf

func GetPathAsUrisOf(
	isNormalizePath bool,
	paths ...string,
) *[]string

func GetPathAsUrisOfPtr

func GetPathAsUrisOfPtr(
	isNormalizePath bool,
	paths *[]string,
) *[]string

func GetPathFromUri

func GetPathFromUri(path string, isNormalizePath bool) string

GetPathFromUri function takes a path (string) and a bool input and returns a string after removing Uri prefixes (file:///, file://). If bool is set to true any double separator is also removed from returned string

func GetRelativePath

func GetRelativePath(fullPath, basePath string) string

func GetRelativePaths

func GetRelativePaths(
	rootPath string,
	fullPaths ...string,
) *[]string

func GetRelativePathsOfPtr

func GetRelativePathsOfPtr(
	rootPath string,
	fullPaths *[]string,
) *[]string

func GetSlug

func GetSlug(path, separatorOfChoice string) string

GetSlug from given path, usages @slugForbiddenArray to replace with @separatorOfChoice

func GetSlugsOf

func GetSlugsOf(
	separatorOfChoice string,
	paths ...string,
) *[]string

func GetSlugsOfPtr

func GetSlugsOfPtr(
	separatorOfChoice string,
	paths *[]string,
) *[]string

func GetWithoutSlash

func GetWithoutSlash(currentPath string) (baseDir, fileName string)

func IndexOfDotFromLastFileName added in v0.0.4

func IndexOfDotFromLastFileName(currentPath string) (fileName string, index int)

returns -1 if last file name doesn't have any extension reference example : https://play.golang.org/p/EMbLKv5Jyqe

func IsGoModuleOn

func IsGoModuleOn() bool

Returns env go bin path

func IsSymbolicLink(path string) bool

func RemoveDoubleUriSeparator

func RemoveDoubleUriSeparator(path, separator string) string

func RemoveFromPath

func RemoveFromPath(
	pathTemplate string,
	removingList *[]string,
	isNormalizePath bool,
) string

Given removingList array items will be replaced with "" empty string. If pathTemplate is given as empty string or nil or whitespace then returns as is.

func Split

func Split(currentPath string) (baseDir, fileName string)

returns base dir with slash

func SplitWithoutSlash

func SplitWithoutSlash(currentPath string) (baseDir, fileName string)

reference example : https://play.golang.org/p/EMbLKv5Jyqe

Types

This section is empty.

Jump to

Keyboard shortcuts

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