MCPcopy Index your code
hub / github.com/coder/coder / portForward

Method portForward

cli/portforward.go:35–219  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

33)
34
35func (r *RootCmd) portForward() *serpent.Command {
36 var (
37 tcpForwards []string // <port>:<port>
38 udpForwards []string // <port>:<port>
39 disableAutostart bool
40 )
41 cmd := &serpent.Command{
42 Use: "port-forward <workspace>",
43 Short: `Forward ports from a workspace to the local machine. For reverse port forwarding, use "coder ssh -R".`,
44 Aliases: []string{"tunnel"},
45 Long: FormatExamples(
46 Example{
47 Description: "Port forward a single TCP port from 1234 in the workspace to port 5678 on your local machine",
48 Command: "coder port-forward <workspace> --tcp 5678:1234",
49 },
50 Example{
51 Description: "Port forward a single UDP port from port 9000 to port 9000 on your local machine",
52 Command: "coder port-forward <workspace> --udp 9000",
53 },
54 Example{
55 Description: "Port forward multiple TCP ports and a UDP port",
56 Command: "coder port-forward <workspace> --tcp 8080:8080 --tcp 9000:3000 --udp 5353:53",
57 },
58 Example{
59 Description: "Port forward multiple ports (TCP or UDP) in condensed syntax",
60 Command: "coder port-forward <workspace> --tcp 8080,9000:3000,9090-9092,10000-10002:10010-10012",
61 },
62 Example{
63 Description: "Port forward specifying the local address to bind to",
64 Command: "coder port-forward <workspace> --tcp 1.2.3.4:8080:8080",
65 },
66 ),
67 Middleware: serpent.Chain(
68 serpent.RequireNArgs(1),
69 ),
70 Handler: func(inv *serpent.Invocation) error {
71 client, err := r.InitClient(inv)
72 if err != nil {
73 return err
74 }
75 ctx, cancel := context.WithCancel(inv.Context())
76 defer cancel()
77 appearanceConfig := initAppearance(ctx, client)
78
79 specs, err := parsePortForwards(tcpForwards, udpForwards)
80 if err != nil {
81 return xerrors.Errorf("parse port-forward specs: %w", err)
82 }
83 if len(specs) == 0 {
84 return xerrors.New("no port-forwards requested")
85 }
86
87 workspace, workspaceAgent, _, err := GetWorkspaceAndAgent(ctx, inv, client, !disableAutostart, inv.Args[0])
88 if err != nil {
89 return err
90 }
91 if workspace.LatestBuild.Transition != codersdk.WorkspaceTransitionStart {
92 return xerrors.New("workspace must be in start transition to port-forward")

Callers 1

CoreSubcommandsMethod · 0.95

Calls 15

InitClientMethod · 0.95
WorkspaceBuildFunction · 0.92
AgentFunction · 0.92
NewFunction · 0.92
FormatExamplesFunction · 0.85
initAppearanceFunction · 0.85
parsePortForwardsFunction · 0.85
GetWorkspaceAndAgentFunction · 0.85
listenAndPortForwardFunction · 0.85
ErrMethod · 0.80

Tested by

no test coverage detected