From 9f528ac4c4ad219ce91f43d14a97401348f315f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isma=C3=ABl=20Bouya?= Date: Wed, 21 Nov 2018 19:11:56 +0100 Subject: Move bash functions to separate scripts --- roles/contexts/fretlink/files/scripts/_fl_tmux_dev | 14 +++++ roles/contexts/fretlink/files/scripts/fl_getDump | 3 + roles/contexts/fretlink/files/scripts/fl_tmux_dev | 36 ++++++++++++ roles/contexts/fretlink/files/scripts/fl_vpn | 8 +++ roles/contexts/fretlink/tasks/main.yml | 21 +++++++ roles/shell/bash/templates/bashrc.j2 | 67 +++------------------- roles/tools/templates/tmux.conf.j2 | 2 +- 7 files changed, 92 insertions(+), 59 deletions(-) create mode 100644 roles/contexts/fretlink/files/scripts/_fl_tmux_dev create mode 100644 roles/contexts/fretlink/files/scripts/fl_getDump create mode 100644 roles/contexts/fretlink/files/scripts/fl_tmux_dev create mode 100644 roles/contexts/fretlink/files/scripts/fl_vpn diff --git a/roles/contexts/fretlink/files/scripts/_fl_tmux_dev b/roles/contexts/fretlink/files/scripts/_fl_tmux_dev new file mode 100644 index 0000000..3cfe50a --- /dev/null +++ b/roles/contexts/fretlink/files/scripts/_fl_tmux_dev @@ -0,0 +1,14 @@ +#!/bin/bash + +_fl_tmux_dev() { + COMPREPLY=() + local cur="${COMP_WORDS[COMP_CWORD]}" + prefix="$HOME/workdir/" + local IFS=$'\n' + local items=($(IFS=' ' compgen -W "bash remote puppet" -- $cur) $(compgen -d $prefix$cur)) + for item in ${items[@]}; do + [[ $item == $prefix.* ]] && continue + COMPREPLY+=("${item#$prefix}") + done +} +complete -o filenames -F _fl_tmux_dev fl_tmux_dev diff --git a/roles/contexts/fretlink/files/scripts/fl_getDump b/roles/contexts/fretlink/files/scripts/fl_getDump new file mode 100644 index 0000000..808dc01 --- /dev/null +++ b/roles/contexts/fretlink/files/scripts/fl_getDump @@ -0,0 +1,3 @@ +#!/bin/bash + +s3cmd -c <(pass show Travail/Fretlink/S3) get "$1" diff --git a/roles/contexts/fretlink/files/scripts/fl_tmux_dev b/roles/contexts/fretlink/files/scripts/fl_tmux_dev new file mode 100644 index 0000000..ca89e27 --- /dev/null +++ b/roles/contexts/fretlink/files/scripts/fl_tmux_dev @@ -0,0 +1,36 @@ +#!/bin/bash + +type="${1:-bash}" +if [ -n "$TMUX" ]; then + m=switch-client +else + m=attach-session +fi + +case "$type" in + "bash") start_dir="$HOME";; + "remote") start_dir="$HOME";; + "puppet") start_dir="/etc/puppetlabs/code" +esac + +if tmux has-session -t "$type" 2>/dev/null; then + tmux $m -t "$type" +else + if [ "$type" = "remote" ]; then + tmux new-session -d -c $start_dir -s "$type" mosh immae.eu \; \ + $m -t "$type" + elif [ -n "$start_dir" ]; then + tmux new-session -d -c $start_dir -s "$type" \; \ + $m -t "$type" \; \ + new-window -c $start_dir + elif [ -d "$HOME/workdir/$type" ]; then + tmux new-session -d -c "$HOME/workdir/$type" -s "$type" \; \ + $m -t "$type" \; \ + send-keys "../start" C-m \; \ + rename-window "running" \; \ + new-window -c "$HOME/workdir/$type" + else + echo "Unknown session" >&2 + exit 1 + fi +fi diff --git a/roles/contexts/fretlink/files/scripts/fl_vpn b/roles/contexts/fretlink/files/scripts/fl_vpn new file mode 100644 index 0000000..1d3bc36 --- /dev/null +++ b/roles/contexts/fretlink/files/scripts/fl_vpn @@ -0,0 +1,8 @@ +#!/bin/bash + +TMPFILE=$(mktemp /tmp/openvpn.XXXXXX) + +trap "rm -f $TMPFILE" EXIT + +pass show Travail/Fretlink/OpenVPN >> $TMPFILE +sudo openvpn $TMPFILE diff --git a/roles/contexts/fretlink/tasks/main.yml b/roles/contexts/fretlink/tasks/main.yml index 4106bf1..ce69e02 100644 --- a/roles/contexts/fretlink/tasks/main.yml +++ b/roles/contexts/fretlink/tasks/main.yml @@ -90,3 +90,24 @@ scope: user name: remind@work.service enabled: true +- name: scripts + block: + - name: completion directory + file: + state: directory + path: $XDG_DATA_HOME/bash_completion + - name: scripts + copy: + mode: 0755 + src: "scripts/{{ item }}" + dest: "$HOME/.local/bin/{{ item }}" + loop: + - fl_tmux_dev + - fl_vpn + - fl_getDump + - name: completion scripts + copy: + src: "scripts/_{{ item }}" + dest: "$XDG_DATA_HOME/bash_completion/_{{ item }}" + loop: + - fl_tmux_dev diff --git a/roles/shell/bash/templates/bashrc.j2 b/roles/shell/bash/templates/bashrc.j2 index bf5acfa..0dc80c0 100644 --- a/roles/shell/bash/templates/bashrc.j2 +++ b/roles/shell/bash/templates/bashrc.j2 @@ -33,16 +33,17 @@ alias info='info -v active-link-style=yellow,bold -v link-style=yellow' [[ -f /usr/share/stgit/completion/stgit-completion.bash ]] && . /usr/share/stgit/completion/stgit-completion.bash +if [[ -d "$XDG_DATA_HOME/bash_completion" ]]; then + for i in $XDG_DATA_HOME/bash_completion/*; do + [[ -f "$i" && -r "$i" ]] && . "$i" + done +fi + {% if role.get(profile) == "fretlink" %} ##### Some Fretlink aliases -alias getDump='s3cmd -c <(pass show Travail/Fretlink/S3) get ' - -function vpn() { - TMPFILE=$(mktemp /tmp/openvpn.XXXXXX) - pass show Travail/Fretlink/OpenVPN >> $TMPFILE - sudo openvpn $TMPFILE - rm -f $TMPFILE -} +alias vpn=fl_vpn +alias tmux_dev=fl_tmux_dev +complete -o filenames -F _fl_tmux_dev tmux_dev function dev_prompt() { if [ -n "$FRETLINK_ENV" ]; then @@ -50,56 +51,6 @@ function dev_prompt() { fi } -function tmux_dev() { - type="${1:-bash}" - if [ -n "$TMUX" ]; then - m=switch-client - else - m=attach-session - fi - - case "$type" in - "bash") start_dir="$HOME";; - "remote") start_dir="$HOME";; - "puppet") start_dir="/etc/puppetlabs/code" - esac - - if tmux has-session -t "$type" 2>/dev/null; then - tmux $m -t "$type" - else - if [ "$type" = "remote" ]; then - tmux new-session -d -c $start_dir -s "$type" mosh immae.eu \; \ - $m -t "$type" - elif [ -n "$start_dir" ]; then - tmux new-session -d -c $start_dir -s "$type" \; \ - $m -t "$type" \; \ - new-window -c $start_dir - elif [ -d "$HOME/workdir/$type" ]; then - tmux new-session -d -c "$HOME/workdir/$type" -s "$type" \; \ - $m -t "$type" \; \ - send-keys "../start" C-m \; \ - rename-window "running" \; \ - new-window -c "$HOME/workdir/$type" - else - echo "Unknown session" >&2 - false - fi - fi -} - -_tmux_dev() { - COMPREPLY=() - local cur="${COMP_WORDS[COMP_CWORD]}" - prefix="$HOME/workdir/" - local IFS=$'\n' - local items=($(IFS=' ' compgen -W "bash remote puppet" -- $cur) $(compgen -d $prefix$cur)) - for item in ${items[@]}; do - [[ $item == $prefix.* ]] && continue - COMPREPLY+=("${item#$prefix}") - done -} -complete -o filenames -F _tmux_dev tmux_dev - if [ -n "$TMUX" ]; then TMUX_SESSION_NAME=$(tmux display-message -p "#S") export HISTFILE="$XDG_STATE_HOME/bash/tmux_${TMUX_SESSION_NAME}_history" diff --git a/roles/tools/templates/tmux.conf.j2 b/roles/tools/templates/tmux.conf.j2 index 0313336..07e88b9 100644 --- a/roles/tools/templates/tmux.conf.j2 +++ b/roles/tools/templates/tmux.conf.j2 @@ -37,7 +37,7 @@ bind-key -T off F12 set-option -u prefix \; set-option -u key-table \; set-optio bind-key -T off C-M-PageDown switch-client -n bind-key -T off C-M-PageUp switch-client -p -bind-key Tab command-prompt -p "which session?" "run-shell \"bash -i -c 'tmux_dev %1'\"" +bind-key Tab command-prompt -p "which session?" "run-shell \"fl_tmux_dev %1\"" # même hack que sur screen lorsqu'on veut profiter du scroll du terminal # (xterm ...) -- cgit v1.2.3