Skip to content

Commit

Permalink
rm os_any.bash
Browse files Browse the repository at this point in the history
  • Loading branch information
alanlivio committed Nov 17, 2024
1 parent 5dc04bf commit 3831d0e
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 107 deletions.
34 changes: 24 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,44 @@
# ps-sh-helpers

Lib template for creating powershell and bash helpers. It let you organize helpers in `OS-dependent` or `program-dependent` and load them from powershell or bash.

The diagram below illustrates how ps-sh-helpers loads `OS-dependent` from `os_*.bash` (files after testing `$OSTYPE`) and loads `program-dependent` from `programs/<program>.bash` (after testing `type <program>`).
Lib template for creating powershell and bash helpers. It let you organize helpers in `OS-dependent` or `program-dependent` and load them from powershell or bash. It loads `OS-dependent` from `os_*` and loads `program-dependent` from `programs/<program>.*`.

```mermaid
%%{init: {'theme':'dark'}}%%
flowchart LR
bashrc["~/.bashrc"]
init["bash-hepers/init.sh"]
anyos["os_any.bash"]
oswinps1["init.ps1"]
init["ps-sh-hepers/init.sh"]
oswinps1["os_win.ps1"]
program-dependent["
programs/[program].bash
...
"]
win["os_win.bash"]
ubu["os_ub.bash"]
ubu["os_ubu.bash"]
bashrc --> |"load"| init
init --> |"load"| anyos
anyos --> |"load if type program"| program-dependent
init --> |"load if program exists"| program-dependent
win --> |"bash alias to each function at"| oswinps1
init --> |"load if $OSTYPE==msys* || -n $WSL_DISTRO_NAME"| win
init --> |"load if $OSTYPE==msys* | -n $WSL_DISTRO_NAME"| win
init --> |"load if $OSTYPE==linux*"| ubu
```

<div style="transform: scale(0.7); transform-origin: top left;">

```mermaid
%%{init: {'theme':'dark'}}%%
flowchart LR
psprofile["Microsoft.PowerShell_profile.ps1"]
program-dependent["
programs/[program].ps1
...
"]
init --> |"load if program exists"| program-dependent
init["ps-sh-hepers/init.ps1"]
oswinps1["os_win.ps1"]
psprofile--> |"load"| init
init --> |"load"| oswinps1
```
<div>

## Install

From bash, install ps-sh-helpers by:
Expand Down
22 changes: 17 additions & 5 deletions init.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
HELPERS_DIR="$(dirname "${BASH_SOURCE[0]}")"

# load os_any.bash
# -- bash basic --
function log_error() { echo -e "\033[00;31m-- $* \033[00m"; }
function log_msg() { echo -e "\033[00;33m-- $* \033[00m"; }
alias bashrc_reload='source $HOME/.bashrc'
alias folder_count_files='find . -maxdepth 1 -type f | wc -l'
alias folder_count_files_recusive='find . -maxdepth 1 -type f | wc -l'
alias folder_list_sorted_by_size='du -ahd 1 | sort -h'
alias folder_find_file_with_crlf='find . -not -type d -exec file "{}" ";" | grep CRLF'
alias passwd_generate='echo $(tr -dc "A-Za-z0-9!?%=" < /dev/urandom | head -c 12)'
function ssh_fix_permisisons() {
# https://stackoverflow.com/questions/9270734/ssh-permissions-are-too-open
chmod 700 $HOME/.ssh/
chmod 600 $HOME/.ssh/id_rsa
chmod 600 $HOME/.ssh/id_rsa.pubssh-rsa
}

source "$HELPERS_DIR/os_any.bash"

# load os_<name>.bash files
# -- load os_<name>.bash files --

if [[ $OSTYPE == msys* || -n $WSL_DISTRO_NAME ]]; then
source "$HELPERS_DIR/os_win.bash"
Expand All @@ -13,7 +25,7 @@ if [[ $OSTYPE == linux* ]]; then
source "$HELPERS_DIR/os_ubu.bash"
fi

# load <program>.bash files
# -- load <program>.bash files --

for file in "$HELPERS_DIR/programs/"*.bash; do
program=$(basename ${file%.*})
Expand Down
90 changes: 0 additions & 90 deletions os_any.bash

This file was deleted.

4 changes: 2 additions & 2 deletions os_win.bash
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fi
# -- load funcs from init.ps1 as aliases --

function _ps_call() {
powershell.exe -command "& { . $(wslpath -w $HELPERS_DIR/init.ps1); $* }"
powershell.exe -command "& { . $(wslpath -w $HELPERS_DIR/os_win.ps1); $* }"
}

function _ps_def_func() {
Expand Down Expand Up @@ -47,7 +47,7 @@ while read -r line; do
_ps_def_func $func
fi
fi
done <$HELPERS_DIR/init.ps1
done <$HELPERS_DIR/os_win.ps1

function wsl_install_cuda_cudnn() {
# https://canonical-ubuntu-wsl.readthedocs-hosted.com/en/latest/tutorials/gpu-cuda/
Expand Down
9 changes: 9 additions & 0 deletions programs/ssh.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function ssh_send_to_server_authorized_pub_key() {
: ${1?"Usage: ${FUNCNAME[0]} <user@server>"}
ssh "$1" sh -c 'cat - >> ~/.ssh/authorized_keys' <$HOME/.ssh/id_rsa.pub
}

function ssh_send_to_server_priv_key() {
: ${1?"Usage: ${FUNCNAME[0]} <user@server>"}
ssh "$1" sh -c 'cat - > ~/.ssh/id_rsa;chmod 600 $HOME/.ssh/id_rsa' <$HOME/.ssh/id_rsa
}
32 changes: 32 additions & 0 deletions programs/tar.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
function decompress() {
: ${1?"Usage: ${FUNCNAME[0]} <zip-name> [dir-name]"}
local filename=$(basename $1)
local filename_noext="${filename%.*}"
local extension=${path##*.}
local dest
if [ $# -eq 1 ]; then dest=.; else dest=$2; fi
case $filename in
*.tar.bz2 | *.tbz | *.tbz2) ret=$(tar -xzf "$1" -C $dest) ;;
*.gz | *.Z) ret=$(gunzip "$1" >$dest/$filename_noext) ;;
*.bz2) ret=$(tar -xjf "$1" -C $dest) ;;
*.zip) ret=$(unzip "$1" -d $dest) ;;
*.zst) ret=$(tar --use-compress-program=unzstd -xvf "$1" -C $dest) ;;
*.xz) ret=$(tar -xJf "$1" -C $dest) ;;
*) log_error "$extension is not supported compress." && return 1 ;;
esac
if [ $? != 0 ] || ! [ -f $file_name ]; then
log_error "decompress "$1" failed " && return 1
fi
}

function decompress_from_url() {
: ${2?"Usage: ${FUNCNAME[0]} <URL> <dir>"}
local file_name="/tmp/$(basename $1)"
if test ! -e $file_name; then
log_msg "fetching "$1" to /tmp/"
curl -LJ "$1" --create-dirs --output $file_name
if [ $? != 0 ]; then log_error "curl fail" && return; fi
fi
log_msg "extracting $file_name to $2"
decompress $file_name $2
}

0 comments on commit 3831d0e

Please sign in to comment.