Skip to content

Commit

Permalink
connect: add the ability to pass a filename as an argument for eval
Browse files Browse the repository at this point in the history
Closes #15
  • Loading branch information
LeonidVas committed Apr 20, 2022
1 parent acec1f3 commit c8270d6
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion cli/connect/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package connect

import (
"fmt"
"io/ioutil"
"os"
"path"

"github.com/tarantool/tt/cli/cmdcontext"
"github.com/tarantool/tt/cli/connector"
Expand All @@ -17,6 +20,21 @@ func getConnOpts(connString string, cmdCtx *cmdcontext.CmdCtx) *connector.ConnOp
return connector.GetConnOpts(connString, cmdCtx.Connect.Username, cmdCtx.Connect.Password)
}

// getEvalCmd returns a command from a file if a filename was passed,
// or a command if it was passed directly.
func getEvalCmd(cmdArg string) (string, error) {
cmdPath := path.Clean(cmdArg)
if _, err := os.Stat(cmdPath); err == nil {
cmd, err := ioutil.ReadFile(cmdPath)
if err != nil {
return "", err
}
return string(cmd), nil
}

return cmdArg, nil
}

// Connect establishes a connection to the instance and starts the console.
func Connect(cmdCtx *cmdcontext.CmdCtx, args []string) error {
if len(args) != 1 {
Expand All @@ -38,7 +56,10 @@ func Eval(cmdCtx *cmdcontext.CmdCtx, args []string) ([]byte, error) {
// Parse the arguments.
connString := args[0]
connOpts := getConnOpts(connString, cmdCtx)
command := args[1]
command, err := getEvalCmd(args[1])
if err != nil {
return nil, err
}

// Connecting to the instance.
conn, err := connector.Connect(connOpts.Address, connOpts.Username, connOpts.Password)
Expand Down

0 comments on commit c8270d6

Please sign in to comment.