utils

package
v0.0.0-...-d0e6ce9 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2022 License: Apache-2.0 Imports: 28 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ColorRed     = "red"
	ColorGreen   = "green"
	ColorYellow  = "yellow"
	ColorBlue    = "blue"
	ColorMagenta = "magenta"
	ColorCyan    = "cyan"
	ColorWhite   = "white"
	ColorGray    = "gray"
)
View Source
const (

	// CheckSymbol is the code for check symbol
	CheckSymbol = "\u2714 "
	// CrossSymbol is the code for check symbol
	CrossSymbol  = "\u2716 "
	ExclamSymbol = "\u0021 "
	ArrowSymbol  = "\u279c"
	DeleteSymbol = "\u2620"
	NoneSymbol   = "\u2605"
	// Message type
	Info     = "info"
	Err      = "error"
	Warn     = "warn"
	Inst     = "install"
	Uinst    = "uninstall"
	None     = "none"
	Alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
)

Variables

View Source
var ErrAbort = errors.New("")

ErrAbort is the error returned when confirm prompts are supplied "n"

View Source
var ErrEOF = errors.New("^D")

ErrEOF is the error returned from prompts when EOF is encountered.

View Source
var ErrInterrupt = errors.New("^C")

ErrInterrupt is the error returned from prompts when an interrupt (ctrl-c) is encountered.

Functions

func ArgumentsCheck

func ArgumentsCheck(argCount, min, max int) error

ArgumentsCheck check arguments count correctness

func CheckAndExit

func CheckAndExit(err error)

func CheckConnect

func CheckConnect(connect string) error

check connect string, format is [user@]host[:port]

func CheckErr

func CheckErr(err error) bool

func Clear

func Clear()

clear screen

func CompressStr

func CompressStr(str string) string

compress string by regexp trim space or tab

func Converted2Rendered

func Converted2Rendered(r io.Reader, w io.Writer, prefix string)

func DeleteExtraSpace

func DeleteExtraSpace(s string) string

func ErrorAssert

func ErrorAssert(err error, assert string) bool

错误断言

func Execute

func Execute(workDir, script string, args ...string) bool

Execute executes shell commands with arguments

func ExitN

func ExitN(messageType string, message string, code int)

func GetColor

func GetColor() (string, color.Color)

func GetNodeIP

func GetNodeIP() string

GetNodeIP fetches node ip via command hostname. If it fails to get this, return empty string directly.

func GetOSName

func GetOSName() (string, error)

GetOSName gets data in /etc/os-release and gets OS name. For example, in a Ubuntu host, fetched data are like: root@i-8brpbc9t:~# cat /etc/os-release NAME="Ubuntu" VERSION="16.04.2 LTS (Xenial Xerus)" ID=ubuntu ID_LIKE=debian PRETTY_NAME="Ubuntu 16.04.2 LTS" VERSION_ID="16.04" HOME_URL="http://www.ubuntu.com/" SUPPORT_URL="http://help.ubuntu.com/" BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/" VERSION_CODENAME=xenial UBUNTU_CODENAME=xenial

func GetUsername

func GetUsername() string

GetUsername return current username

func Install

func Install(osName string, dir string) error

func IsEmpty

func IsEmpty(path string) (bool, error)

IsEmpty check directory is or is not empty

func IsNumeric

func IsNumeric(val interface{}) bool

IsNumeric is_numeric() Numeric strings consist of optional sign, any number of digits, optional decimal part and optional exponential part. Thus +0123.45e6 is a valid numeric value. In PHP hexadecimal (e.g. 0xf4c3b00c) is not supported, but IsNumeric is supported.

func IsSymLink(filepath string) (error, bool)

func MergeSlice

func MergeSlice(s1 []string, s2 []string) []string

func ParseAbsPath

func ParseAbsPath(filepath string, homeDir string) string

ParseAbsPath return relative file path if filepath is absolute

func ParseConnect

