penguin/utils

my env utils

commit e7c016e21be844d3cafabba26e06cb3094bb233d

author斟酌 鵬兄 <tgckpg@gmail.com>
date2022-08-11T12:36:59Z
subjectSimplify rbashrc
commit e7c016e21be844d3cafabba26e06cb3094bb233d
Author: 斟酌 鵬兄 <tgckpg@gmail.com>
Date:   2022-08-11T12:36:59Z

    Simplify rbashrc
---
 bash/bashrc/rbashrc | 123 ++++++++++++++++++++--------------------------------
 1 file changed, 47 insertions(+), 76 deletions(-)

diff --git a/bash/bashrc/rbashrc b/bash/bashrc/rbashrc
index 1f7cad8..0ec4515 100644
--- a/bash/bashrc/rbashrc
+++ b/bash/bashrc/rbashrc
@@ -5,6 +5,8 @@ case $- in
 esac
 
 __SCRIPT=$BASH_SOURCE
+RBASH_HOME="$HOME/.rbash"
+RBASH_SOURCES="$RBASH_HOME/sources"
 
 # Source global definitions
 if [ -f /etc/bashrc ]; then
@@ -16,6 +18,8 @@ if [ -f ~/.rbashenv ]; then
     . ~/.rbashenv
 fi
 
+mkdir -p "$RBASH_SOURCES"
+
 # don't put duplicate lines or lines starting with space in the history.
 # See bash(1) for more options
 HISTCONTROL=ignoreboth
@@ -88,58 +92,40 @@ if [ $? -eq 0 ]; then
     fi
 fi
 
-BASHL_UUID="<UUID>"
-export BASH_SDIR=/tmp/$BASHL_UUID
-[ ! -d "$BASH_SDIR" ] && mkdir "$BASH_SDIR"
-
-
-function __cryptd {
-    echo "<PASSWD>" | gpg --batch -q --cipher-algo AES256 --passphrase-fd 0 -o "$2" -d "$1"
-}
-
-function __crypte {
-    echo "<PASSWD>" | gpg --batch -q --cipher-algo AES256 --passphrase-fd 0 -o "$1.enc" -c "$1"
-    mv "$1.enc" "$1"
-}
-__CFILE=""
-function __cacheDownload {
-    MLINK=$( echo "$1" | md5sum | cut -d' ' -f1 )
-
-    __CFILE=$BASH_SDIR/$MLINK
-
-    if [ ! -f "$__CFILE" ]; then
-        __download "$1" > "$__CFILE"
-        __crypte "$__CFILE"
-    fi
-}
-
 function __download {
     which curl 2>&1 > /dev/null
     if [ $? -eq 0 ]; then
         curl -s "$1"
-        return;
+        return
     fi
 
     which wget 2>&1 > /dev/null
     if [ $? -eq 0 ]; then
         wget -qO- "$1"
-        return;
+        return
     fi
 }
 
