Documentation
¶
Overview ¶
Package interactive allows you to easily execuate and interact with processes using Go channels.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var Debug bool
Debug enables debug output for this package to console
Functions ¶
This section is empty.
Types ¶
type Session ¶
type Session struct {
StdIn io.Writer // input to be written to the console
StdOut io.Reader // output coming from the console
StdErr io.Reader // error output from the shell
Input chan string // incoming lines of input
Output chan string // outgoing lines of input
Cmd *exec.Cmd // cmd that holds this cmd instance
// contains filtered or unexported fields
}
Session is an interactive console session for the specified command and arguments.
func NewSession ¶
NewSession starts a new interactive command session
Example ¶
ExampleNewSession shows how to make a new interactive session with a command. Output comes from the string channel bc.Output while iput is passed in with the bc.Write func. Note that we named the interactive session "bc" here because we're running bc.
package main
import (
"fmt"
"time"
"github.com/integrii/interactive"
)
func main() {
// Start the command "bc" (a CLI calculator)
bc, err := interactive.NewSession("bc", []string{})
if err != nil {
panic(err)
}
// start a concurrent output reader from the output channel of our command
go func(outChan chan string) {
for s := range outChan {
fmt.Println(s)
}
}(bc.Output)
// wait a second for the process to init
time.Sleep(time.Second)
// write 1 + 1 to the bc prompt
bc.Write(`1 + 1`)
// wait one second for the output to come and be displayed
time.Sleep(time.Second)
}
Output:
func NewSessionWithTimeout ¶
NewSessionWithTimeout starts a new session but kills it if it runs longer than the specified timeout. Pass 0 for no timeout or use NewSession()
Example ¶
ExampleSessionWithOutput shows how to start a command that only runs for one second before being killed. The 1 + 1 operation never happens becauase the command is killed prior to its running
package main
import (
"log"
"time"
"github.com/integrii/interactive"
)
func main() {
bc, err := interactive.NewSessionWithTimeout("bc", []string{}, time.Duration(time.Second))
if err != nil {
log.Fatal(err)
}
time.Sleep(time.Second * 2)
bc.Write(`1 + 1`) // this will never happen and there will not be any output
}
Output:
func (*Session) Exit ¶
func (i *Session) Exit()
Exit exits the running command and closes the input channel
func (*Session) ForceClose ¶
func (i *Session) ForceClose()
ForceClose issues a force kill to the command (SIGKILL)