common

package
v0.0.0-...-9c0138c Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2023 License: MIT Imports: 30 Imported by: 0

Documentation

Index

Constants

View Source
const (
	B Size = 1
	K      = 1024 * B
	M      = 1024 * K
	G      = 1024 * M
	T      = 1024 * G
	P      = 1024 * T
	E      = 1024 * P
)
View Source
const BackupSuffix = "~"

Variables

View Source
var (
	// LinuxMountsPath is the standard mountpoint list path
	LinuxMountsPath = "/proc/mounts"

	// Debug function to override for verbose logging.
	Debug = func(string, ...interface{}) {}

	// SystemPartitionGUID is the GUID of EFI system partitions
	// EFI System partitions have GUID C12A7328-F81F-11D2-BA4B-00A0C93EC93B
	SystemPartitionGUID = gpt.Guid([...]byte{
		0x28, 0x73, 0x2a, 0xc1,
		0x1f, 0xf8,
		0xd2, 0x11,
		0xba, 0x4b,
		0x00, 0xa0, 0xc9, 0x3e, 0xc9, 0x3b,
	})
)

Functions

func Backup

func Backup(path string) error

func CheckSumCompare

func CheckSumCompare(t string, w io.Writer, flagSet *flag.FlagSet, suppress bool) error

func CheckSumMain

func CheckSumMain(t string, w io.Writer, flagSet *flag.FlagSet, suppress bool) error

func Checksum

func Checksum(t string, fp io.Reader) (string, error)

func CompareSlice

func CompareSlice(s1, s2 []byte) (int, bool)

func ConvertFileModeStr

func ConvertFileModeStr(mode string) (os.FileMode, error)

func FileExists

func FileExists(path string) bool

func FileNotExists

func FileNotExists(path string) bool

func GetAllPids

func GetAllPids() ([]int, error)

Returns a slice of all pids currently existing

func GetMountpointByDevice

func GetMountpointByDevice(devicePath string) (*string, error)

GetMountpointByDevice gets the mountpoint by given device name. Returns on first match

func Groups

func Groups(uname string) ([]user.Group, error)

func IntContains

func IntContains(target []int, src int) bool

IntContains checks the src in any int of the target int slice.

func IsDecimal

func IsDecimal(b byte) bool

func IsDecimalStr

func IsDecimalStr(s string, max int) (i int, have bool)

func IsDir

func IsDir(name string) (bool, error)

func IsHidden

func IsHidden(name string) bool

func IsMalformedFileMode

func IsMalformedFileMode(mode os.FileMode) bool

func IsNamedPipe

func IsNamedPipe(info os.FileInfo) bool

func IsNeedStr

func IsNeedStr(s string, max int, is func(b byte) bool) (i int, have bool)

func IsNumber

func IsNumber(s string) (int, bool)

func IsNumeric

func IsNumeric(s string) bool

func IsOctal

func IsOctal(b byte) bool

func IsOctalStr

func IsOctalStr(s string, max int) (i int, haveOctal bool)
func IsSymlink(info os.FileInfo) bool

func IsXdigit

func IsXdigit(b byte) bool

func IsXdigitStr

func IsXdigitStr(s string, max int) (i int, haveHex bool)

func OpenTwoFiles

func OpenTwoFiles(files []string) (sf *os.File, df *os.File, err error)

func ReadWholeLine

func ReadWholeLine(b *bufio.Reader) (line string, err error)

func RenderString

func RenderString(s string, ctx interface{}) (string, error)

RenderString renders the string `s` as a `text/template` using the provided context `ctx` as input into the template. Typically used to render the results into a Markdown document.

func StringsContains

func StringsContains(target []string, src string) bool

StringsContains checks the src in any string of the target string slice

func StringsHas

func StringsHas(target []string, src string) bool

StringsHas checks the target string slice contains src or not

func TTY

func TTY() (string, error)

TTY prints the file name of the terminal connected to standard input

func UniqSortInt

func UniqSortInt(src []int) []int

func Walk

func Walk(path string, info os.FileInfo, depth int, parentIgnores IgnoreMatchers, followed bool, walkFn walkFunc) error

func WrapString

func WrapString(src string, lim int, delim string) string

Types

type BlockDev

type BlockDev struct {
	Name   string
	FSType string
	FsUUID string
}

BlockDev maps a device name to a BlockStat structure for a given block device

func Device

func Device(maybeDevpath string) (*BlockDev, error)

Device makes sure the block device exists and returns a handle to it.

maybeDevpath can be path like /dev/sda1, /sys/class/block/sda1 or just sda1. We will just use the last component.

func (*BlockDev) BlockSize

func (b *BlockDev) BlockSize() (int, error)

BlockSize returns the logical block size (BLKSSZGET).

func (BlockDev) DevicePath

func (b BlockDev) DevicePath() string

DevicePath is the path to the actual device.

func (*BlockDev) GPTTable

func (b *BlockDev) GPTTable() (*gpt.Table, error)

