Skip to content

Commit

Permalink
Fix import cycle
Browse files Browse the repository at this point in the history
  • Loading branch information
cmoog committed May 22, 2020
1 parent 0454d16 commit b61fa9c
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 8 deletions.
14 changes: 12 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,19 @@ func RemoteExecer(conn *websocket.Conn) Execer {
return remoteExec{conn: conn}
}

func (r remoteExec) Start(ctx context.Context, c proto.Command) (Process, error) {
type Command struct {
Command string
Args []string
TTY bool
UID uint32
GID uint32
Env []string
WorkingDir string
}

func (r remoteExec) Start(ctx context.Context, c Command) (Process, error) {
header := proto.ClientStartHeader{
Command: c,
Command: mapToProtoCmd(c),
Type: proto.TypeStart,
}
payload, err := json.Marshal(header)
Expand Down
3 changes: 1 addition & 2 deletions dev/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"os"

"cdr.dev/wsep"
"cdr.dev/wsep/internal/proto"
"github.com/spf13/pflag"
"go.coder.com/cli"
"go.coder.com/flog"
Expand Down Expand Up @@ -64,7 +63,7 @@ func do(fl *pflag.FlagSet, tty bool) {
if len(fl.Args()) > 1 {
args = fl.Args()[1:]
}
process, err := executor.Start(ctx, proto.Command{
process, err := executor.Start(ctx, wsep.Command{
Command: fl.Arg(0),
Args: args,
TTY: tty,
Expand Down
27 changes: 26 additions & 1 deletion exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,30 @@ type Process interface {

// Execer starts commands.
type Execer interface {
Start(ctx context.Context, c proto.Command) (Process, error)
Start(ctx context.Context, c Command) (Process, error)
}

// theses maps are needed to prevent an import cycle
func mapToProtoCmd(c Command) proto.Command {
return proto.Command{
Command: c.Command,
Args: c.Args,
TTY: c.TTY,
UID: c.UID,
GID: c.GID,
Env: c.Env,
WorkingDir: c.WorkingDir,
}
}

func mapToClientCmd(c proto.Command) Command {
return Command{
Command: c.Command,
Args: c.Args,
TTY: c.TTY,
UID: c.UID,
GID: c.GID,
Env: c.Env,
WorkingDir: c.WorkingDir,
}
}
3 changes: 1 addition & 2 deletions localexec.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"os/exec"
"syscall"

"cdr.dev/wsep/internal/proto"
"github.com/creack/pty"
"golang.org/x/xerrors"
)
Expand Down Expand Up @@ -66,7 +65,7 @@ func (l *localProcess) Pid() int {
return l.cmd.Process.Pid
}

func (l LocalExecer) Start(ctx context.Context, c proto.Command) (Process, error) {
func (l LocalExecer) Start(ctx context.Context, c Command) (Process, error) {
var (
process localProcess
err error
Expand Down
2 changes: 1 addition & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func Serve(ctx context.Context, c *websocket.Conn, execer Execer) error {
if err != nil {
return xerrors.Errorf("unmarshal start header: %w", err)
}
process, err = execer.Start(ctx, header.Command)
process, err = execer.Start(ctx, mapToClientCmd(header.Command))
if err != nil {
return err
}
Expand Down

0 comments on commit b61fa9c

Please sign in to comment.