@@ -2,6 +2,9 @@ package cmd
2
2
3
3
import (
4
4
"fmt"
5
+ "io/ioutil"
6
+ "os"
7
+ "path/filepath"
5
8
"syscall"
6
9
7
10
"github.com/apex/log"
@@ -36,25 +39,60 @@ func NewConnectCmd() *cobra.Command {
36
39
return connectCmd
37
40
}
38
41
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
+
39
70
// internalConnectModule is a default connect module.
40
71
func internalConnectModule (cmdCtx * cmdcontext.CmdCtx , args []string ) error {
41
72
argsLen := len (args )
42
73
if argsLen < 1 {
43
74
return fmt .Errorf ("Incorrect combination of command parameters" )
44
75
}
45
76
77
+ cliOpts , err := modules .GetCliOpts (cmdCtx .Cli .ConfigPath )
78
+ if err != nil {
79
+ return err
80
+ }
81
+
46
82
cmdCtx .Connect .Username = connectUser
47
83
cmdCtx .Connect .Password = connectPassword
48
84
85
+ newArgs , _ := resolveInstAddr (cmdCtx , cliOpts , args )
86
+
49
87
if argsLen == 1 {
50
88
if terminal .IsTerminal (syscall .Stdin ) {
51
89
log .Info ("Connecting to the instance..." )
52
90
}
53
- if err := connect .Connect (cmdCtx , args ); err != nil {
91
+ if err := connect .Connect (cmdCtx , newArgs ); err != nil {
54
92
return err
55
93
}
56
94
} else if argsLen == 2 {
57
- res , err := connect .Eval (cmdCtx , args )
95
+ res , err := connect .Eval (cmdCtx , newArgs )
58
96
if err != nil {
59
97
return err
60
98
}
0 commit comments