diff --git a/README.md b/README.md index 882c99a..23f97a1 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 +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 [`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 +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` @@ -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/bash/code-connect.sh deleted file mode 100755 index 723d842..0000000 --- a/bash/code-connect.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/bash - -# 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 $@ diff --git a/bash/uninstall.sh b/bash/uninstall.sh deleted file mode 100755 index 05396a9..0000000 --- a/bash/uninstall.sh +++ /dev/null @@ -1,68 +0,0 @@ -#!/bin/bash - -# https://github.com/chvolkmann/code-connect - -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_fg="$c_cyan" -c_log="$c_grey" -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" -} - - -##### - - -alias-exists () { - name="$1" - cat ~/.bashrc | grep -q "alias $name=*" -} - -remove-alias () { - name="$1" - if alias-exists "$name"; then - log "Removing alias ${c_emph}$name${c_log} from ${c_path}~/.bashrc" - sed -i "/alias $name=/d" ~/.bashrc - 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 -} - - -##### - - -remove-alias "code" -remove-alias "code-connect" - -log "Removing ${c_path}$CODE_CONNECT_INSTALL_DIR" -rm -rf $CODE_CONNECT_INSTALL_DIR - -print "" -print "${c_emph}code-connect${c_fg} uninstalled successfully!" 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/code-connect.sh b/shell/code-connect.sh new file mode 100755 index 0000000..ab291c1 --- /dev/null +++ b/shell/code-connect.sh @@ -0,0 +1,9 @@ +#!/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_dir=$(CDPATH='' cd -- "$(dirname -- "$(dirname -- "$0")")" && pwd) +"$code_connect_dir/bin/code_connect.py" "$@" diff --git a/bash/code.sh b/shell/code.sh similarity index 64% rename from bash/code.sh rename to shell/code.sh index f120b82..80fbe34 100755 --- a/bash/code.sh +++ b/shell/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 diff --git a/bash/install.sh b/shell/install.sh similarity index 57% rename from bash/install.sh rename to shell/install.sh index 4a87e9d..8ab9937 100755 --- a/bash/install.sh +++ b/shell/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,15 @@ 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" "$*" ) +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 ) # Helpers -download-repo-file () { +download_repo_file () { repo_path="$1" output_path="$2" url="$CODE_CONNECT_BASE_URL/$repo_path" @@ -49,7 +42,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}" @@ -58,15 +51,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 @@ -74,14 +67,37 @@ 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 +} + ##### -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 +105,32 @@ 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" +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" -print "" - - -# Add the aliases to ~/.bashrc if not already done -ensure-alias "code" "$CODE_SH" -ensure-alias "code-connect" "$CODE_CONNECT_SH" +printf \\n +ensure_aliases "$CODE_SH" "$CODE_CONNECT_SH" -print "" 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." diff --git a/shell/uninstall.sh b/shell/uninstall.sh new file mode 100755 index 0000000..7f5c86c --- /dev/null +++ b/shell/uninstall.sh @@ -0,0 +1,80 @@ +#!/usr/bin/env sh + +# https://github.com/chvolkmann/code-connect + +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_fg="$c_cyan" +c_log="$c_grey" +c_err="$c_red" +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 ) + + +##### + + +alias_exists () { + name="$1" + grep -q "alias $name=*" ~/.bashrc +} + +remove_alias () { + name="$1" + if alias_exists "$name"; then + log "Removing alias ${c_emph}$name${c_log} from ${c_path}~/.bashrc" + sed -i "/alias $name=/d" ~/.bashrc + 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 +} + +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_aliases + +log "Removing ${c_path}$CODE_CONNECT_INSTALL_DIR" +rm -rf $CODE_CONNECT_INSTALL_DIR + +print "" +print "${c_emph}code-connect${c_fg} uninstalled successfully!"