Last active
May 17, 2025 03:37
-
-
Save cloudhan/4c4a7376972f3a3fc21b7558ef194c24 to your computer and use it in GitHub Desktop.
launch VS Code ssh connect to remote and attach to a container with a path opened
This file contains hidden or 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
| import json | |
| import argparse | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("--target-host", "-t", default=None) | |
| parser.add_argument("container", help="container name or id") | |
| parser.add_argument("path", help="path to open in devcontainer") | |
| args = parser.parse_args() | |
| if args.target_host: | |
| # # NOTE: both are OK | |
| # # https://github.com/microsoft/vscode-remote-release/issues/2133 | |
| # # https://github.com/microsoft/vscode-remote-release/issues/8764#issuecomment-1701046905 | |
| # remote_desc = json.dumps({"containerName": f"/{args.container}"}).encode("utf8").hex() | |
| # remote_uri = f"vscode-remote://attached-container+{remote_desc}@ssh-remote+{args.target_host}{args.path}" | |
| remote_desc = json.dumps({ | |
| "containerName": f"/{args.container}", | |
| "settings": { | |
| "host": f"ssh://{args.target_host}" | |
| } | |
| }).encode("utf8").hex() | |
| remote_uri = f"vscode-remote://attached-container+{remote_desc}{args.path}" | |
| if args.path.endswith(".code-workspace"): | |
| # https://stackoverflow.com/a/78779725/2091555 | |
| print("code --file-uri " + remote_uri) | |
| else: | |
| print("code --folder-uri " + remote_uri) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.