-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The minimum required part of the connect module from the cartridge-cli (https://github.com/tarantool/cartridge-cli) was copied without any changes. Part of #15
- Loading branch information
Showing
22 changed files
with
1,892 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/apex/log" | ||
"github.com/spf13/cobra" | ||
"github.com/tarantool/tt/cli/cmdcontext" | ||
"github.com/tarantool/tt/cli/connect" | ||
"github.com/tarantool/tt/cli/modules" | ||
) | ||
|
||
var ( | ||
connectUser string | ||
connectPassword string | ||
) | ||
|
||
// NewConnectCmd creates connect command. | ||
func NewConnectCmd() *cobra.Command { | ||
var connectCmd = &cobra.Command{ | ||
Use: "connect [INSTANCE_NAME]", | ||
Short: "Start tarantool instance", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
err := modules.RunCmd(&cmdCtx, cmd.Name(), &modulesInfo, internalConnectModule, args) | ||
if err != nil { | ||
log.Fatalf(err.Error()) | ||
} | ||
}, | ||
} | ||
|
||
connectCmd.Flags().StringVarP(&connectUser, "username", "u", "", "Username") | ||
connectCmd.Flags().StringVarP(&connectPassword, "password", "p", "", "Password") | ||
|
||
return connectCmd | ||
} | ||
|
||
// internalConnectModule is a default connect module. | ||
func internalConnectModule(cmdCtx *cmdcontext.CmdCtx, args []string) error { | ||
// Fill CmdCtx. | ||
cmdCtx.Connect.Username = connectUser | ||
cmdCtx.Connect.Password = connectPassword | ||
|
||
log.Info("Connecting to the instance...") | ||
if err := connect.Connect(cmdCtx, args); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package connect | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/tarantool/tt/cli/cmdcontext" | ||
) | ||
|
||
const ( | ||
MaxHistoryLines = 10000 | ||
|
||
TCPNetwork = "tcp" | ||
UnixNetwork = "unix" | ||
) | ||
|
||
type ConnOpts struct { | ||
Network string | ||
Address string | ||
Username string | ||
Password string | ||
} | ||
|
||
func getConnOpts(connString string, cmdCtx *cmdcontext.CmdCtx) (*ConnOpts, error) { | ||
connOpts := ConnOpts{ | ||
Username: cmdCtx.Connect.Username, | ||
Password: cmdCtx.Connect.Password, | ||
} | ||
|
||
connStringParts := strings.SplitN(connString, "@", 2) | ||
address := connStringParts[len(connStringParts)-1] | ||
|
||
if len(connStringParts) > 1 { | ||
authString := connStringParts[0] | ||
authStringParts := strings.SplitN(authString, ":", 2) | ||
|
||
if connOpts.Username == "" { | ||
connOpts.Username = authStringParts[0] | ||
} | ||
if len(authStringParts) > 1 && connOpts.Password == "" { | ||
connOpts.Password = authStringParts[1] | ||
} | ||
} | ||
|
||
addrLen := len(address) | ||
switch { | ||
case addrLen > 0 && (address[0] == '.' || address[0] == '/'): | ||
connOpts.Network = UnixNetwork | ||
connOpts.Address = address | ||
case addrLen >= 7 && address[0:7] == "unix://": | ||
connOpts.Network = UnixNetwork | ||
connOpts.Address = address[7:] | ||
case addrLen >= 5 && address[0:5] == "unix:": | ||
connOpts.Network = UnixNetwork | ||
connOpts.Address = address[5:] | ||
case addrLen >= 6 && address[0:6] == "unix/:": | ||
connOpts.Network = UnixNetwork | ||
connOpts.Address = address[6:] | ||
case addrLen >= 6 && address[0:6] == "tcp://": | ||
connOpts.Network = TCPNetwork | ||
connOpts.Address = address[6:] | ||
case addrLen >= 4 && address[0:4] == "tcp:": | ||
connOpts.Network = TCPNetwork | ||
connOpts.Address = address[4:] | ||
default: | ||
connOpts.Network = TCPNetwork | ||
connOpts.Address = address | ||
} | ||
|
||
return &connOpts, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package connect | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/tarantool/tt/cli/cmdcontext" | ||
) | ||
|
||
const ( | ||
// see https://github.com/tarantool/tarantool/blob/b53cb2aeceedc39f356ceca30bd0087ee8de7c16/src/box/lua/console.c#L265 | ||
tarantoolWordSeparators = "\t\r\n !\"#$%&'()*+,-/;<=>?@[\\]^`{|}~" | ||
) | ||
|
||
func Connect(cmdCtx *cmdcontext.CmdCtx, args []string) error { | ||
if len(args) != 1 { | ||
return fmt.Errorf("Should be specified one connection string") | ||
} | ||
|
||
connString := args[0] | ||
|
||
connOpts, err := getConnOpts(connString, cmdCtx) | ||
if err != nil { | ||
return fmt.Errorf("Failed to get connection opts: %s", err) | ||
} | ||
|
||
if err := runConsole(connOpts, ""); err != nil { | ||
return fmt.Errorf("Failed to run interactive console: %s", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func runConsole(connOpts *ConnOpts, title string) error { | ||
console, err := NewConsole(connOpts, title) | ||
if err != nil { | ||
return fmt.Errorf("Failed to create new console: %s", err) | ||
} | ||
defer console.Close() | ||
|
||
if err := console.Run(); err != nil { | ||
return fmt.Errorf("Failed to start new console: %s", err) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.