Skip to content

Commit 4d33be6

Browse files
committed
chore: Add hidden options to support v1-to-v2 translation.
1 parent f951767 commit 4d33be6

File tree

4 files changed

+73
-1
lines changed

4 files changed

+73
-1
lines changed

cmd/options.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,30 @@ func WithAutoIP() Option {
7676
}
7777
}
7878

79+
// WithProxyV1Compatibility enables legacy behavior of v1 and will print
80+
// out "Ready for new connections" when the proxy starts.
81+
func WithProxyV1Compatibility() Option {
82+
return func(c *Command) {
83+
c.conf.ProxyV1Compatibility = true
84+
}
85+
}
86+
87+
// WithProxyV1LogDebugStdout enables legacy behavior of v1 and will print
88+
// debug/info logs to stdout instead of stderr.
89+
func WithProxyV1LogDebugStdout() Option {
90+
return func(c *Command) {
91+
c.conf.LogDebugStdout = true
92+
}
93+
}
94+
95+
// WithProxyV1Verbose enables legacy behavior of v1 and will set
96+
// the debug-logs flag on the v2 proxy.
97+
func WithProxyV1Verbose(v bool) Option {
98+
return func(c *Command) {
99+
c.conf.DebugLogs = v
100+
}
101+
}
102+
79103
// WithQuietLogging configures the Proxy to log error messages only.
80104
func WithQuietLogging() Option {
81105
return func(c *Command) {

cmd/options_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,36 @@ func TestCommandOptions(t *testing.T) {
146146
},
147147
option: WithLazyRefresh(),
148148
},
149+
{
150+
desc: "with proxy v1 compatibility",
151+
isValid: func(c *Command) error {
152+
if !c.conf.ProxyV1Compatibility {
153+
return errors.New("compatibility was false, but should be true")
154+
}
155+
return nil
156+
},
157+
option: WithProxyV1Compatibility(),
158+
},
159+
{
160+
desc: "with proxy v1 verbose true",
161+
isValid: func(c *Command) error {
162+
if !c.conf.DebugLogs {
163+
return errors.New("DebugLogs was false, but should be true")
164+
}
165+
return nil
166+
},
167+
option: WithProxyV1Verbose(true),
168+
},
169+
{
170+
desc: "with proxy v1 verbose false",
171+
isValid: func(c *Command) error {
172+
if c.conf.DebugLogs {
173+
return errors.New("DebugLogs was true, but should be false")
174+
}
175+
return nil
176+
},
177+
option: WithProxyV1Verbose(false),
178+
},
149179
}
150180

151181
for _, tc := range tcs {

cmd/root.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,12 @@ func semanticVersion() string {
7373
// Execute adds all child commands to the root command and sets flags appropriately.
7474
// This is called by main.main(). It only needs to happen once to the rootCmd.
7575
func Execute() {
76-
if err := NewCommand().Execute(); err != nil {
76+
ExecuteCommand(NewCommand())
77+
}
78+
79+
// ExecuteCommand runs a preconfigured command, and exits the appropriate error code.
80+
func ExecuteCommand(c *Command) {
81+
if err := c.Execute(); err != nil {
7782
exit := 1
7883
var terr *exitError
7984
if errors.As(err, &terr) {
@@ -646,6 +651,8 @@ func loadConfig(c *Command, args []string, opts []Option) error {
646651

647652
if c.conf.Quiet {
648653
c.logger = log.NewStdLogger(io.Discard, os.Stderr)
654+
} else if c.conf.LogDebugStdout {
655+
c.logger = log.NewStdLogger(os.Stdout, os.Stderr)
649656
}
650657

651658
err = parseConfig(c, c.conf, args)
@@ -1105,6 +1112,9 @@ func runSignalWrapper(cmd *Command) (err error) {
11051112
return err
11061113
case p = <-startCh:
11071114
cmd.logger.Infof("The proxy has started successfully and is ready for new connections!")
1115+
if cmd.conf.ProxyV1Compatibility {
1116+
cmd.logger.Infof("Ready for new connections")
1117+
}
11081118
// If running under systemd with Type=notify, it will send a message to the
11091119
// service manager that it is ready to handle connections now.
11101120
go func() {

internal/proxy/proxy.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,14 @@ type Config struct {
188188
// endpoint for all instances
189189
PSC bool
190190

191+
// ProxyV1Compatibility supports a legacy behavior where the Proxy will
192+
// print out "Ready for new connections" when the proxy starts.
193+
ProxyV1Compatibility bool
194+
195+
// LogDebugStdout supports a legacy behavior where the Proxy will
196+
// print debug/info logs to stdout instead of stderr.
197+
LogDebugStdout bool
198+
191199
// AutoIP supports a legacy behavior where the Proxy will connect to
192200
// the first IP address returned from the SQL ADmin API response. This
193201
// setting should be avoided and used only to support legacy Proxy

0 commit comments

Comments
 (0)