Help us shape the future of Go! Take the 2026 Go Developer Survey

echo

package module
v0.0.1-alpha.3 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2020 License: MIT Imports: 13 Imported by: 0

README

Echo$

Script-like functionalities wrapped in the rigid safety of Go!

Start

Create a session:

e := echo.New()

Then configure your session:

e.Conf.SetPanicOnError(true)

Variables

Var, SetVar

Echo supports storing values that can be accessed using method e.Var() for the duration of a session:

e.Var("Foo=Bar")
e.Var("Fuzz=${Foo} Buzz=Bazz") 

Method e.SetVar(name, value string) saves a named value one at a time.

Env, SetEnv

Values can be made visible as environment variables for externally launced commands using method e.Env(). Both methods support value expansion as shown below:

e.Env("Foo=Bar")
e.Env("Fuzz=${Foo} Buzz=Bazz")
e.Env("BAZZ=$HOME")

Method e.SetEnv(name, value string) sets environment variables one value at a time.

Expansion

All echo methods support variable value expansion using $name or ${name} which are automatically replaced with the value of the named variable.

Slices

Split

A space-separated list can be turned into a native Go []string using the e.Split() method as shown:

e.SetVar("list", "item3 item4")
for _, val := range e.Split("item0 item1 item2 $list"){  
    ...
}

An additional separator value may be provided to e.Split():

e.SetVar("list", "item3;item4")
for _, val := range e.Arr("Hello;World;!;$list", ";"){  
    fmt.Println(val)
}
Glob

Method e.Glob() expands the provided shell path pattern into a slice of matching file/directory names:

for _, f := range e.Glob("$HOME/go/src/*.com") {
  fmt.Println(f)
}

External processes

Run

External commands can be launched using the e.Run() method by passing a command and its arguments as a single string:

fmt.Println(e.Var("greeting=Hello name=Vladimir").Run("echo $greeting $name"))

Method e.Runout automatically prints the result to sdtdout for you:

e.Runout("echo no place like $HOME")
e.RExitCode() int
e.RStatus() 
e.RExited() bool
e.RPid() int
e.ROkay() bool
e.RSystime() string
e.RUsertime() string

Strings

e.Empty(string) bool
e.Lower(string) string
e.Upper(string) string
e.Streq(string, string) bool // string equal
e.Trim(string)string 

Files

File operation methods:

e.Abs()
e.Rel()
e.Base()
e.Dir()
e.PathSym()
e.Ext()
e.PathJoin()
e.PathMatched()
e.IsExit()
e.IsReg()
e.IsDir()
e.Mkdirs()
e.Rmdirs()
e.Chown()
e.Chmod()
e.AreSame() // Are files equal

Program

e.Name()
e.Args()
e.Exit(code)
e.Hostname()
e.Grps()[]int
e.Pid()
e.PPid()
e.Pwd()
e.Which(path)

User

e.Username()
e.Home()
e.Gid()
e.Uid()

Flagset

...

More to come

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// contains filtered or unexported fields
}

Config stores configuration

func (*Config) IsPanicOnErr

func (c *Config) IsPanicOnErr() bool

IsPanicOnErr returns panic-on-error flag

func (*Config) IsVerbose

func (c *Config) IsVerbose() bool

IsVerbose returns verbosity flag

func (*Config) SetPanicOnErr

func (c *Config) SetPanicOnErr(val bool) *Config

SetPanicOnErr panics program on any error

func (*Config) SetVerbose

func (c *Config) SetVerbose(val bool) *Config

SetVerbose sets verbosity

type Echo

type Echo struct {
	Conf *Config     // session config
	Proc *ProcResult // last executed proc
	// contains filtered or unexported fields
}

Echo represents a new Echo session

func New

func New() *Echo

New creates a new Echo session

func (*Echo) Abs

func (e *Echo) Abs(path string) string

Abs returns the absolute representation for path. Returns empty string if it fails for any reason.

func (*Echo) AreSame

func (e *Echo) AreSame(path0, path1 string) bool

AreSame tests files at path0 and path to check if they are the same

func (*Echo) Base

func (e *Echo) Base(path string) string

Base returns the last portion (or name) of a path.

func (*Echo) Dir

func (e *Echo) Dir(path string) string

Dir returns parent path portion of a path (without the Base).

func (*Echo) Empty

func (e *Echo) Empty(val string) bool

Empty returns true if len(val) == 0

func (*Echo) Env

func (e *Echo) Env(val string) *Echo

Env declares process environment variables using a multi-line space-separated list of KEY=VAL format: i.e. GOOS=linux GOARCH=amd64

func (*Echo) Eval

func (e *Echo) Eval(str string) string

Eval returns the string str with its content expanded with variable values i.e. Eval("I am $HOME") returns "I am <user home path>"

func (*Echo) Ext

func (e *Echo) Ext(path string) string

