Skip to content

Commit 990f024

Browse files
committed
Merge branch 'release/v0.3.0' into main
2 parents deda35e + 341180c commit 990f024

File tree

12 files changed

+346
-55
lines changed

12 files changed

+346
-55
lines changed

CHANGELOG.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,33 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

88
## [Unreleased]
9+
910
Maybe your next idea?
11+
12+
## [0.3.0] - 2021-02-18
13+
14+
### Added
15+
16+
- bash uninstaller
17+
18+
### Changed
19+
20+
- `code-connect` is now not just one alias anymore, but two aliases.
21+
22+
- The `code-connect` alias is added functionality of this repo, it points to `code_connect.py`
23+
- The `code` alias checks whether `code` is in the PATH and omits using `code-connect` in this case. This is useful for the integrated terminal as a `code` executable is injected by VS Code into the PATH. Thus, `code` should just run that existing executable, not `code-connect` instead.
24+
25+
See [#8](https://github.com/chvolkmann/code-connect/issues/8)
26+
27+
- bash installer is now fancy
28+
- All bash-related files are now in the `bash/` folder
29+
- All `code_connect.py` is now in the `bin/` folder
30+
- Fisher installation logic ([#10](https://github.com/chvolkmann/code-connect/pull/10))
31+
1032
## [0.2.2] - 2021-02-16
1133

1234
### Added
35+
1336
- Code styling with black, isort and flake8
1437
- Poetry for managing code style dev dependencies
1538
- CI with Github Actions
@@ -54,7 +77,8 @@ Maybe your next idea?
5477

5578
- Initial release of `code-connect` and the corresponding fish plugin
5679

57-
[unreleased]: https://github.com/chvolkmann/code-connect/compare/v0.2.2...HEAD
80+
[unreleased]: https://github.com/chvolkmann/code-connect/compare/v0.3.0...HEAD
81+
[0.3.0]: https://github.com/chvolkmann/code-connect/compare/v0.2.2...v0.3.0
5882
[0.2.2]: https://github.com/chvolkmann/code-connect/compare/v0.2.1...v0.2.2
5983
[0.2.1]: https://github.com/chvolkmann/code-connect/compare/v0.2.0...v0.2.1
6084
[0.2.0]: https://github.com/chvolkmann/code-connect/compare/v0.1.1...v0.2.0

README.md

Lines changed: 64 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,53 +8,84 @@ Open a file in your locally running Visual Studio Code instance from arbitrary t
88

99
VS Code supports opening files with the terminal using `code /path/to/file`. While this is possible in [WSL sessions](https://code.visualstudio.com/docs/remote/wsl) and [remote SSH sessions](https://code.visualstudio.com/docs/remote/ssh) if the integrated terminal is used, it is currently not possible for arbitrary terminal sessions.
1010

11-
Say, you have just SSH'd into a remote server using your favorite terminal and would like to open a webserver config file in your local VS Code instance. So you type `code nginx.conf`, which doesn't work in this terminal. If you try to run `code nginx.conf` in the integrated terminal however, VS Code opens it the file just fine.
11+
Say, you have just SSH'd into a remote server using your favorite terminal and would like to open a webserver config file in your local VS Code instance. So you type `code nginx.conf`, which doesn't work in this terminal. If you try to run `code nginx.conf` in the integrated terminal however, VS Code opens the file just fine.
1212

1313
The aim of this project is to make the `code` cli available to _any_ terminal, not only to VS Code's integrated terminal.
1414

1515
## Prerequisites
1616

17-
- **Linux** - we make assumptions on where VS Code stores it data based on Linux
17+
- **Linux** - we make assumptions on where VS Code stores its data based on Linux
1818

1919
> Macs could also support everything out of the box, confirmation needed. Please don't hesitate to come into contact if you have any information to share.
2020
21-
- **Python 3** - _tested under Python 3.8, but slightly older versions should work fine_
21+
- **Python 3**
22+
> Tested under Python 3.8, but slightly older versions should work fine
2223
- **socat** - used for pinging UNIX sockets
2324
```bash
2425
apt-get install socat
2526
```
2627

27-
### VS Code Server
28+
### Visual Studio Code Server
29+
30+
You need to set up the server component of VS Code on the machine before using this utility. For this, [connect to your target in a remote SSH session](https://code.visualstudio.com/docs/remote/ssh).
2831

29-
You need to set up VS Code Server before using this utility. For this, [connect to your target in a remote SSH session](https://code.visualstudio.com/docs/remote/ssh).
3032
Afterwards, you should have a folder `.vscode-server` in your home directory.
3133

3234
## Installation
35+
3336
### [Fish](https://fishshell.com/)
37+
38+
#### Installing
39+
3440
With [fisher](https://github.com/jorgebucaran/fisher)
41+
3542
```fish
3643
fisher install chvolkmann/code-connect
3744
```
38-
This downloads [`code_connect.py`](./functions/code_connect.py) and sets up an alias for you. See [`functions/code.fish`](./functions/code.fish)
45+
46+
This downloads [`code_connect.py`](./bin/code_connect.py) along with two functions. See [`functions/code.fish`](./functions/code.fish) and [`functions/code-connect.fish`](./functions/code-connect.fish)
47+
48+
You can autocomplete the repository name in subsequent commands, e.g. `fisher update code<TAB>`
49+
50+
#### Updating
51+
52+
```fish
53+
fisher update chvolkmann/code-connect
54+
```
55+
56+
#### Uninstalling
57+
58+
```fish
59+
fisher remove chvolkmann/code-connect
60+
```
61+
3962
### Bash
63+
64+
#### Installing & Updating
65+
66+
With [`bash/install.sh`](./bash/install.sh)
67+
4068
```bash
41-
source <(curl https://raw.githubusercontent.com/chvolkmann/code-connect/main/install.sh)
69+
curl -sS https://raw.githubusercontent.com/chvolkmann/code-connect/main/bash/install.sh | bash
4270
```
4371

44-
This downloads [`code_connect.py`](./functions/code_connect.py) and sets up an alias for you. See [`install.sh`](./install.sh).
45-
46-
To uninstall, delete the alias from you `~/.bashrc` and remove `~/.code-connect`.
72+
This downloads [`code_connect.py`](./bin/code_connect.py) along with two scripts and sets up aliases in your `.bashrc` for you. See [`bash/code.sh`](./bash/code.sh) and [`bash/code-connect.sh`](./bash/code-connect.sh)
4773

48-
### Manually
74+
#### Uninstalling
4975

50-
Set up an alias for `code`, pointing to [`code_connect.py`](./functions/code_connect.py) by placing the following line in your shell's rcfile (bash: `~/.bashrc`, fish: `~/.config/fish/fuctions/code.fish`).
76+
With [`bash/uninstall.sh`](./bash/uninstall.sh)
5177

5278
```bash
53-
alias code="/path/to/code_connect.py"
79+
curl -sS https://raw.githubusercontent.com/chvolkmann/code-connect/main/bash/uninstall.sh | bash
5480
```
5581

82+
Deletes the aliases from `~/.bashrc` and removes the folder `~/.code-connect`
83+
5684
## Usage
57-
Just use `code` like you normally would!
85+
86+
Use `code` as you would normally!
87+
88+
If you have VS Code installed on your remote machine as well (i.e. a `code` executable already exists), you can use `code` for your local instance and `code-connect` for a IPC connected instance.
5889

5990
```
6091
Usage: code [options][paths...]
@@ -81,17 +112,20 @@ See [CHANGELOG.md](./CHANGELOG.md)
81112

82113
## How it works
83114

115+
### VS Code Remote under the hood
116+
84117
VS Code uses datagram sockets to communicate between a terminal and the rendering window.
85118

86-
The integrated terminal as well as the WSL terminal spawn an IPC socket. You also create one when manually attaching a remote SSH session. These sockets can be found in the folder VS Code Server.
119+
The integrated terminal as well as the WSL terminal spawn an IPC socket. You also create one when connecting through a remote SSH session. These sockets can be found in the folders of VS Code Server.
87120

88-
Each time you connect remotely, the VS Code client instructs the server to fetch the newest version of itself. All versions are stored by commit id in `~/.vscode-server/bin`. `code-connect` uses the version that has been most recently accessed. The corresponding binary can be found in `~/.vscode-server/bin/<commid-id>/bin/code`.
121+
Each time you connect remotely, the VS Code client instructs the server to fetch the newest version of itself. All versions are stored by commit id in `~/.vscode-server/bin`. `code-connect` uses the version that has been most recently accessed. The corresponding `code` executable can be found in `~/.vscode-server/bin/<commid-id>/bin/code`.
89122

90123
A similar method is used to list all of VS Code's IPC sockets, which are located under `/run/user/<userid>/vscode-ipc-<UUID>.sock`, where `<userid>` is the [current user's UID](https://en.wikipedia.org/wiki/User_identifier) and `<UUID>` is a unique ID. VS Code does not seem to clean up all stale connections, so some of these sockets are active, some are not.
91124

92-
So the socket that is listening and that was accessed within a timeframe of 4 hours by default is chosen.
125+
Thus the first socket that is listening and that was accessed within a timeframe of 4 hours by default is chosen.
126+
127+
VS Code communicates the presence of an active IPC connection with the environment variable `VSCODE_IPC_HOOK_CLI` which stores the path to the socket.
93128

94-
VS Code communicates the presence of an active IPC connection with the environment variable `VSCODE_IPC_HOOK_CLI` which stores the path to the socket.
95129
You can verify this by opening a connection to your remote machine. In one case, you use VS Code's integrated terminal. In the other case, you use any other terminal.
96130

97131
Run
@@ -110,15 +144,21 @@ socat -u OPEN:/dev/null UNIX-CONNECT:/path/to/socket
110144

111145
This returns `0` if and only if there's something listening.
112146

147+
### `code-connect` under the hood
148+
113149
The script `code_connect.py` performs all of the above steps and runs the VS Code `code` executable
114-
as a child process with `VSCODE_IPC_HOOK_CLI` set properly.
150+
as a child process with `VSCODE_IPC_HOOK_CLI` set properly, making it a drop-in replacement for `code`.
151+
152+
When we already have a `code` executable available, we don't need to search for it ourselves using `code_connect.py`. So we introduce two more scripts:
153+
154+
- `code-connect`
155+
Direct alias to `code_connect.py`
156+
- `code`
157+
Checks whether there is a `code` executable locally installed already and tries to use it if available. Otherwise, `code-connect` will be used.
115158

116-
## [Contributing](./CONTRIBUTING.md)
159+
## Contributing
117160

118-
- Fork the repo
119-
- Commit your changes to your branch
120-
- Create a pull request
121-
_Please make sure that [edits to your pull request are permitted](https://docs.github.com/en/github/collaborating-with-issues-and-pull-requests/allowing-changes-to-a-pull-request-branch-created-from-a-fork)._
161+
See [CONTRIBUTING.md](./CONTRIBUTING.md)
122162

123163
## Credit
124164

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.2.2
1+
0.3.0

bash/code-connect.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
3+
# https://github.com/chvolkmann/code-connect
4+
5+
# Use this script through an alias
6+
# alias code-connect="/path/to/code-connect.sh"
7+
8+
~/.code-connect/bin/code_connect.py $@

bash/code.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/bash
2+
3+
# https://github.com/chvolkmann/code-connect
4+
5+
# Use this script through an alias
6+
# alias code="/path/to/code.sh"
7+
8+
local_code_executable="$(which code)"
9+
if test -n "$local_code_executable"; then
10+
# code is in the PATH, use that binary instead of the code-connect
11+
$local_code_executable $@
12+
else
13+
# code not locally installed, use code-connect
14+
~/.code-connect/bin/code_connect.py $@
15+
fi

bash/install.sh

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
#!/bin/bash
2+
3+
# https://github.com/chvolkmann/code-connect
4+
5+
# Configure this variable to change the install location of code-connect
6+
CODE_CONNECT_INSTALL_DIR=~/.code-connect
7+
8+
CODE_CONNECT_BASE_URL="https://raw.githubusercontent.com/chvolkmann/code-connect/main"
9+
10+
11+
####
12+
13+
# Fancy output helpers
14+
15+
c_cyan=`tput setaf 7`
16+
c_red=`tput setaf 1`
17+
c_magenta=`tput setaf 6`
18+
c_grey=`tput setaf 8`
19+
c_green=`tput setaf 10`
20+
c_reset=`tput sgr0`
21+
22+
c_fg="$c_cyan"
23+
c_log="$c_grey"
24+
c_err="$c_red"
25+
c_emph="$c_magenta"
26+
c_path="$c_green"
27+
28+
print () {
29+
echo "$c_fg$@$c_reset"
30+
}
31+
32+
log () {
33+
echo "$c_log$@$c_reset"
34+
}
35+
36+
error () {
37+
echo "$c_err$@$c_reset"
38+
}
39+
40+
41+
# Helpers
42+
43+
download-repo-file () {
44+
repo_path="$1"
45+
output_path="$2"
46+
url="$CODE_CONNECT_BASE_URL/$repo_path"
47+
48+
if test "$output_path" != "-"; then
49+
log "Downloading ${c_path}$repo_path${c_log} from ${c_path}$url"
50+
fi
51+
52+
curl -sS -o "$output_path" "$url"
53+
ret="$?"
54+
if test "$ret" != "0"; then
55+
error "ERROR: Could not fetch ${c_path}$url${c_err}"
56+
error "${c_emph}curl${c_err} exited with status code ${c_emph}$ret"
57+
exit $ret
58+
fi
59+
}
60+
61+
alias-exists () {
62+
name="$1"
63+
cat ~/.bashrc | grep -q "alias $name=*"
64+
}
65+
66+
ensure-alias () {
67+
name="$1"
68+
val="$2"
69+
if alias-exists "$name"; then
70+
log "Alias ${c_emph}$name${c_log} already registered in ${c_path}~/.bashrc${c_log}, skipping"
71+
else
72+
echo "alias $name='$val'" >> ~/.bashrc
73+
log "Adding alias ${c_emph}$name${c_log} to ${c_path}~/.bashrc"
74+
fi
75+
}
76+
77+
78+
#####
79+
80+
81+
version=$(download-repo-file "VERSION" -)
82+
print ""
83+
print "${c_emph}code-connect ${c_log}v$version"
84+
print ""
85+
86+
87+
# Download the required files from the repository
88+
89+
mkdir -p "$CODE_CONNECT_INSTALL_DIR/bin"
90+
91+
CODE_CONNECT_PY="$CODE_CONNECT_INSTALL_DIR/bin/code_connect.py"
92+
download-repo-file "functions/code_connect.py" $CODE_CONNECT_PY
93+
chmod +x "$CODE_CONNECT_PY"
94+
95+
mkdir -p "$CODE_CONNECT_INSTALL_DIR/bin"
96+
97+
CODE_SH="$CODE_CONNECT_INSTALL_DIR/bin/code.sh"
98+
download-repo-file "bash/code.sh" $CODE_SH
99+
chmod +x "$CODE_SH"
100+
101+
CODE_CONNECT_SH="$CODE_CONNECT_INSTALL_DIR/bin/code-connect.sh"
102+
download-repo-file "bash/code-connect.sh" $CODE_CONNECT_SH
103+
chmod +x "$CODE_CONNECT_SH"
104+
105+
print ""
106+
107+
108+
# Add the aliases to ~/.bashrc if not already done
109+
ensure-alias "code" "$CODE_SH"
110+
ensure-alias "code-connect" "$CODE_CONNECT_SH"
111+
112+
113+
print ""
114+
print "${c_emph}code-connect${c_fg} installed to ${c_path}$CODE_CONNECT_INSTALL_DIR${c_fg} successfully!"
115+
print ""
116+
print "Restart your shell or reload your ${c_path}.bashrc${c_fg} to see the changes."
117+
print ""
118+
print " ${c_emph}source ${c_path}.bashrc"
119+
print ""
120+
121+
122+
local_code_binary=$(which code)
123+
if test -z "$local_code_binary"; then
124+
print "Local installation of ${c_emph}code${c_fg} detected at ${c_path}$local_code_binary"
125+
print "Use the ${c_emph}code${c_fg} executable as you would normally."
126+
print "If you want to connect to a remote VS Code session, use ${c_emph}code-connect${c_fg} as a drop-in replacement for ${c_emph}code${c_fg}!"
127+
else
128+
print "Use the ${c_emph}code${c_fg} executable as you would normally and you will interface with an open VS Code remote session, if available."
129+
print "If you want to ${c_err}explicitly${c_fg} connect to a remote VS Code session, use ${c_emph}code-connect${c_fg} as a drop-in replacement for ${c_emph}code${c_fg}!"
130+
fi

0 commit comments

Comments
 (0)