Skip to content

Commit

Permalink
removed not used interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
thorstenkramm committed Aug 8, 2022
1 parent 9405599 commit ec92b9a
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 98 deletions.
22 changes: 10 additions & 12 deletions internal/pkg/controllers/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,6 @@ type IPProvider interface {
GetIP(ctx context.Context) (string, error)
}

type RDPFileWriter interface {
WriteRDPFile(fi models.FileInput) (filePath string, err error)
}

type RDPExecutor interface {
StartDefaultApp(filePath string) error
}

type TunnelController struct {
Rport *api.Rport
TunnelRenderer TunnelRenderer
Expand Down Expand Up @@ -166,12 +158,18 @@ func (tc *TunnelController) Create(ctx context.Context, params *options.Paramete
}
}

// deconstruct the values of '-r, --remote' using either <IP address>:<PORT> e.g. 127.0.0.1:22
// or just <PORT> e.g. 22.
remotePortAndHostStr := params.ReadString(config.Remote, "")
remotePortInt, _ := utils.ExtractPortAndHost(remotePortAndHostStr)
if TunnelLauncher.Scheme != "" && remotePortAndHostStr == "" {
// if '-r, --remote' is not given, try to get the port from the scheme
remotePortInt = utils.GetPortByScheme(TunnelLauncher.Scheme)
}
if remotePortAndHostStr == "" && remotePortInt > 0 {
// If we have just a port convert back to string.
// For the RPort server API a port without a host is sufficient
// to create a tunnel to this port on localhost
remotePortAndHostStr = strconv.Itoa(remotePortInt)
}

