Skip to content

Commit

Permalink
Merge pull request #266 from jakecorrenti/fix-command-output
Browse files Browse the repository at this point in the history
Fix Command type ToCmdline bug
  • Loading branch information
openshift-merge-robot authored Sep 7, 2023
2 parents 466be91 + 9006f54 commit 4f1bed3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 35 deletions.
56 changes: 28 additions & 28 deletions pkg/types/command.go → pkg/types/gvproxy_command.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package types

import (
"fmt"
"os/exec"
"strconv"
)

type Command struct {
type GvproxyCommand struct {
// Print packets on stderr
Debug bool

Expand All @@ -29,8 +29,8 @@ type Command struct {
SSHPort int
}

func NewCommand() Command {
return Command{
func NewGvproxyCommand() GvproxyCommand {
return GvproxyCommand{
MTU: 1500,
SSHPort: 2222,
endpoints: []string{},
Expand All @@ -39,96 +39,96 @@ func NewCommand() Command {
}
}

func (c *Command) checkSocketsInitialized() {
func (c *GvproxyCommand) checkSocketsInitialized() {
if len(c.sockets) < 1 {
c.sockets = map[string]string{}
}
}

func (c *Command) checkForwardInfoInitialized() {
func (c *GvproxyCommand) checkForwardInfoInitialized() {
if len(c.forwardInfo) < 1 {
c.forwardInfo = map[string][]string{}
}
}

func (c *Command) AddEndpoint(endpoint string) {
func (c *GvproxyCommand) AddEndpoint(endpoint string) {
if len(c.endpoints) < 1 {
c.endpoints = []string{}
}

c.endpoints = append(c.endpoints, endpoint)
}

func (c *Command) AddVpnkitSocket(socket string) {
func (c *GvproxyCommand) AddVpnkitSocket(socket string) {
c.checkSocketsInitialized()
c.sockets["listen-vpnkit"] = socket
}

func (c *Command) AddQemuSocket(socket string) {
func (c *GvproxyCommand) AddQemuSocket(socket string) {
c.checkSocketsInitialized()
c.sockets["listen-qemu"] = socket
}

func (c *Command) AddBessSocket(socket string) {
func (c *GvproxyCommand) AddBessSocket(socket string) {
c.checkSocketsInitialized()
c.sockets["listen-bess"] = socket
}

func (c *Command) AddStdioSocket(socket string) {
func (c *GvproxyCommand) AddStdioSocket(socket string) {
c.checkSocketsInitialized()
c.sockets["listen-stdio"] = socket
}

func (c *Command) AddVfkitSocket(socket string) {
func (c *GvproxyCommand) AddVfkitSocket(socket string) {
c.checkSocketsInitialized()
c.sockets["listen-vfkit"] = socket
}

func (c *Command) addForwardInfo(flag, value string) {
func (c *GvproxyCommand) addForwardInfo(flag, value string) {
c.forwardInfo[flag] = append(c.forwardInfo[flag], value)
}

func (c *Command) AddForwardSock(socket string) {
func (c *GvproxyCommand) AddForwardSock(socket string) {
c.checkForwardInfoInitialized()
c.addForwardInfo("forward-sock", socket)
}

func (c *Command) AddForwardDest(dest string) {
func (c *GvproxyCommand) AddForwardDest(dest string) {
c.checkForwardInfoInitialized()
c.addForwardInfo("forward-dest", dest)
}

func (c *Command) AddForwardUser(user string) {
func (c *GvproxyCommand) AddForwardUser(user string) {
c.checkForwardInfoInitialized()
c.addForwardInfo("forward-user", user)
}

func (c *Command) AddForwardIdentity(identity string) {
func (c *GvproxyCommand) AddForwardIdentity(identity string) {
c.checkForwardInfoInitialized()
c.addForwardInfo("forward-identity", identity)
}

// socketsToCmdline converts Command.sockets to a commandline format
func (c *Command) socketsToCmdline() []string {
func (c *GvproxyCommand) socketsToCmdline() []string {
args := []string{}

for socketFlag, socket := range c.sockets {
if socket != "" {
args = append(args, fmt.Sprintf("-%s %s", socketFlag, socket))
args = append(args, "-"+socketFlag, socket)
}
}

return args
}

// forwardInfoToCmdline converts Command.forwardInfo to a commandline format
func (c *Command) forwardInfoToCmdline() []string {
func (c *GvproxyCommand) forwardInfoToCmdline() []string {
args := []string{}

for forwardInfoFlag, forwardInfo := range c.forwardInfo {
for _, i := range forwardInfo {
if i != "" {
args = append(args, fmt.Sprintf("-%s %s", forwardInfoFlag, i))
args = append(args, "-"+forwardInfoFlag, i)
}
}
}
Expand All @@ -137,12 +137,12 @@ func (c *Command) forwardInfoToCmdline() []string {
}

// endpointsToCmdline converts Command.endpoints to a commandline format
func (c *Command) endpointsToCmdline() []string {
func (c *GvproxyCommand) endpointsToCmdline() []string {
args := []string{}

for _, endpoint := range c.endpoints {
if endpoint != "" {
args = append(args, "-listen "+endpoint)
args = append(args, "-listen", endpoint)
}
}

Expand All @@ -151,7 +151,7 @@ func (c *Command) endpointsToCmdline() []string {

// ToCmdline converts Command to a properly formatted command for gvproxy based
// on its fields
func (c *Command) ToCmdline() []string {
func (c *GvproxyCommand) ToCmdline() []string {
args := []string{}

// listen (endpoints)
Expand All @@ -163,10 +163,10 @@ func (c *Command) ToCmdline() []string {
}

// mtu
args = append(args, fmt.Sprintf("-mtu %d", c.MTU))
args = append(args, "-mtu", strconv.Itoa(c.MTU))

// ssh-port
args = append(args, fmt.Sprintf("-ssh-port %d", c.SSHPort))
args = append(args, "-ssh-port", strconv.Itoa(c.SSHPort))

// sockets
args = append(args, c.socketsToCmdline()...)
Expand All @@ -176,14 +176,14 @@ func (c *Command) ToCmdline() []string {

// pid-file
if c.PidFile != "" {
args = append(args, "-pid-file "+c.PidFile)
args = append(args, "-pid-file", c.PidFile)
}

return args
}

// Cmd converts Command to a commandline format and returns an exec.Cmd which
// can be executed by os/exec
func (c *Command) Cmd(gvproxyPath string) *exec.Cmd {
func (c *GvproxyCommand) Cmd(gvproxyPath string) *exec.Cmd {
return exec.Command(gvproxyPath, c.ToCmdline()...) // #nosec G204
}
14 changes: 7 additions & 7 deletions test/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ var _ = Describe("dns", func() {

var _ = Describe("command-line format", func() {
It("should convert Command to command line format", func() {
command := types.NewCommand()
command := types.NewGvproxyCommand()
command.AddEndpoint("unix:///tmp/network.sock")
command.Debug = true
command.AddQemuSocket("tcp://0.0.0.0:1234")
Expand All @@ -208,13 +208,13 @@ var _ = Describe("command-line format", func() {

cmd := command.ToCmdline()
Expect(cmd).To(Equal([]string{
"-listen unix:///tmp/network.sock",
"-listen", "unix:///tmp/network.sock",
"-debug",
"-mtu 1500",
"-ssh-port 2222",
"-listen-qemu tcp://0.0.0.0:1234",
"-forward-user demouser",
"-pid-file ~/gv-pidfile.txt",
"-mtu", "1500",
"-ssh-port", "2222",
"-listen-qemu", "tcp://0.0.0.0:1234",
"-forward-user", "demouser",
"-pid-file", "~/gv-pidfile.txt",
}))
})
})

0 comments on commit 4f1bed3

Please sign in to comment.