Skip to content

Commit cdd3c14

Browse files
committed
connect: add the ability to use an instance name as the address
Part of #15
1 parent ed7f9e0 commit cdd3c14

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

cli/cmd/connect.go

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ package cmd
22

33
import (
44
"fmt"
5+
"io/ioutil"
6+
"os"
7+
"path/filepath"
58
"syscall"
69

710
"github.com/apex/log"
@@ -36,25 +39,60 @@ func NewConnectCmd() *cobra.Command {
3639
return connectCmd
3740
}
3841

42+
// resolveInstAddr checks if the instance name is used as the address and
43+
// replaces it with a control socket if so.
44+
func resolveInstAddr(cmdCtx *cmdcontext.CmdCtx, cliOpts *modules.CliOpts, args []string) ([]string, error) {
45+
var err error
46+
newArgs := args
47+
48+
runDir := cliOpts.App.RunDir
49+
if runDir == "" {
50+
if runDir, err = os.Getwd(); err != nil {
51+
return newArgs, err
52+
}
53+
}
54+
55+
files, err := ioutil.ReadDir(runDir)
56+
if err != nil {
57+
return newArgs, err
58+
}
59+
60+
for _, file := range files {
61+
if file.Name() == newArgs[0]+".pid" {
62+
newArgs[0] = filepath.Join(runDir, newArgs[0]+".control")
63+
break
64+
}
65+
}
66+
67+
return newArgs, nil
68+
}
69+
3970
// internalConnectModule is a default connect module.
4071
func internalConnectModule(cmdCtx *cmdcontext.CmdCtx, args []string) error {
4172
argsLen := len(args)
4273
if argsLen < 1 {
4374
return fmt.Errorf("Incorrect combination of command parameters")
4475
}
4576

77+
cliOpts, err := modules.GetCliOpts(cmdCtx.Cli.ConfigPath)
78+
if err != nil {
79+
return err
80+
}
81+
4682
cmdCtx.Connect.Username = connectUser
4783
cmdCtx.Connect.Password = connectPassword
4884

85+
newArgs, _ := resolveInstAddr(cmdCtx, cliOpts, args)
86+
4987
if argsLen == 1 {
5088
if terminal.IsTerminal(syscall.Stdin) {
5189
log.Info("Connecting to the instance...")
5290
}
53-
if err := connect.Connect(cmdCtx, args); err != nil {
91+
if err := connect.Connect(cmdCtx, newArgs); err != nil {
5492
return err
5593
}
5694
} else if argsLen == 2 {
57-
res, err := connect.Eval(cmdCtx, args)
95+
res, err := connect.Eval(cmdCtx, newArgs)
5896
if err != nil {
5997
return err
6098
}

0 commit comments

Comments
 (0)