Skip to content

Commit

Permalink
connect: add eval functionality to the module
Browse files Browse the repository at this point in the history
The patch adds `eval` functionality to the connect module.
Now, if code has been passed in by a command, it will be
executed on the instance and the result (in YAML format) will
be returned to `stdout`.

Part of #15
  • Loading branch information
LeonidVas committed Apr 19, 2022
1 parent 73bc65c commit 84e9ce6
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 6 deletions.
28 changes: 22 additions & 6 deletions cli/cmd/connect.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"fmt"
"syscall"

"github.com/apex/log"
Expand Down Expand Up @@ -37,15 +38,30 @@ func NewConnectCmd() *cobra.Command {

// internalConnectModule is a default connect module.
func internalConnectModule(cmdCtx *cmdcontext.CmdCtx, args []string) error {
// Fill CmdCtx.
argsLen := len(args)
if argsLen < 1 {
return fmt.Errorf("Incorrect combination of command parameters")
}

cmdCtx.Connect.Username = connectUser
cmdCtx.Connect.Password = connectPassword

if terminal.IsTerminal(syscall.Stdin) {
log.Info("Connecting to the instance...")
}
if err := connect.Connect(cmdCtx, args); err != nil {
return err
if argsLen == 1 {
if terminal.IsTerminal(syscall.Stdin) {
log.Info("Connecting to the instance...")
}
if err := connect.Connect(cmdCtx, args); err != nil {
return err
}
} else if argsLen == 2 {
res, err := connect.Eval(cmdCtx, args)
if err != nil {
return err
}
// "Println" is used instead of "log..." to print the result without any decoration.
fmt.Println(string(res))
} else {
return fmt.Errorf("Incorrect combination of command parameters")
}

return nil
Expand Down
33 changes: 33 additions & 0 deletions cli/connect/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/tarantool/tt/cli/cmdcontext"
"github.com/tarantool/tt/cli/connector"
"gopkg.in/yaml.v2"
)

const (
Expand Down Expand Up @@ -32,6 +33,38 @@ func Connect(cmdCtx *cmdcontext.CmdCtx, args []string) error {
return nil
}

// Eval executes the command on the remote instance (according to args).
func Eval(cmdCtx *cmdcontext.CmdCtx, args []string) ([]byte, error) {
// Parse the arguments.
connString := args[0]
connOpts := getConnOpts(connString, cmdCtx)
command := args[1]

// Connecting to the instance.
conn, err := connector.Connect(connOpts.Address, connOpts.Username, connOpts.Password)
if err != nil {
return nil, fmt.Errorf("Unable to establish connection: %s", err)
}

// Execution of the command.
req := connector.EvalReq(evalFuncBody, command)
res, err := conn.Exec(req)
if err != nil {
return nil, err
}

// Check that the result is encoded in YAML and convert it to bytes,
// since the ""gopkg.in/yaml.v2" library handles YAML as an array
// of bytes.
resYAML := []byte((res[0]).(string))
var checkMock interface{}
if err = yaml.Unmarshal(resYAML, &checkMock); err != nil {
return nil, err
}

return resYAML, nil
}

// runConsole run a new console.
func runConsole(connOpts *connector.ConnOpts, title string) error {
console, err := NewConsole(connOpts, title)
Expand Down
7 changes: 7 additions & 0 deletions cli/connector/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ func (conn *Conn) Exec(req *Request) ([]interface{}, error) {
return req.execFunc(conn)
}

// Eval executes a command.
func (conn *Conn) Eval(command string) ([]interface{}, error) {
evalFuncBody := "return require('console').eval(...)\n"
req := EvalReq(evalFuncBody, command)
return conn.Exec(req)
}

// ExecTyped executes an operation and returns the typed result.
func (conn *Conn) ExecTyped(req *Request, resData interface{}) error {
return req.execTypedFunc(conn, resData)
Expand Down

0 comments on commit 84e9ce6

Please sign in to comment.