Documentation
¶
Index ¶
- Constants
- Variables
- func WithCallbackOption(v func(c *Command) (bool, error)) *withCallback
- func WithNextCommandOption(v func(c *Command) *Command) *withNextCommand
- func WithNoCheckReturnCodeOption() *withNoCheckReturnCode
- func WithOutputLevelOption(v OutputLevel) *withOutputLevel
- func WithTimeoutOption(v time.Duration) *withTimeout
- type Client
- type Command
- type CommandResult
- type Option
- type OutputLevel
- type Prompt
Constants ¶
const ( // DefaultTimeOut archeve, Command is canneled. You can change by WithTimeoutOption DefaultTimeOut = time.Second * 5 )
Variables ¶
var ( // DefaultCallback is called after Command and just sleep in a second. You can change by WithCallbackOption DefaultCallback = func(c *Command) (bool, error) { time.Sleep(time.Millisecond * 500) if c.Result.ReturnCode != 0 { return false, fmt.Errorf("cmd [%v] %v", c.Input, ErrReturnCodeNotZero) } return true, nil } // ErrReturnCodeNotZero is error in command exit with non zero ErrReturnCodeNotZero = errors.New("return code is not 0") )
var ( // DefaultPrompt is prompt pettern like "pi@raspberrypi:~ $ " DefaultPrompt = Prompt{ SufixPattern: '$', SufixPosition: 2, } // DefaultRootPrompt is prompt pettern like "# " DefaultRootPrompt = Prompt{ SufixPattern: '#', SufixPosition: 2, } )
Functions ¶
func WithCallbackOption ¶
WithCallbackOption is option function called after command is finished
func WithNextCommandOption ¶
WithNextCommandOption is option function called after Callback func return true
func WithNoCheckReturnCodeOption ¶
func WithNoCheckReturnCodeOption() *withNoCheckReturnCode
NoCheckReturnCodeOption is option whether add ";echo $?" and check return code after command
func WithOutputLevelOption ¶
func WithOutputLevelOption(v OutputLevel) *withOutputLevel
WithOutputLevelOption is option of command log print
func WithTimeoutOption ¶
WithTimeoutOption is option time.Duration to command timeout
Types ¶
type Client ¶
type Client struct { Sshconfig *ssh.ClientConfig Host string Port string Prompt []Prompt }
Client has configuration to interact a host
type Command ¶
type Command struct { Input string Callback func(c *Command) (bool, error) NextCommand func(c *Command) *Command ReturnCodeCheck bool OutputLevel OutputLevel Timeout time.Duration Result *CommandResult }
Command has Input config and Output in remote host. Input is line of command execute in remote host. Callback is called after input command is finished. You can check whether Output is exepected in this function. NextCommand is called after Callback and called only Callback returns "true". NextCommand cannot has another NextCommand. ReturnCodeCheck is "true", Input is added ";echo $?" and check after Output is 0. Also you can manage retrun code in Callback. OutputLevel is logging level of command. Secret command should be set Silent Result is Command Output. You can use this in Callback, NextCommand, DefaultNextCommand functions.
func ChangeDirectory ¶
ChangeDirectory run "cd" command in remote host
func NewCommand ¶
NewCommand return Command with given options
func SwitchUser ¶
SwitchUser run "su - xxx" command in remote host
type CommandResult ¶
CommandResult has command output and return code in remote host
type OutputLevel ¶
type OutputLevel int
OutputLevel set logging level of command
const ( // Silent logs nothing Silent OutputLevel = iota // Info logs only start and end of command Info // Output logs command output in remote host Output )
type Prompt ¶
Prompt represent ssh prompt pettern used in check whether command is finished. Example:
Terminal Prompt "pi@raspberrypi:~ $ " SufixPattern='$' (rune to byte) SufixPosition=2 ($ + space)
When you use multi prompt such as Root user (often '#'), you must give all prompt pattern before Run