passing a command by reference to 'injectProxy' instead of return types

This commit is contained in:
Baipyrus 2024-10-01 20:19:02 +02:00
parent 39a0a6d828
commit b865c7c5af

View File

@ -21,33 +21,32 @@ func processVars(cmd *util.Command, isVariableType bool) {
} }
} }
func injectProxy(configArgs []string, configCmd, proxyServer string, variant *util.Variant) ([]string, string) { func injectProxy(cmd *util.Command, variant *util.Variant, proxyServer string) {
// Skip, no proxy provided or proxy option discarded // Skip if no proxy is provided or proxy option is discarded
if proxyServer == "" || variant.DiscardProxy { if proxyServer == "" || variant.DiscardProxy {
return configArgs, configCmd return
} }
// Surround 'proxyServer' with any given string, if provided // Surround 'proxyServer' with the provided surround string, if any
if variant.Surround != "" { if variant.Surround != "" {
proxyServer = fmt.Sprintf("%[1]s%[2]s%[1]s", variant.Surround, proxyServer) proxyServer = fmt.Sprintf("%[1]s%[2]s%[1]s", variant.Surround, proxyServer)
} }
// Insert proxy only on last VARIABLE type // Insert proxy into last place of command if the variant is VARIABLE
if variant.Type == util.VARIABLE && strings.Count(configCmd, "$PRSW_ARG") == 1 { if variant.Type == util.VARIABLE && strings.Count(cmd.Name, "$PRSW_ARG") == 1 {
configCmd = strings.Replace(configCmd, "$PRSW_ARG", proxyServer, 1) cmd.Name = strings.Replace(cmd.Name, "$PRSW_ARG", proxyServer, 1)
return configArgs, configCmd return
} }
// Insert proxy right after equator // Insert proxy after the equator if specified
if variant.Equator != "" { if variant.Equator != "" {
configArgs[len(configArgs)-1] += variant.Equator + proxyServer lastArgIdx := len(cmd.Arguments) - 1
return configArgs, configCmd cmd.Arguments[lastArgIdx] += variant.Equator + proxyServer
return
} }
// Or otherwise just append it as an argument // Otherwise, append the proxy as a new argument
configArgs = append(configArgs, proxyServer) cmd.Arguments = append(cmd.Arguments, proxyServer)
return configArgs, configCmd
} }
func generateCommands(base string, variants []*util.Variant, proxyServer string) []*util.Command { func generateCommands(base string, variants []*util.Variant, proxyServer string) []*util.Command {
@ -64,9 +63,9 @@ func generateCommands(base string, variants []*util.Variant, proxyServer string)
} }
processVars(cmd, isVariableType) processVars(cmd, isVariableType)
configArgs, configCmd := injectProxy(cmd.Arguments, cmd.Name, proxyServer, variant) injectProxy(cmd, variant, proxyServer)
commands = append(commands, &util.Command{Name: configCmd, Arguments: configArgs}) commands = append(commands, cmd)
} }
return commands return commands