Skip to content

Commit ed7f9e0

Browse files
committed
connect: add eval functionality to the module
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
1 parent 8fb1edf commit ed7f9e0

File tree

3 files changed

+62
-6
lines changed

3 files changed

+62
-6
lines changed

cli/cmd/connect.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmd
22

33
import (
4+
"fmt"
45
"syscall"
56

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

3839
// internalConnectModule is a default connect module.
3940
func internalConnectModule(cmdCtx *cmdcontext.CmdCtx, args []string) error {
40-
// Fill CmdCtx.
41+
argsLen := len(args)
42+
if argsLen < 1 {
43+
return fmt.Errorf("Incorrect combination of command parameters")
44+
}
45+
4146
cmdCtx.Connect.Username = connectUser
4247
cmdCtx.Connect.Password = connectPassword
4348

44-
if terminal.IsTerminal(syscall.Stdin) {
45-
log.Info("Connecting to the instance...")
46-
}
47-
if err := connect.Connect(cmdCtx, args); err != nil {
48-
return err
49+
if argsLen == 1 {
50+
if terminal.IsTerminal(syscall.Stdin) {
51+
log.Info("Connecting to the instance...")
52+
}
53+
if err := connect.Connect(cmdCtx, args); err != nil {
54+
return err
55+
}
56+
} else if argsLen == 2 {
57+
res, err := connect.Eval(cmdCtx, args)
58+
if err != nil {
59+
return err
60+
}
61+
// "Println" is used instead of "log..." to print the result without any decoration.
62+
fmt.Println(string(res))
63+
} else {
64+
return fmt.Errorf("Incorrect combination of command parameters")
4965
}
5066

5167
return nil

cli/connect/connect.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/tarantool/tt/cli/cmdcontext"
77
"github.com/tarantool/tt/cli/connector"
8+
"gopkg.in/yaml.v2"
89
)
910

1011
const (
@@ -32,6 +33,38 @@ func Connect(cmdCtx *cmdcontext.CmdCtx, args []string) error {
3233
return nil
3334
}
3435

36+
// Eval executes the command on the remote instance (according to args).
37+
func Eval(cmdCtx *cmdcontext.CmdCtx, args []string) ([]byte, error) {
38+
// Parse the arguments.
39+
connString := args[0]
40+
connOpts := getConnOpts(connString, cmdCtx)
41+
command := args[1]
42+
43+
// Connecting to the instance.
44+
conn, err := connector.Connect(connOpts.Address, connOpts.Username, connOpts.Password)
45+
if err != nil {
46+
return nil, fmt.Errorf("Unable to establish connection: %s", err)
47+
}
48+
49+
// Execution of the command.
50+
req := connector.EvalReq(evalFuncBody, command)
51+
res, err := conn.Exec(req)
52+
if err != nil {
53+
return nil, err
54+
}
55+
56+
// Check that the result is encoded in YAML and convert it to bytes,
57+
// since the ""gopkg.in/yaml.v2" library handles YAML as an array
58+
// of bytes.
59+
resYAML := []byte((res[0]).(string))
60+
var checkMock interface{}
61+
if err = yaml.Unmarshal(resYAML, &checkMock); err != nil {
62+
return nil, err
63+
}
64+
65+
return resYAML, nil
66+
}
67+
3568
// runConsole run a new console.
3669
func runConsole(connOpts *connector.ConnOpts, title string) error {
3770
console, err := NewConsole(connOpts, title)

cli/connector/connector.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,13 @@ func (conn *Conn) Exec(req *Request) ([]interface{}, error) {
8585
return req.execFunc(conn)
8686
}
8787

88+
// Eval executes a command.
89+
func (conn *Conn) Eval(command string) ([]interface{}, error) {
90+
evalFuncBody := "return require('console').eval(...)\n"
91+
req := EvalReq(evalFuncBody, command)
92+
return conn.Exec(req)
93+
}
94+
8895
// ExecTyped executes an operation and returns the typed result.
8996
func (conn *Conn) ExecTyped(req *Request, resData interface{}) error {
9097
return req.execTypedFunc(conn, resData)

0 commit comments

Comments
 (0)