forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add command to open codespaces in JupyterLab
Add command to open codespaces in JupyterLab
- Loading branch information
Showing
7 changed files
with
150 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package codespace | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net" | ||
"strings" | ||
|
||
"github.com/cli/cli/v2/pkg/liveshare" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func newJupyterCmd(app *App) *cobra.Command { | ||
var codespace string | ||
|
||
jupyterCmd := &cobra.Command{ | ||
Use: "jupyter", | ||
Short: "Open a codespace in JupyterLab", | ||
Args: noArgsConstraint, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return app.Jupyter(cmd.Context(), codespace) | ||
}, | ||
} | ||
|
||
jupyterCmd.Flags().StringVarP(&codespace, "codespace", "c", "", "Name of the codespace") | ||
|
||
return jupyterCmd | ||
} | ||
|
||
func (a *App) Jupyter(ctx context.Context, codespaceName string) (err error) { | ||
// Ensure all child tasks (e.g. port forwarding) terminate before return. | ||
ctx, cancel := context.WithCancel(ctx) | ||
defer cancel() | ||
|
||
codespace, err := getOrChooseCodespace(ctx, a.apiClient, codespaceName) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
session, err := startLiveShareSession(ctx, codespace, a, false, "") | ||
if err != nil { | ||
return err | ||
} | ||
defer safeClose(session, &err) | ||
|
||
a.StartProgressIndicatorWithLabel("Starting JupyterLab on codespace") | ||
serverPort, serverUrl, err := session.StartJupyterServer(ctx) | ||
a.StopProgressIndicator() | ||
if err != nil { | ||
return fmt.Errorf("failed to start JupyterLab server: %w", err) | ||
} | ||
|
||
// Pass 0 to pick a random port | ||
listen, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", 0)) | ||
if err != nil { | ||
return err | ||
} | ||
defer listen.Close() | ||
destPort := listen.Addr().(*net.TCPAddr).Port | ||
|
||
tunnelClosed := make(chan error, 1) | ||
go func() { | ||
fwd := liveshare.NewPortForwarder(session, "jupyter", serverPort, true) | ||
tunnelClosed <- fwd.ForwardToListener(ctx, listen) // always non-nil | ||
}() | ||
|
||
// Server URL contains an authentication token that must be preserved | ||
targetUrl := strings.Replace(serverUrl, fmt.Sprintf("%d", serverPort), fmt.Sprintf("%d", destPort), 1) | ||
err = a.browser.Browse(targetUrl) | ||
if err != nil { | ||
return fmt.Errorf("failed to open JupyterLab in browser: %w", err) | ||
} | ||
|
||
fmt.Fprintln(a.io.Out, targetUrl) | ||
|
||
select { | ||
case err := <-tunnelClosed: | ||
return fmt.Errorf("tunnel closed: %w", err) | ||
case <-ctx.Done(): | ||
return nil // success | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters