From 72fcc6d09f6434238d88618ae3be059406d46792 Mon Sep 17 00:00:00 2001 From: Scott Leggett Date: Fri, 21 Jul 2023 14:32:50 +0800 Subject: [PATCH] fix: handle empty logs= value --- internal/sshserver/connectionparams.go | 4 +++- internal/sshserver/connectionparams_test.go | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/internal/sshserver/connectionparams.go b/internal/sshserver/connectionparams.go index 2fb42e18..0ea1b95d 100644 --- a/internal/sshserver/connectionparams.go +++ b/internal/sshserver/connectionparams.go @@ -10,7 +10,7 @@ import ( var ( serviceRegex = regexp.MustCompile(`^service=(.+)`) containerRegex = regexp.MustCompile(`^container=(.+)`) - logsRegex = regexp.MustCompile(`^logs=(.+)`) + logsRegex = regexp.MustCompile(`^logs=(.+)?`) tailLinesRegex = regexp.MustCompile(`^tailLines=(\d+)$`) ) @@ -117,6 +117,8 @@ func parseLogsArg(service, logs string, cmd []string) (bool, int64, error) { for _, arg := range strings.Split(logs, ",") { matches := tailLinesRegex.FindStringSubmatch(arg) switch { + case arg == "": + // ignore empty arg case arg == "follow": follow = true case len(matches) == 2: diff --git a/internal/sshserver/connectionparams_test.go b/internal/sshserver/connectionparams_test.go index 81931a9c..49bb54f8 100644 --- a/internal/sshserver/connectionparams_test.go +++ b/internal/sshserver/connectionparams_test.go @@ -74,6 +74,15 @@ func TestParseConnectionParams(t *testing.T) { args: []string{"drush do something"}, }, }, + "service, container and empty logs params": { + input: []string{"service=nginx", "container=php", "logs=", "drush do something"}, + expect: parsedParams{ + service: "nginx", + container: "php", + logs: "", + args: []string{"drush do something"}, + }, + }, "service, container and logs params (wrong order)": { input: []string{"service=nginx", "logs=follow", "container=php", "drush do something"}, expect: parsedParams{ @@ -225,6 +234,13 @@ func TestValidateConnectionParams(t *testing.T) { err: sshserver.ErrInvalidLogsValue, }, }, + "empty logs args - rely on default": { + input: parsedParams{ + service: "nginx-php", + logs: "", + }, + expect: result{}, + }, } for name, tc := range testCases { t.Run(name, func(tt *testing.T) {