package os

import "git.sr.ht/~shulhan/pakakeh.go/lib/os"

Package os extend the standard os package to provide additional functionalities.

Index

Examples

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

func ConfirmYesNo(in io.Reader, msg string, defIsYes bool) bool

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

func Copy(out, in string) (err error)

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

func Environments() (envs map[string]string)

Environments return list of system environment as map of key and value.

Example

Code:play 

package main

import (
	"fmt"
	"os"

	libos "git.sr.ht/~shulhan/pakakeh.go/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

func Extract(fileInput, dirOutput string) (err error)

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:

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

func IsBinary(file string) bool

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

Code:play 

package main

import (
	"fmt"

	libos "git.sr.ht/~shulhan/pakakeh.go/lib/os"
)

func main() {
	fmt.Println(libos.IsBinary("/bin/bash"))
	fmt.Println(libos.IsBinary("io.go"))
}

Output:

true
false

func IsDirEmpty

func IsDirEmpty(dir string) (ok bool)

IsDirEmpty will return true if directory is not exist or empty; otherwise it will return false.

func IsFileExist

func IsFileExist(parent, relpath string) bool

IsFileExist will return true if relative path is exist on parent directory; otherwise it will return false.

func PathFold

func PathFold(in string) (out string, err error)

PathFold replace the path "in" with tilde "~" if its prefix match with user's home directory from os.UserHomeDir.

func PathUnfold

func PathUnfold(in string) (out string, err error)

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

func RmdirEmptyAll(path string) error

RmdirEmptyAll remove directory in path if it's empty until one of the parent is not empty.

Source Files

extract.go os.go

Directories

PathSynopsis
lib/os/execPackage exec wrap the standar package "os/exec" to simplify calling Run with stdout and stderr.
Version
v0.60.0 (latest)
Published
Feb 1, 2025
Platform
linux/amd64
Imports
15 packages
Last checked
20 minutes ago

Tools for package owners.