Documentation ¶
Overview ¶
Package os extend the standard os package to provide additional functionalities.
Index ¶
- Variables
- func ConfirmYesNo(in io.Reader, msg string, defIsYes bool) bool
- func Copy(out, in string) (err error)
- func Environments() (envs map[string]string)
- func Extract(fileInput, dirOutput string) (err error)
- func IsBinary(file string) bool
- func IsDirEmpty(dir string) (ok bool)
- func IsFileExist(parent, relpath string) bool
- func PathFold(in string) (out string, err error)
- func PathUnfold(in string) (out string, err error)
- func RmdirEmptyAll(path string) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrExtractInputExt define an error when the input file extension is // unknown. // This does not means that the file is not supported. ErrExtractInputExt = errors.New("unknown extract input extension") )
Functions ¶
func ConfirmYesNo ¶ added in v0.46.0
ConfirmYesNo display a question to standard output and read for answer from input Reader for simple yes "y" or no "n" answer. If input Reader is nil, it will set to standard input. If "defIsYes" is true and answer is empty (only new line), then it will return true.
func Copy ¶ added in v0.46.0
Copy file from in to out. If the output file is already exist, it will be truncated. If the file is not exist, it will created with permission set to user's read-write only.
func Environments ¶ added in v0.49.0
Environments return list of system environment as map of key and value.
Example ¶
package main import ( "fmt" "os" libos "github.com/shuLhan/share/lib/os" ) func main() { os.Clearenv() os.Setenv(`USER`, `gopher`) os.Setenv(`HOME`, `/home/underground`) var osEnvs = libos.Environments() fmt.Println(osEnvs) }
Output: map[HOME:/home/underground USER:gopher]
func Extract ¶
Extract uncompress and/or unarchive file from fileInput into directory defined by dirOutput. This is the high level API that combine standard archive/zip, archive/tar, compress/bzip2, and/or compress/gzip.
The compression and archive format is detected automatically based on the following fileInput extension:
- .bz2: decompress using compress/bzip2.
- .gz: decompress using compress/gzip.
- .tar: unarchive using archive/tar.
- .zip: unarchive using archive/zip.
- .tar.bz2: decompress using compress/bzip2 and unarchive using archive/tar.
- .tar.gz: decompresss using compress/gzip and unarchive using archive/tar.
The output directory, dirOutput, where the decompressed and/or unarchived file stored will be created if not exist. If its empty, it will set to current directory.
On success, the compressed and/or archived file will be removed from the file system.
func IsBinary ¶ added in v0.46.0
IsBinary will return true if content of file is binary. If file is not exist or there is an error when reading or closing the file, it will return false.
Example ¶
package main import ( "fmt" libos "github.com/shuLhan/share/lib/os" ) func main() { fmt.Println(libos.IsBinary("/bin/bash")) fmt.Println(libos.IsBinary("io.go")) }
Output: true false
func IsDirEmpty ¶ added in v0.46.0
IsDirEmpty will return true if directory is not exist or empty; otherwise it will return false.
func IsFileExist ¶ added in v0.46.0
IsFileExist will return true if relative path is exist on parent directory; otherwise it will return false.
func PathFold ¶ added in v0.49.0
PathFold replace the path "in" with tilde "~" if its prefix match with user's home directory from os.UserHomeDir.
func PathUnfold ¶ added in v0.49.0
PathUnfold expand the tilde "~/" prefix into user's home directory using os.UserHomeDir and environment variables using os.ExpandEnv inside the string path "in".
func RmdirEmptyAll ¶ added in v0.46.0
RmdirEmptyAll remove directory in path if it's empty until one of the parent is not empty.
Types ¶
This section is empty.