pathhelper

package module
v0.3.4 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2021 License: MIT Imports: 26 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 AddPathExtensionOnRequired added in v0.2.4

func AddPathExtensionOnRequired(location, dotExt string) string

func AllDirSplits added in v0.0.6

func AllDirSplits(currentPath string) (baseDirNames *[]string)

func AllDirSplitsWithSep added in v0.0.6

func AllDirSplitsWithSep(currentPath, separator string) (baseDirNames *[]string)

func ArgumentsJoin added in v0.1.0

func ArgumentsJoin(args ...string) string

func ArgumentsJoinSlice added in v0.1.0

func ArgumentsJoinSlice(args []string) string

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/oT6eWNZAeEi

func GetBaseDirNames added in v0.0.6

func GetBaseDirNames(currentPath string) (baseDirNames *[]string)

func GetBaseDirOrEmpty added in v0.2.4

func GetBaseDirOrEmpty(currentPath string) (baseDir string)

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 Location 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 Location 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(separator string, 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(
	separator string,
	fullPaths *[]string,
	isNormalize bool,
) *[]*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, isNormalize bool) *fileinfo.Wrappers

func GetFileInfoWrappersFromUsingSeparator added in v0.0.6

func GetFileInfoWrappersFromUsingSeparator(path, separator string, isNormalize bool) *fileinfo.Wrappers

func GetFileNameWithExt

func GetFileNameWithExt(currentPath string) (fileName string)

GetFileNameWithExt reference example : https://play.golang.org/p/oT6eWNZAeEi

func GetFileNameWithoutExt

func GetFileNameWithoutExt(currentPath string) (filename string)

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

func GetFileNames

func GetFileNames(path string, isNormalize bool) *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

GetLongPathFixed

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

GetLongPathsFixedOf

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 GetOsFileInfos added in v0.0.7

func GetOsFileInfos(
	allPaths *[]string,
) (
	infos *[]os.FileInfo,
	errsCollection *errwrappers.Collection,
)

func GetOsFileInfosCollection added in v0.0.7

func GetOsFileInfosCollection(
	allPaths *[]string,
) (
	infos *osfileinfos.Collection,
	errsCollection *errwrappers.Collection,
)

func GetParentDir added in v0.1.3

func GetParentDir(location string) string

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/oT6eWNZAeEi

func IsDir added in v0.1.3

func IsDir(location string) bool

func IsExistButDirectory added in v0.1.7

func IsExistButDirectory(location string) bool

func IsGoModuleOn

func IsGoModuleOn() bool

Returns env go bin path

func IsPathExists added in v0.1.3

func IsPathExists(location string) bool
func IsSymbolicLink(path string) bool

func PathCombineWithBase added in v0.2.4

func PathCombineWithBase(
	isNormalize,
	isExpandEnv bool,
	baseDir,
	relativePath string,
) string

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/oT6eWNZAeEi

Types

type LocationInfo added in v0.2.4

type LocationInfo struct {
	RawLocation,
	FileNameWithExtension,
	BaseDir,
	FileName,
	DotExtension,
	Extension string
}

func GetLocationInfo added in v0.2.4

func GetLocationInfo(location string) *LocationInfo

func (*LocationInfo) HasBaseDir added in v0.2.4

func (it *LocationInfo) HasBaseDir() bool

func (*LocationInfo) HasDotExtension added in v0.2.4

func (it *LocationInfo) HasDotExtension() bool

func (*LocationInfo) HasExtension added in v0.2.4

func (it *LocationInfo) HasExtension() bool

func (*LocationInfo) HasFileName added in v0.2.4

func (it *LocationInfo) HasFileName() bool

func (*LocationInfo) HasFileNameWithExtension added in v0.2.4

func (it *LocationInfo) HasFileNameWithExtension() bool

func (*LocationInfo) HasLocation added in v0.2.4

func (it *LocationInfo) HasLocation() bool

func (*LocationInfo) IsDotExtension added in v0.2.4

func (it *LocationInfo) IsDotExtension(dotExt string, isIgnoreCase bool) bool

func (*LocationInfo) IsExtension added in v0.2.4

func (it *LocationInfo) IsExtension(ext string, isIgnoreCase bool) bool

func (*LocationInfo) IsFileName added in v0.2.4

func (it *LocationInfo) IsFileName(fileName string, isIgnoreCase bool) bool

func (*LocationInfo) IsFileNameExt added in v0.2.4

func (it *LocationInfo) IsFileNameExt(fileNameExt string, isIgnoreCase bool) bool

func (*LocationInfo) IsInvalidBaseDir added in v0.2.4

func (it *LocationInfo) IsInvalidBaseDir() bool

func (*LocationInfo) IsInvalidDotExtension added in v0.2.4

func (it *LocationInfo) IsInvalidDotExtension() bool

func (*LocationInfo) IsInvalidExtension added in v0.2.4

func (it *LocationInfo) IsInvalidExtension() bool

func (*LocationInfo) IsInvalidFileName added in v0.2.4

func (it *LocationInfo) IsInvalidFileName() bool

func (*LocationInfo) IsInvalidFileNameWithExtension added in v0.2.4

func (it *LocationInfo) IsInvalidFileNameWithExtension() bool

func (*LocationInfo) IsInvalidLocation added in v0.2.4

func (it *LocationInfo) IsInvalidLocation() bool

func (*LocationInfo) PathCombineWithBase added in v0.2.4

func (it *LocationInfo) PathCombineWithBase(
	isNormalize,
	isExpandEnv bool,
	relativePath string,
) string

func (*LocationInfo) PathCombineWithBaseAsLocationInfo added in v0.2.4

func (it *LocationInfo) PathCombineWithBaseAsLocationInfo(
	isNormalize,
	isExpandEnv bool,
	relativePath string,
) *LocationInfo

func (*LocationInfo) PathSimpleStat added in v0.2.4

func (it *LocationInfo) PathSimpleStat() *pathchmod.SimpleStat

Source Files

Jump to

Keyboard shortcuts

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