os

package
v0.55.1 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2024 License: BSD-3-Clause Imports: 15 Imported by: 2

Documentation

Overview

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

Index

Examples

Constants

This section is empty.

Variables

View Source
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
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:

  • .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

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
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.

Types

This section is empty.

Directories

Path Synopsis
Package exec wrap the standar package "os/exec" to simplify calling Run with stdout and stderr.
Package exec wrap the standar package "os/exec" to simplify calling Run with stdout and stderr.

Jump to

Keyboard shortcuts

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