GODT-2039: fix --parent-pid flag is removed from command-line when restarting the application.

This commit is contained in:
Xavier Michelon
2022-11-11 11:10:27 +01:00
parent 984c43cd75
commit bbcb7ad980
2 changed files with 46 additions and 0 deletions

View File

@ -20,6 +20,7 @@ package base
import (
"os"
"strconv"
"strings"
"github.com/sirupsen/logrus"
"golang.org/x/sys/execabs"
@ -38,6 +39,8 @@ func (b *Base) restartApp(crash bool) error {
args = os.Args[1:]
}
args = removeFlagWithValue(args, FlagParentPID)
if b.launcher != "" {
args = forceLauncherFlag(args, b.launcher)
}
@ -85,6 +88,30 @@ func incrementRestartFlag(args []string) []string {
return res
}
// removeFlagWithValue removes a flag that requires a value from a list of command line parameters.
// The flag can be of the following form `-flag value`, `--flag value`, `-flag=value` or `--flags=value`.
func removeFlagWithValue(argList []string, flag string) []string {
var result []string
for i := 0; i < len(argList); i++ {
arg := argList[i]
// "detect the parameter form "-flag value" or "--paramName value"
if (arg == "-"+flag) || (arg == "--"+flag) {
i++
continue
}
// "detect the form "--flag=value" or "--flag=value"
if strings.HasPrefix(arg, "-"+flag+"=") || (strings.HasPrefix(arg, "--"+flag+"=")) {
continue
}
result = append(result, arg)
}
return result
}
// forceLauncherFlag replace or add the launcher args with the one set in the app.
func forceLauncherFlag(args []string, launcher string) []string {
res := append([]string{}, args...)

View File

@ -61,3 +61,22 @@ func TestVersionLessThan(t *testing.T) {
r.False(current.LessThan(current))
r.False(newer.LessThan(current))
}
func TestRemoveFlagWithValue(t *testing.T) {
tests := []struct {
argList []string
flag string
expected []string
}{
{[]string{}, "b", nil},
{[]string{"-a", "-b=value", "-c"}, "b", []string{"-a", "-c"}},
{[]string{"-a", "--b=value", "-c"}, "b", []string{"-a", "-c"}},
{[]string{"-a", "-b", "value", "-c"}, "b", []string{"-a", "-c"}},
{[]string{"-a", "--b", "value", "-c"}, "b", []string{"-a", "-c"}},
{[]string{"-a", "-B=value", "-c"}, "b", []string{"-a", "-B=value", "-c"}},
}
for _, tt := range tests {
require.Equal(t, removeFlagWithValue(tt.argList, tt.flag), tt.expected)
}
}