From ec92b9ada51d38fab4b7092f11c952a9c7f90cb5 Mon Sep 17 00:00:00 2001 From: Thorsten Kramm Date: Mon, 8 Aug 2022 14:34:36 +0200 Subject: [PATCH] removed not used interfaces --- internal/pkg/controllers/tunnel.go | 22 ++++++++++------------ internal/pkg/controllers/tunnel_test.go | 23 ----------------------- internal/pkg/exec/exec.go | 21 ++++++--------------- internal/pkg/exec/exec_nix.go | 4 ---- internal/pkg/exec/exec_osx.go | 4 ---- internal/pkg/exec/exec_test.go | 23 ++++++++++------------- internal/pkg/exec/exec_win.go | 7 ------- internal/pkg/launcher/rdp.go | 15 +-------------- internal/pkg/launcher/uri.go | 7 +------ 9 files changed, 28 insertions(+), 98 deletions(-) diff --git a/internal/pkg/controllers/tunnel.go b/internal/pkg/controllers/tunnel.go index 468fe35..cab79fa 100644 --- a/internal/pkg/controllers/tunnel.go +++ b/internal/pkg/controllers/tunnel.go @@ -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 @@ -166,12 +158,18 @@ func (tc *TunnelController) Create(ctx context.Context, params *options.Paramete } } + // deconstruct the values of '-r, --remote' using either : e.g. 127.0.0.1:22 + // or just 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) } @@ -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) @@ -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 } diff --git a/internal/pkg/controllers/tunnel_test.go b/internal/pkg/controllers/tunnel_test.go index ab71e3b..f5763ee 100644 --- a/internal/pkg/controllers/tunnel_test.go +++ b/internal/pkg/controllers/tunnel_test.go @@ -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" @@ -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() diff --git a/internal/pkg/exec/exec.go b/internal/pkg/exec/exec.go index b606907..e56a6e3 100644 --- a/internal/pkg/exec/exec.go +++ b/internal/pkg/exec/exec.go @@ -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 { diff --git a/internal/pkg/exec/exec_nix.go b/internal/pkg/exec/exec_nix.go index bfb3d64..fb39cd9 100644 --- a/internal/pkg/exec/exec_nix.go +++ b/internal/pkg/exec/exec_nix.go @@ -4,7 +4,3 @@ package exec const OpenCmd = "xdg-open" - -func CommandProvider(filePath string) (cmd string, args []string) { - return OpenCmd, []string{filePath} -} diff --git a/internal/pkg/exec/exec_osx.go b/internal/pkg/exec/exec_osx.go index b023884..dbc7d26 100644 --- a/internal/pkg/exec/exec_osx.go +++ b/internal/pkg/exec/exec_osx.go @@ -4,7 +4,3 @@ package exec const OpenCmd = "open" - -func CommandProvider(filePath string) (cmd string, args []string) { - return OpenCmd, []string{filePath} -} diff --git a/internal/pkg/exec/exec_test.go b/internal/pkg/exec/exec_test.go index 968b634..6664fa5 100644 --- a/internal/pkg/exec/exec_test.go +++ b/internal/pkg/exec/exec_test.go @@ -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]) } diff --git a/internal/pkg/exec/exec_win.go b/internal/pkg/exec/exec_win.go index 663db96..3a94e7a 100644 --- a/internal/pkg/exec/exec_win.go +++ b/internal/pkg/exec/exec_win.go @@ -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} -} diff --git a/internal/pkg/launcher/rdp.go b/internal/pkg/launcher/rdp.go index 1f8fb1e..8f6b0f4 100644 --- a/internal/pkg/launcher/rdp.go +++ b/internal/pkg/launcher/rdp.go @@ -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 == "" { @@ -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) } diff --git a/internal/pkg/launcher/uri.go b/internal/pkg/launcher/uri.go index 6b28a81..405fc67 100644 --- a/internal/pkg/launcher/uri.go +++ b/internal/pkg/launcher/uri.go @@ -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" @@ -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) }