sys

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2025 License: MIT Imports: 10 Imported by: 6

Documentation

Index

Constants

View Source
const (
	// CopyDoNotOverwrite will prevent copying if the destination file already exists
	CopyDoNotOverwrite CopyOverwriteType = 0
	// CopyOverwrite will cause the destination to be replaced if it exists
	CopyOverwrite = 1
	// CopyOverwriteOnlyIfNewer will cause the destination to be overwritten only if the source is newer than the destination.
	CopyOverwriteOnlyIfNewer = 2
)

Variables

This section is empty.

Functions

func CopyDirectory

func CopyDirectory(src, dst string, overwrite CopyOverwriteType) (err error)

CopyDirectory copies the src directory to the destination directory. The destination directory will be the parent of the resulting directory, and the result will have the same name as the source. If the destination already exists, it will perform a kind of merge, where existing files will not be touched, and only new files will be copied. If you want to replace the destination, delete it first. dst must exist.

func CopyDirectoryEx added in v1.0.0

func CopyDirectoryEx(src, dst string, overwrite CopyOverwriteType, excludes []string) (err error)

CopyDirectoryEx copies the src directory to the destination directory excluding files that match a specified pattern.

The destination directory will be the parent of the resulting directory, and the result will have the same name as the source. If the destination already exists, it will perform a kind of merge, where existing files will not be touched, and only new files will be copied. If you want to replace the destination, delete it first. dst must exist.

func CopyFiles

func CopyFiles(dst string, overwrite CopyOverwriteType, src ...string) (err error)

CopyFiles copies the src files or directories to the destination

If there is more than one source, the destination must be a directory that exists. The items listed will be copied inside the destination directory.

If there is only one source, the destination must be:

  • A directory that exists, in which case the source will be placed in the destination directory
  • A file that exists, in which case the source will overwrite the destination. The source must also be a single file.
  • A file that does not exist, but whose parent directory does exist, in which case the file will be copied and renamed to the destination.

If overwrite is true, files that already exist will be overwritten. If overwrite is false, only new files will be created. If a directory is over-writing another directory, this will determine what happens when file names are duplicates. Note that old files in a directory will not be deleted when a directory overwrites another directory. If you want old files to be deleted, empty the destination directory first.

func CopyFilesEx added in v1.0.0

func CopyFilesEx(dst string, overwrite CopyOverwriteType, exclusions []string, src ...string) (err error)

CopyFilesEx copies the src files or directories to the destination excluding files matching the exclusions slice.

If there is more than one source, the destination must be a directory that exists. The items listed will be copied inside the destination directory.

If there is only one source, the destination must be:

  • A directory that exists, in which case the source will be placed in the destination directory
  • A file that exists, in which case the source will overwrite the destination. The source must also be a single file.
  • A file that does not exist, but whose parent directory does exist, in which case the file will be copied and renamed to the destination.

If overwrite is true, files that already exist will be overwritten. If overwrite is false, only new files will be created. If a directory is over-writing another directory, this will determine what happens when file names are duplicates. Note that old files in a directory will not be deleted when a directory overwrites another directory. If you want old files to be deleted, empty the destination directory first.

func ExecuteShellCommand

func ExecuteShellCommand(command string) (result []byte, err error)

ExecuteShellCommand executes a shell command in the current working directory and returns its output, if any. The result is stdOut. If you get an error, you can cast err to (*exec.ExitError) and read the Stderr member to see the error message that was generated. The command string is the shell command, complete with all arguments. To add string that has a space in it, enclose it in single or double quotes. Linux permits backslash escaped spaces, but that will not work here, since a backslash represents something else in windows. However, quotes work in all OS's. To include a quote character, use the other kind of quote. For example, to include a single quote, surround with double quotes. This does not support recursive quotes. For that, you just will need to revert to the exec.Command function.

func GetModulePath

func GetModulePath(path string, modules map[string]string) (newPath string, err error)

GetModulePath compares the given path with the list of modules and if the path begins with a module name, it will substitute the absolute path for the module name. modules is the output from ModulePaths. Module paths always use forward slashes. The resulting path uses the native path separator.

If the path is not a module path, the path will be returned unchanged.

func HasMeta added in v1.0.2

func HasMeta(path string) bool

HasMeta reports whether path contains any of the magic characters recognized by Match. This is copied from the unexported function from filepath.

func ImportPath added in v1.2.0

func ImportPath(fp string) (importPath string, err error)

ImportPath will convert a path to a go source file to the equivalent module oriented import path. This is useful for doing code generation to convert relative paths to absolute module paths, since go does not support relative import paths. fp can be a directory, in which case it will be treated as a path to a package that you want the import path for. This can be a lengthy process, so minimize the number of times you call this.

func IsDir added in v0.3.0

func IsDir(path string) bool

IsDir returns true if the given path exists and is a directory.

func ModuleExpandFileList

func ModuleExpandFileList(args []string, modules map[string]string) (list []string)

ModuleExpandFileList will do the following given a list of arguments that represent command line arguments that would be a list of files, directories, or glob patterns:

replace any environment variables with their values
replace any items that start with a module with the actual location on disk
expand any glob patterns
remove duplicates

It will return the final list.

modules is the list of modules returned from ModulePaths.

Glob patterns will be matched, and if nothing is found, no file will be generated. However, if a path does not have a glob pattern, but does not exist, it will be left in the list, since it might refer to a file or directory you want to add. The list out is not necessarily in the same order as the list in.

func ModulePaths

func ModulePaths() (ret map[string]string, err error)

ModulePaths returns a listing of the paths of all the modules included in the go.mod file, keyed by module name, from the perspective of the current working directory.

If we are running without module support, it will return an error.

func SplitList

func SplitList(s string) (list []string)

SplitList will split a file and/or directory list into individual items in a cross-platform way. In other words, the list can be specified in a Unix(colons) or Windows(semicolon) friendly way, and it will split the list regardless of the platform the list was designed for, or run on. This means on Unix, you can't use a semicolon in a file name, and on windows, you can't use a colon, but that should not be an issue for most people.

Types

type CopyOverwriteType

type CopyOverwriteType int

CopyOverwriteType is used by the CopyFiles function to determine how to treat file collisions when copying over a file that already exists.

Jump to

Keyboard shortcuts

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