From 1c7532934caf14b1463e98ed21810ef067b07703 Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Fri, 26 Aug 2016 00:57:05 +0100 Subject: [PATCH] Bed --- bin/nb-repo-dirs | 22 +++++++++++++++------- bin/nb-update | 30 +++++++++++++++++++++++++----- etc/profile | 25 +++++++++++++++++-------- etc/profile.d/envs | 36 ++++++++++++++++++++++++++---------- etc/profile.d/functions | 2 +- 5 files changed, 84 insertions(+), 31 deletions(-) diff --git a/bin/nb-repo-dirs b/bin/nb-repo-dirs index cc13dbfc..660f9fdb 100755 --- a/bin/nb-repo-dirs +++ b/bin/nb-repo-dirs @@ -40,8 +40,8 @@ print #my $dir = /\/\Q$FILE_EXISTS\E$/ ? dirname($_) : $_; my $dir = !-d $_ ? dirname($_) : $_; - if (-d $dir and -d "$dir/.git") { - warn "DEBUG: MISSING: $dir\n" if $debug and !-r "$dir/FILE_EXISTS"; + if (-d "$dir/.git") { + warn "DEBUG: MISSING: $dir/$FILE_EXISTS\n" if $debug and !-r "$dir/$FILE_EXISTS"; add_args($dir); #$dir; } else { @@ -54,14 +54,22 @@ print ; sub add_args { - # Concat each args (ex: $0 proie etc/profile ...) + # Concat each args and return existing (ex: $0 proie etc/profile ...) my @new; - foreach my $d (@_) { - foreach my $a (@ARGV) { + + for my $d (@_) { + #for my $d (glob join(' ',@_)) { + #for my $d (map { glob $_ } @_) { + #warn ".. $d" if $debug; + for my $a (@ARGV) { #warn "$d/$a"; - push @new, "$d/$a"; + #warn ".. $d/$a" if $debug; + push @new, glob "$d/$a"; } } - return @new if @new; + + #warn @new; + @_ = grep { -e $_} @new if @new; + #return grep {-e $_} @_; return @_; } diff --git a/bin/nb-update b/bin/nb-update index ba04e1c5..870225f9 100755 --- a/bin/nb-update +++ b/bin/nb-update @@ -16,14 +16,32 @@ declare CLEANUP=0 ##################################################################### # Git Connection check # +url2nc() { + perl -e ' +for (@ARGV) { +@_ = m,://([^/:]+)(?::(\d+))?, and print join(" ",$_[0],$_[1] ? $_[1] : (/^https/?443:80))."\n", and next; +@_ = m,^(?:[^@]+@)?([^:/]+):[^/].*?(?:\.git)?$, and print "$1 22\n" and next; +@_ = m,([^@:/]+):[^/].*?(?:\.git)?$, and print "$1 22\n" and next; +} +' $@ + return 0 +} +#[ "$1" = "TEST" ] && url2nc nico@zaza.com:re.git && exit +#[ "$1" = "TEST" ] && url2nc zaza.com:re.git && exit +#[ "$1" = "TEST" ] && url2nc https://zaza.com/re.git && exit _git_conn_check=" " git_conn_check() { - local dest=$(git config --local --get remote.origin.url | sed -e 's/:.*$//' -e 's/^.*@//') + #local dest=$(git config --local --get remote.origin.url | sed -e 's/:.*$//' -e 's/^.*@//') + local dest=$(git config --local --get remote.origin.url) + local port='' if [ -z "$dest" ]; then - logger -t $NAME -s "Can't get \`git config --het remote.origin.url\` from `pwd`" + logger -t $NAME -s "Can't get url from \`git config --het remote.origin.url\` in `pwd`" return 1 fi + #[ "$1" = "TEST" ] && + dest=$(url2nc "$dest") + #dest="$dest 22" local ok case "$_git_conn_check" in @@ -33,8 +51,9 @@ git_conn_check() { ok=0 case "$dest" in *"@"*) timeout 5 ssh -o BatchMode=yes -o ConnectTimeout=5 $dest true && ok=1 ;; - *) nc -z -w 5 $dest 22 && ok=1 ;; + *) nc -z -w 5 $dest && ok=1 ;; esac +#echo "$ok: git=$(git config --local --get remote.origin.url) | dest=$dest | url2nc="$(url2nc $(git config --local --get remote.origin.url)) _git_conn_check="${_git_conn_check}$dest=$ok " ;; esac @@ -77,13 +96,14 @@ for repo in $(nb-repo-dirs); do [ "$CLEANUP" = "1" ] && cmd="$cmd && git gc" if [ 1 = 1 ] && ! git_conn_check; then - [ 1 = 1 ] + echo elif [ "$USER" = "$user" ]; then bash -c "$cmd" elif [ "$USER" = "root" ]; then su $user -c "$cmd" else - echo "Ignore $repo: current user=$USER repo user=$user" + echo "Wrong user current user=$USER repo user=$user" + echo continue fi ret=$? diff --git a/etc/profile b/etc/profile index 6c04a36f..fdf0d9a3 100644 --- a/etc/profile +++ b/etc/profile @@ -1,11 +1,17 @@ # # Avoid infinit loop. Eg: profile -> .bashrc -> profile # -[ -n "$NB_LOOP" ] && return; export NB_LOOP=1 -[ "$1" = "--debug" ] && NB_DEBUG='eval echo 1>&2 "$(basename $BASH_SOURCE): "' && shift +#[ "$1" = "--debug" ] && NB_DEBUG='eval echo 1>&2 "$(basename $BASH_SOURCE): "' && shift +[ "$1" = "--debug" ] && NB_DEBUG='echo NB_DEBUG:' && shift +if [ -n "$NB_LOOP" ]; then + [ -n "$NB_DEBUG" ] && $NB_DEBUG "NB_LOOP=$NB_LOOP: BASH_SOURCE=$BASH_SOURCE \$0=$0" + NB_LOOP=$(($NB_LOOP+1)) + return +fi +export NB_LOOP=1 # -# ROOT PATH +# SEARCH ROOT # dir='.' case "$BASH_SOURCE" in @@ -17,23 +23,26 @@ case "$BASH_SOURCE" in fi ;; esac -[ -e "$dir/profile.d/functions" ] && source "$dir/profile.d/functions" + +[ -e "$dir/profile.d/functions" ] && . "$dir/profile.d/functions" NB_ROOT=$(realpath ${dir}/.. 2>/dev/null) + +#echo "$NB_ROOT: `pwd`" unset dir # # Profiles # if [ -n "$NB_ROOT" ]; then + [ -e "$NB_ROOT/etc/profile.d/envs" ] && . "$NB_ROOT/etc/profile.d/envs" for i in \ - ${NB_ROOT}/etc/profile.d/envs \ ${NB_ROOT}/etc/profile.d/aliases \ ${NB_ROOT}/etc/profile.d/*.sh \ - ${HOME}/.${SHELL_NAME}rc \ + $(nb-repo-dirs "etc/profile" "etc/profile.d/*.sh") \ ;do - [ -n "$NB_DEBUG" ] && $NB_DEBUG "$i" - [ -r "$i" ] && source "$i" + [ -n "$NB_DEBUG" ] && $NB_DEBUG ". $i" + [ -r "$i" -a -f "$i" ] && . "$i" done fi diff --git a/etc/profile.d/envs b/etc/profile.d/envs index f2513931..0e076cad 100644 --- a/etc/profile.d/envs +++ b/etc/profile.d/envs @@ -12,12 +12,17 @@ [ -z "$UID" ] && UID=`id -u` [ -z "$USER" ] && USER=`whoami` [ -z "$OSTYPE" ] && OSTYPE=`uname|tr [:upper:] [:lower:]` -[ -z "PWD" ] && PWD=`pwd` +# NB 25.08.16 [ -z "PWD" ] && PWD=`pwd` + +# We don't want to destroyed variables +if true || env_add_path &>/dev/null; then # -# PATHS +# PATH # -env_add_path &>/dev/null && PATH=`env_add_path "$PATH" \ +[ -n "$NB_ROOT" ] && PATH=$(env_add_path "$PATH" "$NB_ROOT/bin") # to find nb-repo-dirs + #$($NB_ROOT/bin/nb-repo-dirs bin sbin) +PATH=$(env_add_path "$PATH" \ /bin \ /sbin \ /usr/sbin \ @@ -26,24 +31,35 @@ env_add_path &>/dev/null && PATH=`env_add_path "$PATH" \ /opt/local/sbin \ /usr/local/bin \ /usr/local/sbin \ + /usr/share/awstats/tools \ + $(nb-repo-dirs bin sbin) \ + /home/bin \ $HOME/bin \ - $HOME/sbin \ - /usr/share/awstats/tools \ - /home/bin \ - /home/www/wp/bin \ - $($NB_ROOT/bin/nb-repo-dirs | xargs -n1 -IZ printf '%s\n%s\n' "Z/bin" "Z/sbin" 2>/dev/null) -` +) export PATH +# +# MANPATH +# +MANPATH=$(env_add_path "$MANPATH" \ + /usr/share/man\ + /usr/local/man\ + /usr/local/memcached/share/man \ + $(nb-repo-dirs | xargs -n1 -IZ echo "Z/man" 2>/dev/null) \ +) +export MANPATH + +fi # < env_add_path + # # OTHERS # export IGNOREEOF=0 +case "$OSTYPE" in darwin*) export DARWIN=1;; esac export LESS="-iMR" export PAGER="less $LESS" export EDITOR=vim -case "$OSTYPE" in darwin*) export DARWIN=1;; esac # # SHELL diff --git a/etc/profile.d/functions b/etc/profile.d/functions index 4ec9d6f4..83019078 100644 --- a/etc/profile.d/functions +++ b/etc/profile.d/functions @@ -84,7 +84,7 @@ ips() { env_add_path() { # Add paths to a variables # Usage PATH=`env_add_path "$PATH" "/blbabla"` - declare env_value; env_value=$1; shift + declare env_value=$1; shift for p in $@; do [ -e "$p" ] || continue -- 2.47.3