Ext returns extension part of path

func (*Echo) Glob

func (e *Echo) Glob(pathPattern string) []string

Glob uses shell file path pattern (i.e. /usr/*/*a) to return a []string of file names

func (*Echo) IsAbs

func (e *Echo) IsAbs(path string) bool

IsAbs returns true if path is an absolute path

func (*Echo) IsDir

func (e *Echo) IsDir(path string) bool

IsDir returns true if path is a directory. All other cases (errors) will return false.

func (*Echo) IsExist

func (e *Echo) IsExist(path string) bool

IsExist returns true if file is 1) accessible 2) exists. All other conditions returns false.

func (*Echo) IsReg

func (e *Echo) IsReg(path string) bool

IsReg returns true if path is a regular file. All other cases (errors) will return false.

func (*Echo) Lower

func (e *Echo) Lower(val string) string

Lower returns val as lower case

func (*Echo) Mkdirs

func (e *Echo) Mkdirs(paths string, modes ...uint32)

Mkdirs creates one or more space-separated directories using the optional filemods. If no mode is provided, it defaults to 0777. When mode is provided only modes[0] is applied.

func (*Echo) PathJoin

func (e *Echo) PathJoin(paths ...string) string

PathJoin collate individual paths together for a longer path

func (*Echo) PathMatched

func (e *Echo) PathMatched(pattern, path string) bool

PathMatched returns true if path matches shell file pattern

func (*Echo) PathSplit

func (e *Echo) PathSplit(path string) []string

PathSplit splits an OS-specific list of path into []string

func (*Echo) PathSym

func (e *Echo) PathSym(path string) string

PathSym returns the evaluated symbolic link for path. Returns empty if symbolic evaluation fails.

func (*Echo) Rel

func (e *Echo) Rel(base string, target string) string

Rel returns path that is relative to base/target. Returns an empty path if error.

func (*Echo) Rmdirs

func (e *Echo) Rmdirs(paths string)

Rmdirs removes one or more space-separated directories

func (*Echo) Run

func (e *Echo) Run(cmdStr string) string

Run parses and executes cmdStr and returns the result as a string. This uses RunProc and extracts the result as a string.

func (*Echo) RunProc

func (e *Echo) RunProc(cmdStr string) *ProcResult

RunProc parses and executes cmdStr and returns a process result. Use this to have access to process result informoation.

func (*Echo) Runout

func (e *Echo) Runout(cmdStr string)

Runout parses and executes command cmdStr and prints out the result

func (*Echo) SetEnv

func (e *Echo) SetEnv(name, value string) *Echo

SetEnv declares a global process environment variable.

func (*Echo) SetVar

func (e *Echo) SetVar(name, value string) *Echo

SetVar declares an echo session local variable.

func (*Echo) Split

func (e *Echo) Split(list string, sep ...string) []string

Split splits a space-separated (default) string into []string. An optional separator sep maybe provided as well.

func (*Echo) StartProc

func (e *Echo) StartProc(cmdStr string) *ProcResult

StartProc concurrently starts a process

func (*Echo) Streq

func (e *Echo) Streq(val0, val1 string) bool

Streq returns true if both strings are equal

func (*Echo) String

func (e *Echo) String() string

func (*Echo) Trim

func (e *Echo) Trim(val string) string

Trim removes spaces around a val

func (*Echo) Upper

func (e *Echo) Upper(val string) string

Upper returns val as upper case

func (*Echo) Val

func (e *Echo) Val(name string) string

Val retrieves a session or environment variable

func (*Echo) Var

func (e *Echo) Var(val string) *Echo

Var declares variables used during current echo session using a multi-line space-separated list of KEY=VAL format: i.e. foo=bar fuzz=buzz

type ProcResult

type ProcResult struct {
	// contains filtered or unexported fields
}

ProcResult stores process info

func (*ProcResult) Err

func (p *ProcResult) Err() error

Err returns any execution error

func (*ProcResult) ExitCode

func (p *ProcResult) ExitCode() int

ExitCode returns process exit code

func (*ProcResult) Exited

func (p *ProcResult) Exited() bool

Exited returns true if process exits ok

func (*ProcResult) ID

func (p *ProcResult) ID() int

ID returns process id

func (*ProcResult) IsSuccess

func (p *ProcResult) IsSuccess() bool

IsSuccess returns true if proc exit ok

func (*ProcResult) Out

func (p *ProcResult) Out() io.Reader

Out surfaces an io.Reader for proc result

func (*ProcResult) SysTime

func (p *ProcResult) SysTime() time.Duration

SysTime returns proc system cpu time

func (*ProcResult) UserTime

func (p *ProcResult) UserTime() time.Duration

UserTime returns proc user cpu time

func (*ProcResult) Wait

func (p *ProcResult) Wait()

Wait for associated process to complete. Any error can be accessed via p.Err()

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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