func ParseConnect(connectStr string) (string, string, string)

ParseConnect parse connect string, format is [user@]host[:port]

func ParseOriginalFilePath

func ParseOriginalFilePath(filepath string) string

ParseOriginalFilePath return the original file path if file is a symbol link

func ParseRelPath

func ParseRelPath(filepath string, homeDir string) string

ParseRelPath return absolute file path if filepath is relative

func PathExist

func PathExist(_path string) bool

PathExist check file is or is not exist

func PrintErr

func PrintErr(err error)

func PrintErrWithPrefix

func PrintErrWithPrefix(prefix string, err error)

func PrintN

func PrintN(messageType string, message string)

func Query

func Query(values, keys []string, ignoreCase bool) bool

Query values contains keys

func Render

func Render(tpl *template.Template, data interface{}) []byte

func RenderedOutput

func RenderedOutput(wr io.Writer, line ColorLine) error

func RenderedTpl

func RenderedTpl() *template.Template

func Root

func Root() bool

func SortKeys

func SortKeys(m map[string]string) []string

SortKeys sort map keys

func Uninstall

func Uninstall(osName string, dir string) error

Types

type ColorLine

type ColorLine struct {
	Prefix string
	Value  string
}

type SSHSession

type SSHSession struct {
	Stdin  io.Writer
	Stdout io.Reader
	Stderr io.Reader
	// contains filtered or unexported fields
}

func NewSSHSession

func NewSSHSession(session *ssh.Session) *SSHSession

New Session

func NewSSHSessionWithRoot

func NewSSHSessionWithRoot(session *ssh.Session, useSudo, noPasswordSudo bool, rootPassword, userPassword string) *SSHSession

New Session and auto switch root user

func NewSSHSessionWithRootAndCmdDelay

func NewSSHSessionWithRootAndCmdDelay(session *ssh.Session, useSudo, noPasswordSudo bool, rootPassword, userPassword string, cmdDelay time.Duration) *SSHSession

New Session and auto switch root user(support custom switch cmd delay)

func (*SSHSession) Close

func (s *SSHSession) Close() error

Close close the session

func (*SSHSession) PipeExec

func (s *SSHSession) PipeExec(cmd string, printFn func(r io.Reader, w io.Writer)) error

func (*SSHSession) Terminal

func (s *SSHSession) Terminal() error

open a interactive shell

func (*SSHSession) TerminalWithKeepAlive

func (s *SSHSession) TerminalWithKeepAlive(serverAliveInterval time.Duration) error

TerminalWithKeepAlive open a interactive terminal shell with keepalive

type SystemVersion

type SystemVersion struct {
	Name string `json:"Name,omitempty"`

	// Arch type of underlying hardware
	Arch string `json:"Arch,omitempty"`

	// The time when this binary of daemon is built
	BuildTime string `json:"BuildTime,omitempty"`

	// Commit ID held by the latest commit operation
	GitCommit string `json:"GitCommit,omitempty"`

	// version of Go runtime
	GoVersion string `json:"GoVersion,omitempty"`

	// Operating system type of underlying system
	Os string `json:"Os,omitempty"`

	Version string `json:"Version,omitempty"`
}

func Version

func Version() (SystemVersion, error)

type VersionInfo

type VersionInfo struct {
	Kernel int    // Version of the kernel (e.g. 4.1.2-generic -> 4)
	Major  int    // Major part of the kernel version (e.g. 4.1.2-generic -> 1)
	Minor  int    // Minor part of the kernel version (e.g. 4.1.2-generic -> 2)
	Flavor string // Flavor of the kernel version (e.g. 4.1.2-generic -> generic)
}

VersionInfo holds information about the kernel.

func GetKernelVersion

func GetKernelVersion() (*VersionInfo, error)

GetKernelVersion returns the kernel version info.

func (*VersionInfo) String

func (k *VersionInfo) String() string

String returns the kernel version's string format.

Jump to

Keyboard shortcuts

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