-function __uuid {
-    which uuidgen 2>&1 > /dev/null
-    if [ $? -eq 0 ]; then
-        uuidgen
-    else
-        head -n1 /proc/sys/kernel/random/uuid
+function rbash_cache {
+    local CACHE_FILE
+    MLINK=$( echo "$1" | md5sum | cut -d' ' -f1 )
+
+    CACHE_FILE="$RBASH_SOURCES/$MLINK"
+
+    if [ ! -f "$CACHE_FILE" ]; then
+        __download "$1" > "$CACHE_FILE"
     fi
+
+    chmod 700 "$CACHE_FILE"
+    return "$CACHE_FILE"
 }
 
 function rbash_upgrade {
     echo "Updating $__SCRIPT"
-    TMPID=`__uuid`
-    TMPFILE=/tmp/$TMPID
+
+    local TMPFILE
+
+    TMPFILE=$( mktemp )
     __download "https://git.k8s.astropenguin.net/penguin/utils/raw/branch/master/bash/bashrc/rbashrc" > $TMPFILE
 
     if [ -z "$1" ]; then
@@ -150,76 +136,61 @@ function rbash_upgrade {
         MCC_NAME=$2
     fi
 
-    UUID=`__uuid`
-    PASS=`__uuid`
     PMC_NAME=$( echo "#_MACHINE_NAME_#" | sed -e "s/#_/</g" -e "s/_#/>/g" )
     PMCC_NAME=$( echo "#_MACHINE_COLORED_NAME_#" | sed -e "s/#_/</g" -e "s/_#/>/g" )
-    UUID_TOK=$( echo "#_UUID_#" | sed -e "s/#_/</g" -e "s/_#/>/g" )
-    PASS_TOK=$( echo "#_PASSWD_#" | sed -e "s/#_/</g" -e "s/_#/>/g" )
     COLOR_CODE_TOK=$( echo "#_COLOR_CODE_#" | sed -e "s/#_/</g" -e "s/_#/>/g" )
     COLOR_CODE=$(( $RANDOM * 6 / 32767 + 1 ))
 
     sed -i \
         -e "s/$PMC_NAME/$MC_NAME/g" \
         -e "s/$PMCC_NAME/$MCC_NAME/g" \
-        -e "s/$UUID_TOK/$UUID/g" \
-        -e "s/$PASS_TOK/$PASS/g" \
         -e "s/$COLOR_CODE_TOK/$COLOR_CODE/g" \
          $TMPFILE
 
     mv $TMPFILE $__SCRIPT
 
     # Clean up the old dir
-    if [ $? -eq 0 ] && [ -d "$BASH_SDIR" ]; then
-        rm -r "$BASH_SDIR"
+    if [ $? -eq 0 ] && [ -d "$RBASH_SOURCES" ]; then
+        rm -r "$RBASH_SOURCES"
     fi
 
-    . $__SCRIPT
+    source $__SCRIPT
 }
 
 function rbash_run {
+    local f
     echo "Getting: $1"
-    __cacheDownload "https://git.k8s.astropenguin.net/penguin/utils/raw/branch/master/$1"
+
+    f=`rbash_cache "https://git.k8s.astropenguin.net/penguin/utils/raw/branch/master/$1"`
     shift
-    TMPID=`__uuid`
-    TMPFILE=/tmp/$TMPID
-    __cryptd "$__CFILE" "$TMPFILE"
 
-    chmod 700 "$TMPFILE"
-    "$TMPFILE" "$@"
-    rm "$TMPFILE"
+    "$f" "$@"
 }
 
 export PS1='This is <MACHINE_NAME>\e[1;3<COLOR_CODE>m<MACHINE_COLORED_NAME>\e[0m: \w\n\$ '
 export EDITOR=vim
 
 # User specific aliases and functions
-echo "Source:"
-function __ns {
-    if [ -z "$2" ]; then
-        FPATH="sources/$1"
-    else
-        FPATH=$1
-    fi
-
+function rbash_load {
+    local f path
     echo "  $1"
-    __cacheDownload "https://git.k8s.astropenguin.net/penguin/utils/raw/branch/master/bash/bashrc/$FPATH"
-    shift
-    TMPID=`__uuid`
-    TMPFILE=/tmp/$TMPID
-    __cryptd "$__CFILE" "$TMPFILE"
-    . $TMPFILE
-    rm $TMPFILE
+    path="sources/$1"
+    f=rbash_cache "https://git.k8s.astropenguin.net/penguin/utils/raw/branch/master/bash/bashrc/$path"
+    source "$f"
 }
 
-__ns "package.sh" 1
-__ns "10_aliases"
-__ns "12_shortcuts"
-__ns "20_fast-greps"
-__ns "40_go-command"
-__ns "41_pivot-command"
-
-# Source for additional rc
-if [ -f ~/.bashrc_local ]; then
-    . ~/.bashrc_local
+# Create default source config
+if [ ! -f "$RBASH_HOME/source.conf" ]; then
+    cat <<___DEFAULT___ > "$RBASH_HOME/source.conf"
+echo "Source:"
+rbash_load "package.sh" 1
+rbash_load "10_aliases"
+rbash_load "12_shortcuts"
+rbash_load "20_fast-greps"
+rbash_load "40_go-command"
+rbash_load "41_pivot-command"
+___DEFAULT___
+    chmod 600 "$RBASH_HOME/source.conf"
 fi
+
+source "$RBASH_HOME/source.conf"