Expand Down Expand Up @@ -204,7 +202,7 @@ func (tc *TunnelController) Create(ctx context.Context, params *options.Paramete
tunnelCreated.RportServer = tc.Rport.BaseURL
tunnelCreated.ClientID = clientID
tunnelCreated.ClientName = clientName
tc.getRportServerName(&tunnelCreated)
tc.getRportServerName(tunnelCreated)
tunnelCreated.Usage = utils.GetUsageByScheme(tunnelCreated.Scheme, tunnelCreated.RportServer, tunnelCreated.Lport)

err = tc.TunnelRenderer.RenderTunnel(tunnelCreated)
Expand All @@ -223,12 +221,12 @@ func (tc *TunnelController) Create(ctx context.Context, params *options.Paramete
}

// getRportServerName extracts just the server name from the Rport API URL
func (tc *TunnelController) getRportServerName(tunnelCreated **models.TunnelCreated) {
func (tc *TunnelController) getRportServerName(tunnelCreated *models.TunnelCreated) {
var rportURL *url.URL
rportURL, err := url.Parse((*tunnelCreated).RportServer)
rportURL, err := url.Parse(tunnelCreated.RportServer)
if err != nil {
return
}
(*tunnelCreated).RportServer = rportURL.Hostname()
tunnelCreated.RportServer = rportURL.Hostname()
// @todo: Get the tunnel host from the API. Tunnel host can differ from API host
}
23 changes: 0 additions & 23 deletions internal/pkg/controllers/tunnel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import (
"github.com/cloudradar-monitoring/rportcli/internal/pkg/config"
"github.com/stretchr/testify/require"

"github.com/stretchr/testify/mock"

options "github.com/breathbath/go_utils/v2/pkg/config"

"github.com/cloudradar-monitoring/rportcli/internal/pkg/output"
Expand Down Expand Up @@ -79,27 +77,6 @@ func (ipm IPProviderMock) GetIP(ctx context.Context) (string, error) {
return ipm.IP, nil
}

type RDPWriterMock struct {
FileInput models.FileInput
filePathToGive string
errorToGive error
}

func (rwm *RDPWriterMock) WriteRDPFile(fi models.FileInput) (filePath string, err error) {
rwm.FileInput = fi
return rwm.filePathToGive, rwm.errorToGive
}

type RDPExecutorMock struct {
mock.Mock
}

func (rem *RDPExecutorMock) StartDefaultApp(filePath string) error {
args := rem.Called(filePath)

return args.Error(0)
}

func TestTunnelsController(t *testing.T) {
srv := startClientsServer()
defer srv.Close()
Expand Down
21 changes: 6 additions & 15 deletions internal/pkg/exec/exec.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
package exec

import (
"io"
"os"
"os/exec"

"github.com/sirupsen/logrus"
)

type Executor struct {
CommandProvider func(filePath string) (cmd string, args []string)
StdOut io.Writer
Stdin io.Reader
StdErr io.Writer
}

func (re *Executor) StartDefaultApp(filePath string) error {
rdpCmd, args := re.CommandProvider(filePath)
c := exec.Command(rdpCmd, args...)

c.Stdout = re.StdOut
c.Stdin = re.Stdin
c.Stderr = re.StdErr
func StartDefaultApp(filePath string) error {
c := exec.Command(OpenCmd, filePath)

c.Stdout = os.Stdout
c.Stdin = os.Stdin
c.Stderr = os.Stderr
err := c.Run()
logrus.Debugf("will run %s", c.String())
if err != nil {
Expand Down
4 changes: 0 additions & 4 deletions internal/pkg/exec/exec_nix.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,3 @@
package exec

const OpenCmd = "xdg-open"

func CommandProvider(filePath string) (cmd string, args []string) {
return OpenCmd, []string{filePath}
}
4 changes: 0 additions & 4 deletions internal/pkg/exec/exec_osx.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,3 @@
package exec

const OpenCmd = "open"

func CommandProvider(filePath string) (cmd string, args []string) {
return OpenCmd, []string{filePath}
}
23 changes: 10 additions & 13 deletions internal/pkg/exec/exec_test.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
package exec

import (
"bytes"
"testing"

"github.com/stretchr/testify/require"

"github.com/cloudradar-monitoring/rportcli/internal/pkg/recorder"

"github.com/stretchr/testify/assert"
)

func TestExecutor(t *testing.T) {
stdOut := &bytes.Buffer{}
const filePath = "file123"
e := &Executor{
CommandProvider: func(fp string) (cmd string, args []string) {
assert.Equal(t, filePath, fp)
return "echo", []string{"123"}
},
StdOut: stdOut,
}
r := recorder.NewCmdRecorder()
err := StartDefaultApp("info.txt")
require.NoError(t, err)
cmdRecords := r.GetRecords()

err := e.StartDefaultApp(filePath)
assert.NoError(t, err)
assert.Equal(t, "123\n", stdOut.String())
assert.Len(t, cmdRecords, 1)
assert.Equal(t, OpenCmd+" info.txt", cmdRecords[0])
}
7 changes: 0 additions & 7 deletions internal/pkg/exec/exec_win.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,4 @@

package exec

import "strings"

const OpenCmd = "cmd.exe /C start"

func CommandProvider(filePath string) (cmd string, args []string) {
parts := strings.Split(OpenCmd, " ")
return parts[0], []string{parts[1], parts[2], filePath}
}
15 changes: 1 addition & 14 deletions internal/pkg/launcher/rdp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,12 @@ package launcher

import (
"fmt"
"os"

"github.com/cloudradar-monitoring/rportcli/internal/pkg/exec"
"github.com/cloudradar-monitoring/rportcli/internal/pkg/models"
"github.com/cloudradar-monitoring/rportcli/internal/pkg/rdp"
)

type RDPFileWriter interface {
WriteRDPFile(fi models.FileInput) (filePath string, err error)
}

type RDPExecutor interface {
StartDefaultApp(filePath string) error
}

func LaunchRDPTunnel(tunnelCreated *models.TunnelCreated, user string, height, width int) error {
clientName := tunnelCreated.ClientName
if clientName == "" {
Expand All @@ -35,9 +26,5 @@ func LaunchRDPTunnel(tunnelCreated *models.TunnelCreated, user string, height, w
return err
}

rdpExecutor := &exec.Executor{
CommandProvider: exec.CommandProvider,
StdErr: os.Stderr,
}
return rdpExecutor.StartDefaultApp(filePath)
return exec.StartDefaultApp(filePath)
}
7 changes: 1 addition & 6 deletions internal/pkg/launcher/uri.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package launcher

import (
"fmt"
"os"

"github.com/cloudradar-monitoring/rportcli/internal/pkg/exec"
"github.com/cloudradar-monitoring/rportcli/internal/pkg/models"
Expand All @@ -12,9 +11,5 @@ import (
func LaunchURITunnel(tunnelCreated *models.TunnelCreated, scheme string) error {
// Create a URI to be opened by the default app of OS
uri := fmt.Sprintf("%s://%s:%s", utils.GetHandlerByScheme(scheme), tunnelCreated.RportServer, tunnelCreated.Lport)
uriExecutor := &exec.Executor{
CommandProvider: exec.CommandProvider,
StdErr: os.Stderr,
}
return uriExecutor.StartDefaultApp(uri)
return exec.StartDefaultApp(uri)
}

0 comments on commit ec92b9a

Please sign in to comment.