mirror of
https://github.com/ProtonMail/proton-bridge.git
synced 2025-12-10 04:36:43 +00:00
GODT-2039: fix --parent-pid flag is removed from command-line when restarting the application.
This commit is contained in:
@ -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...)
|
||||
|
||||
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user