From 922fd2358db5d9341a5b3819bfec4caf1ce3d0be Mon Sep 17 00:00:00 2001 From: ELLIOTTCABLE Date: Fri, 17 Jan 2025 16:46:01 -0600 Subject: [PATCH 1/7] (- fix sh) Fix quoting in scripts; mark as POSIX-sh --- bash/code-connect.sh | 5 +++-- bash/code.sh | 7 ++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/bash/code-connect.sh b/bash/code-connect.sh index 723d842..ab291c1 100755 --- a/bash/code-connect.sh +++ b/bash/code-connect.sh @@ -1,8 +1,9 @@ -#!/bin/bash +#!/usr/bin/env sh # https://github.com/chvolkmann/code-connect # Use this script through an alias # alias code-connect="/path/to/code-connect.sh" -~/.code-connect/bin/code_connect.py $@ +code_connect_dir=$(CDPATH='' cd -- "$(dirname -- "$(dirname -- "$0")")" && pwd) +"$code_connect_dir/bin/code_connect.py" "$@" diff --git a/bash/code.sh b/bash/code.sh index f120b82..80fbe34 100755 --- a/bash/code.sh +++ b/bash/code.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env sh # https://github.com/chvolkmann/code-connect @@ -8,8 +8,9 @@ local_code_executable="$(which code 2>/dev/null)" if test -n "$local_code_executable"; then # code is in the PATH, use that binary instead of the code-connect - $local_code_executable $@ + "$local_code_executable" "$@" else # code not locally installed, use code-connect - ~/.code-connect/bin/code_connect.py $@ + code_connect_dir=$(CDPATH='' cd -- "$(dirname -- "$(dirname -- "$0")")" && pwd) + "$code_connect_dir/bin/code_connect.py" "$@" fi From 61916c999593e8312472dac3ff1a1df3bbdc48a8 Mon Sep 17 00:00:00 2001 From: ELLIOTTCABLE Date: Fri, 17 Jan 2025 17:07:32 -0600 Subject: [PATCH 2/7] (- fix sh) Clean install.sh to be POSIX-compliant --- bash/install.sh | 64 ++++++++++++++++++++++--------------------------- 1 file changed, 29 insertions(+), 35 deletions(-) diff --git a/bash/install.sh b/bash/install.sh index 4a87e9d..5a507e7 100755 --- a/bash/install.sh +++ b/bash/install.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env sh # https://github.com/chvolkmann/code-connect @@ -12,12 +12,12 @@ CODE_CONNECT_BASE_URL="https://raw.githubusercontent.com/chvolkmann/code-connect # Fancy output helpers -c_cyan=`tput setaf 7` -c_red=`tput setaf 1` -c_magenta=`tput setaf 6` -c_grey=`tput setaf 8` -c_green=`tput setaf 10` -c_reset=`tput sgr0` +c_cyan="$(tput setaf 7)" +c_red="$(tput setaf 1)" +c_magenta="$(tput setaf 6)" +c_grey="$(tput setaf 8)" +c_green="$(tput setaf 10)" +c_reset="$(tput sgr0)" c_fg="$c_cyan" c_log="$c_grey" @@ -25,22 +25,16 @@ c_err="$c_red" c_emph="$c_magenta" c_path="$c_green" -print () { - echo "$c_fg$@$c_reset" -} +print() ( IFS=" " printf "$c_fg%s$c_reset\n" "$*" ) -log () { - echo "$c_log$@$c_reset" -} +log() ( IFS=" " printf "$c_log%s$c_reset\n" "$*" ) -error () { - echo "$c_err$@$c_reset" -} +error() ( IFS=" " printf "$c_err%s$c_reset\n" "$*" >&2 ) # Helpers -download-repo-file () { +download_repo_file () { repo_path="$1" output_path="$2" url="$CODE_CONNECT_BASE_URL/$repo_path" @@ -58,15 +52,15 @@ download-repo-file () { fi } -alias-exists () { +alias_exists () { name="$1" - cat ~/.bashrc | grep -q "alias $name=*" + grep -q "alias $name=*" ~/.bashrc } -ensure-alias () { +ensure_alias () { name="$1" val="$2" - if alias-exists "$name"; then + if alias_exists "$name"; then log "Alias ${c_emph}$name${c_log} already registered in ${c_path}~/.bashrc${c_log}, skipping" else echo "alias $name='$val'" >> ~/.bashrc @@ -78,10 +72,10 @@ ensure-alias () { ##### -version=$(download-repo-file "VERSION" -) -print "" +version=$(download_repo_file "VERSION" -) +printf \\n print "${c_emph}code-connect ${c_log}v$version" -print "" +printf \\n # Download the required files from the repository @@ -89,37 +83,37 @@ print "" mkdir -p "$CODE_CONNECT_INSTALL_DIR/bin" CODE_CONNECT_PY="$CODE_CONNECT_INSTALL_DIR/bin/code_connect.py" -download-repo-file "bin/code_connect.py" $CODE_CONNECT_PY +download_repo_file "bin/code_connect.py" $CODE_CONNECT_PY chmod +x "$CODE_CONNECT_PY" mkdir -p "$CODE_CONNECT_INSTALL_DIR/bash" CODE_SH="$CODE_CONNECT_INSTALL_DIR/bash/code.sh" -download-repo-file "bash/code.sh" $CODE_SH +download_repo_file "bash/code.sh" $CODE_SH chmod +x "$CODE_SH" CODE_CONNECT_SH="$CODE_CONNECT_INSTALL_DIR/bash/code-connect.sh" -download-repo-file "bash/code-connect.sh" $CODE_CONNECT_SH +download_repo_file "bash/code-connect.sh" $CODE_CONNECT_SH chmod +x "$CODE_CONNECT_SH" -print "" +printf \\n # Add the aliases to ~/.bashrc if not already done -ensure-alias "code" "$CODE_SH" -ensure-alias "code-connect" "$CODE_CONNECT_SH" +ensure_alias "code" "$CODE_SH" +ensure_alias "code-connect" "$CODE_CONNECT_SH" -print "" +printf \\n print "${c_emph}code-connect${c_fg} installed to ${c_path}$CODE_CONNECT_INSTALL_DIR${c_fg} successfully!" -print "" +printf \\n print "Restart your shell or reload your ${c_path}.bashrc${c_fg} to see the changes." -print "" +printf \\n print " ${c_emph}source ${c_path}.bashrc" -print "" +printf \\n -local_code_binary=$(which code) +local_code_binary="$(which code)" if test -z "$local_code_binary"; then print "Local installation of ${c_emph}code${c_fg} detected at ${c_path}$local_code_binary" print "Use the ${c_emph}code${c_fg} executable as you would normally." From 0e9c46697c245edf48bd9976c10a02fc194981b7 Mon Sep 17 00:00:00 2001 From: ELLIOTTCABLE Date: Fri, 17 Jan 2025 17:26:34 -0600 Subject: [PATCH 3/7] (re sh) Move bash/ to shell/ --- README.md | 22 +++++++++++----------- {bash => shell}/code-connect.sh | 0 {bash => shell}/code.sh | 0 {bash => shell}/install.sh | 0 {bash => shell}/uninstall.sh | 0 5 files changed, 11 insertions(+), 11 deletions(-) rename {bash => shell}/code-connect.sh (100%) rename {bash => shell}/code.sh (100%) rename {bash => shell}/install.sh (100%) rename {bash => shell}/uninstall.sh (100%) diff --git a/README.md b/README.md index 882c99a..5e725df 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ The aim of this project is to make the `code` cli available to _any_ terminal, n - **Python 3** > Tested under Python 3.6 and Python 3.8, but should work fine in Python 3.5 or newer. - **socat** - used for pinging UNIX sockets - ```bash + ```sh apt-get install socat ``` @@ -59,24 +59,24 @@ fisher update chvolkmann/code-connect fisher remove chvolkmann/code-connect ``` -### Bash +### POSIX-compliant shells (bash, dash, etc.) #### Installing & Updating -With [`bash/install.sh`](./bash/install.sh) +With [`shell/install.sh`](./shell/install.sh) -```bash -curl -sS https://raw.githubusercontent.com/chvolkmann/code-connect/main/bash/install.sh | bash +```sh +curl -sS https://raw.githubusercontent.com/chvolkmann/code-connect/main/shell/install.sh | sh ``` -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) +This downloads [`code_connect.py`](./bin/code_connect.py) along with two scripts and sets up aliases in your `.bashrc` for you. See [`shell/code.sh`](./shell/code.sh) and [`shell/code-connect.sh`](./shell/code-connect.sh) #### Uninstalling -With [`bash/uninstall.sh`](./bash/uninstall.sh) +With [`shell/uninstall.sh`](./shell/uninstall.sh) -```bash -curl -sS https://raw.githubusercontent.com/chvolkmann/code-connect/main/bash/uninstall.sh | bash +```sh +curl -sS https://raw.githubusercontent.com/chvolkmann/code-connect/main/shell/uninstall.sh | sh ``` Deletes the aliases from `~/.bashrc` and removes the folder `~/.code-connect` @@ -130,7 +130,7 @@ You can verify this by opening a connection to your remote machine. In one case, Run -```bash +```sh echo $VSCODE_IPC_HOOK_CLI ``` @@ -138,7 +138,7 @@ which displays an output in the integrated terminal, but not on the other one. In order, every socket is checked to see if it is listening. For this, the following snippet based on [this answer on StackOverflow](https://unix.stackexchange.com/a/556790) was used. -```bash +```sh socat -u OPEN:/dev/null UNIX-CONNECT:/path/to/socket ``` diff --git a/bash/code-connect.sh b/shell/code-connect.sh similarity index 100% rename from bash/code-connect.sh rename to shell/code-connect.sh diff --git a/bash/code.sh b/shell/code.sh similarity index 100% rename from bash/code.sh rename to shell/code.sh diff --git a/bash/install.sh b/shell/install.sh similarity index 100% rename from bash/install.sh rename to shell/install.sh diff --git a/bash/uninstall.sh b/shell/uninstall.sh similarity index 100% rename from bash/uninstall.sh rename to shell/uninstall.sh From 097c06360b9f652aaacdebfe477c763343f3784b Mon Sep 17 00:00:00 2001 From: ELLIOTTCABLE Date: Fri, 17 Jan 2025 17:29:37 -0600 Subject: [PATCH 4/7] (re sh) Handle redirects gracefully; safer sh -c --- README.md | 4 ++-- conf.d/code-connect.fish | 2 +- shell/install.sh | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 5e725df..23f97a1 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,7 @@ fisher remove chvolkmann/code-connect With [`shell/install.sh`](./shell/install.sh) ```sh -curl -sS https://raw.githubusercontent.com/chvolkmann/code-connect/main/shell/install.sh | sh +sh -c "$(curl -sSLo- https://raw.githubusercontent.com/chvolkmann/code-connect/main/shell/install.sh)" ``` This downloads [`code_connect.py`](./bin/code_connect.py) along with two scripts and sets up aliases in your `.bashrc` for you. See [`shell/code.sh`](./shell/code.sh) and [`shell/code-connect.sh`](./shell/code-connect.sh) @@ -76,7 +76,7 @@ This downloads [`code_connect.py`](./bin/code_connect.py) along with two scripts With [`shell/uninstall.sh`](./shell/uninstall.sh) ```sh -curl -sS https://raw.githubusercontent.com/chvolkmann/code-connect/main/shell/uninstall.sh | sh +sh -c "$(curl -sSLo- https://raw.githubusercontent.com/chvolkmann/code-connect/main/shell/uninstall.sh)" ``` Deletes the aliases from `~/.bashrc` and removes the folder `~/.code-connect` diff --git a/conf.d/code-connect.fish b/conf.d/code-connect.fish index 114ecdd..1b749d5 100644 --- a/conf.d/code-connect.fish +++ b/conf.d/code-connect.fish @@ -4,7 +4,7 @@ function _on_code-connect_install --on-event code-connect_install mkdir -p ~/.code-connect/bin - curl -sS "https://raw.githubusercontent.com/chvolkmann/code-connect/main/bin/code_connect.py" >~/.code-connect/bin/code_connect.py + curl -sSL "https://raw.githubusercontent.com/chvolkmann/code-connect/main/bin/code_connect.py" >~/.code-connect/bin/code_connect.py chmod +x ~/.code-connect/bin/code_connect.py end diff --git a/shell/install.sh b/shell/install.sh index 5a507e7..cee4c94 100755 --- a/shell/install.sh +++ b/shell/install.sh @@ -43,7 +43,7 @@ download_repo_file () { log "Downloading ${c_path}$repo_path${c_log} from ${c_path}$url" fi - curl -sS -o "$output_path" "$url" + curl -sSL -o "$output_path" "$url" ret="$?" if test "$ret" != "0"; then error "ERROR: Could not fetch ${c_path}$url${c_err}" From 0b86456288c6a4b1f812471128162335a648a710 Mon Sep 17 00:00:00 2001 From: ELLIOTTCABLE Date: Fri, 17 Jan 2025 17:44:22 -0600 Subject: [PATCH 5/7] (- fix sh) Fix uninstall.sh as well --- shell/install.sh | 2 -- shell/uninstall.sh | 41 ++++++++++++++++------------------------- 2 files changed, 16 insertions(+), 27 deletions(-) diff --git a/shell/install.sh b/shell/install.sh index cee4c94..5ebf28a 100755 --- a/shell/install.sh +++ b/shell/install.sh @@ -26,9 +26,7 @@ c_emph="$c_magenta" c_path="$c_green" print() ( IFS=" " printf "$c_fg%s$c_reset\n" "$*" ) - log() ( IFS=" " printf "$c_log%s$c_reset\n" "$*" ) - error() ( IFS=" " printf "$c_err%s$c_reset\n" "$*" >&2 ) diff --git a/shell/uninstall.sh b/shell/uninstall.sh index 05396a9..36c9556 100755 --- a/shell/uninstall.sh +++ b/shell/uninstall.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env sh # https://github.com/chvolkmann/code-connect @@ -9,12 +9,12 @@ CODE_CONNECT_INSTALL_DIR=~/.code-connect # Fancy output helpers -c_cyan=`tput setaf 7` -c_red=`tput setaf 1` -c_magenta=`tput setaf 6` -c_grey=`tput setaf 8` -c_green=`tput setaf 10` -c_reset=`tput sgr0` +c_cyan="$(tput setaf 7)" +c_red="$(tput setaf 1)" +c_magenta="$(tput setaf 6)" +c_grey="$(tput setaf 8)" +c_green="$(tput setaf 10)" +c_reset="$(tput sgr0)" c_fg="$c_cyan" c_log="$c_grey" @@ -22,28 +22,20 @@ c_err="$c_red" c_emph="$c_magenta" c_path="$c_green" -print () { - echo "$c_fg$@$c_reset" -} - -log () { - echo "$c_log$@$c_reset" -} - -error () { - echo "$c_err$@$c_reset" -} +print() ( IFS=" " printf "$c_fg%s$c_reset\n" "$*" ) +log() ( IFS=" " printf "$c_log%s$c_reset\n" "$*" ) +error() ( IFS=" " printf "$c_err%s$c_reset\n" "$*" >&2 ) ##### -alias-exists () { +alias_exists () { name="$1" - cat ~/.bashrc | grep -q "alias $name=*" + grep -q "alias $name=*" ~/.bashrc } -remove-alias () { +remove_alias () { name="$1" if alias-exists "$name"; then log "Removing alias ${c_emph}$name${c_log} from ${c_path}~/.bashrc" @@ -51,15 +43,14 @@ remove-alias () { else log "Alias for ${c_emph}$name${c_log} not registered in ${c_path}~/.bashrc${c_log}, skipping" fi - unalias $name > /dev/null 2>&1 + unalias "$name" > /dev/null 2>&1 } ##### - -remove-alias "code" -remove-alias "code-connect" +remove_alias "code" +remove_alias "code-connect" log "Removing ${c_path}$CODE_CONNECT_INSTALL_DIR" rm -rf $CODE_CONNECT_INSTALL_DIR From 668eb2b08df178494cb65ae12d30b4b1c3147745 Mon Sep 17 00:00:00 2001 From: ELLIOTTCABLE Date: Fri, 17 Jan 2025 17:53:25 -0600 Subject: [PATCH 6/7] (new sh) Request user-permission to modify bashrc --- shell/install.sh | 31 +++++++++++++++++++++++++------ shell/uninstall.sh | 27 ++++++++++++++++++++++++--- 2 files changed, 49 insertions(+), 9 deletions(-) diff --git a/shell/install.sh b/shell/install.sh index 5ebf28a..da88341 100755 --- a/shell/install.sh +++ b/shell/install.sh @@ -26,6 +26,7 @@ c_emph="$c_magenta" c_path="$c_green" print() ( IFS=" " printf "$c_fg%s$c_reset\n" "$*" ) +print_n() ( IFS=" " printf "$c_fg%s$c_reset" "$*" ) log() ( IFS=" " printf "$c_log%s$c_reset\n" "$*" ) error() ( IFS=" " printf "$c_err%s$c_reset\n" "$*" >&2 ) @@ -66,6 +67,29 @@ ensure_alias () { fi } +ensure_aliases () { + code_sh_path="$1" + code_connect_sh_path="$2" + print_n "May I modify your ${c_path}~/.bashrc${c_fg}? [yN] " + read -r yn + + case $yn in + [Yy]*) + # Add the aliases to ~/.bashrc if not already done + ensure_alias "code" "$code_sh_path" + ensure_alias "code-connect" "$code_connect_sh_path" + + ;; + *) + print "Okay; make sure to add the following to your shell-profile manually:" + print "alias code='$code_sh_path'" + print "alias code-connect='$code_connect_sh_path'" + ;; + esac + + printf \\n +} + ##### @@ -96,13 +120,8 @@ chmod +x "$CODE_CONNECT_SH" printf \\n +ensure_aliases "$CODE_SH" "$CODE_CONNECT_SH" -# Add the aliases to ~/.bashrc if not already done -ensure_alias "code" "$CODE_SH" -ensure_alias "code-connect" "$CODE_CONNECT_SH" - - -printf \\n print "${c_emph}code-connect${c_fg} installed to ${c_path}$CODE_CONNECT_INSTALL_DIR${c_fg} successfully!" printf \\n print "Restart your shell or reload your ${c_path}.bashrc${c_fg} to see the changes." diff --git a/shell/uninstall.sh b/shell/uninstall.sh index 36c9556..7f5c86c 100755 --- a/shell/uninstall.sh +++ b/shell/uninstall.sh @@ -23,6 +23,7 @@ c_emph="$c_magenta" c_path="$c_green" print() ( IFS=" " printf "$c_fg%s$c_reset\n" "$*" ) +print_n() ( IFS=" " printf "$c_fg%s$c_reset" "$*" ) log() ( IFS=" " printf "$c_log%s$c_reset\n" "$*" ) error() ( IFS=" " printf "$c_err%s$c_reset\n" "$*" >&2 ) @@ -37,7 +38,7 @@ alias_exists () { remove_alias () { name="$1" - if alias-exists "$name"; then + if alias_exists "$name"; then log "Removing alias ${c_emph}$name${c_log} from ${c_path}~/.bashrc" sed -i "/alias $name=/d" ~/.bashrc else @@ -46,11 +47,31 @@ remove_alias () { unalias "$name" > /dev/null 2>&1 } +remove_aliases () { + print_n "May I modify your ${c_path}~/.bashrc${c_fg}? [yN] " + read -r yn + + case $yn in + [Yy]*) + # Add the aliases to ~/.bashrc if not already done + remove_alias "code" + remove_alias "code-connect" + + ;; + *) + print "Okay; make sure to remove the following from your shell-profile manually:" + print "alias code='...'" + print "alias code-connect='...'" + ;; + esac + + printf \\n +} + ##### -remove_alias "code" -remove_alias "code-connect" +remove_aliases log "Removing ${c_path}$CODE_CONNECT_INSTALL_DIR" rm -rf $CODE_CONNECT_INSTALL_DIR From 49a0a7736d997d0da0da2df9f13b8be17d4f527c Mon Sep 17 00:00:00 2001 From: ELLIOTTCABLE Date: Fri, 17 Jan 2025 18:05:09 -0600 Subject: [PATCH 7/7] (- fix install) Fix dirnames one more place --- shell/install.sh | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/shell/install.sh b/shell/install.sh index da88341..8ab9937 100755 --- a/shell/install.sh +++ b/shell/install.sh @@ -108,14 +108,14 @@ CODE_CONNECT_PY="$CODE_CONNECT_INSTALL_DIR/bin/code_connect.py" download_repo_file "bin/code_connect.py" $CODE_CONNECT_PY chmod +x "$CODE_CONNECT_PY" -mkdir -p "$CODE_CONNECT_INSTALL_DIR/bash" +mkdir -p "$CODE_CONNECT_INSTALL_DIR/shell" -CODE_SH="$CODE_CONNECT_INSTALL_DIR/bash/code.sh" -download_repo_file "bash/code.sh" $CODE_SH +CODE_SH="$CODE_CONNECT_INSTALL_DIR/shell/code.sh" +download_repo_file "shell/code.sh" $CODE_SH chmod +x "$CODE_SH" -CODE_CONNECT_SH="$CODE_CONNECT_INSTALL_DIR/bash/code-connect.sh" -download_repo_file "bash/code-connect.sh" $CODE_CONNECT_SH +CODE_CONNECT_SH="$CODE_CONNECT_INSTALL_DIR/shell/code-connect.sh" +download_repo_file "shell/code-connect.sh" $CODE_CONNECT_SH chmod +x "$CODE_CONNECT_SH" printf \\n