GPTTable tries to read a GPT table from the block device described by the passed BlockDev object, and returns a gpt.Table object, or an error if any

func (*BlockDev) KernelBlockSize

func (b *BlockDev) KernelBlockSize() (int, error)

KernelBlockSize returns the soft block size used inside the kernel (BLKBSZGET).

func (*BlockDev) PhysicalBlockSize

func (b *BlockDev) PhysicalBlockSize() (int, error)

PhysicalBlockSize returns the physical block size.

func (*BlockDev) ReadPartitionTable

func (b *BlockDev) ReadPartitionTable() error

ReadPartitionTable prompts the kernel to re-read the partition table on this block device.

func (*BlockDev) Size

func (b *BlockDev) Size() (uint64, error)

Size returns the size in bytes.

func (*BlockDev) String

func (b *BlockDev) String() string

String implements fmt.Stringer.

type BlockDevices

type BlockDevices []*BlockDev

BlockDevices is a list of block devices.

func GetBlockDevices

func GetBlockDevices() (BlockDevices, error)

GetBlockDevices iterates over /sys/class/block entries and returns a list of BlockDev objects, or an error if any

func (BlockDevices) FilterFSUUID

func (b BlockDevices) FilterFSUUID(fsuuid string) BlockDevices

FilterFSUUID returns a list of BlockDev objects whose underlying block device has a filesystem with the given FSUUID.

func (BlockDevices) FilterHavingPartitions

func (b BlockDevices) FilterHavingPartitions(parts []int) BlockDevices

FilterHavingPartitions returns BlockDevices with have the specified partitions. (e.g. f(1, 2) {sda, sda1, sda2, sdb} -> {sda})

func (BlockDevices) FilterName

func (b BlockDevices) FilterName(name string) BlockDevices

FilterName returns a list of BlockDev objects whose underlying block device has a Name with the given Name

func (BlockDevices) FilterNames

func (b BlockDevices) FilterNames(names ...string) BlockDevices

FilterNames filters block devices by the given list of device names (e.g. /dev/sda1 sda2 /sys/class/block/sda3).

func (BlockDevices) FilterPartID

func (b BlockDevices) FilterPartID(guid string) BlockDevices

FilterPartID returns partitions with the given partition ID GUID.

func (BlockDevices) FilterPartLabel

func (b BlockDevices) FilterPartLabel(label string) BlockDevices

FilterPartLabel returns a list of BlockDev objects whose underlying block device has the given partition label. The name comparison is case-insensitive.

func (BlockDevices) FilterPartType

func (b BlockDevices) FilterPartType(guid string) BlockDevices

FilterPartType returns partitions with the given partition type GUID.

func (BlockDevices) FilterZeroSize

func (b BlockDevices) FilterZeroSize() BlockDevices

FilterZeroSize attempts to find block devices that have at least one block of content.

This serves to eliminate block devices that have no backing storage, but appear in /sys/class/block anyway (like some loop, nbd, or ram devices).

type BufferedReader

type BufferedReader struct {
	*bufio.Reader
}

func NewBufferedReader

func NewBufferedReader(r io.Reader) *BufferedReader

func (*BufferedReader) ReadWholeLine

func (b *BufferedReader) ReadWholeLine() (line string, err error)

type IgnoreMatcher

type IgnoreMatcher interface {
	Match(path string, isDir bool) bool
}

type IgnoreMatchers

type IgnoreMatchers []IgnoreMatcher

func (IgnoreMatchers) Match

func (im IgnoreMatchers) Match(path string, isDir bool) bool

type Process

type Process struct {
	*os.Process
	Name     string
	Owner    int
	Cmdline  string
	State    string
	Parent   int
	MemUsage int
}

func GetProcessByPid

func GetProcessByPid(pid int) (*Process, error)

type SI

type SI struct {
	Uptime       time.Duration // time since boot
	Loads        [3]float64    // 1, 5, and 15 minute load averages, see e.g. UPTIME(1)
	Procs        uint64        // number of current processes
	TotalRam     uint64        // total usable main memory size
	FreeRam      uint64        // available memory size
	SharedRam    uint64        // amount of shared memory
	BufferRam    uint64        // memory used by buffers
	TotalSwap    uint64        // total swap space size
	FreeSwap     uint64        // swap space still available
	TotalHighRam uint64        // total high memory size
	FreeHighRam  uint64        // available high memory size
}

Go-ized http://man7.org/linux/man-pages/man2/sysinfo.2.html

func (SI) String

func (si SI) String() string

Make the "fmt" Stringer interface happy.

type Size

type Size int

func ParseSize

func ParseSize(s string) (Size, error)

type UnameInfo

type UnameInfo struct {
	Name      string
	Node      string
	Release   string
	Version   string
	Machine   string
	Domain    string
	OS        string
	Processor string
}

func Uname

func Uname() (*UnameInfo, error)

Jump to

Keyboard shortcuts

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