mirror of
				https://github.com/DictXiong/dotfiles.git
				synced 2025-11-04 07:27:48 +08:00 
			
		
		
		
	rewrite piv-agent to sagent; fmt_note and fmt_into will write to stderr
This commit is contained in:
		
							parent
							
								
									a9850e7e3f
								
							
						
					
					
						commit
						13353fcac5
					
				
							
								
								
									
										39
									
								
								.zshrc2
									
									
									
									
									
								
							
							
						
						
									
										39
									
								
								.zshrc2
									
									
									
									
									
								
							@ -113,44 +113,7 @@ if [[ -x $(command -v trash) ]]; then
 | 
				
			|||||||
    alias "rm"="echo use the full path i.e. '/bin/rm'\; consider using trash"
 | 
					    alias "rm"="echo use the full path i.e. '/bin/rm'\; consider using trash"
 | 
				
			||||||
fi
 | 
					fi
 | 
				
			||||||
gbes() { git for-each-ref --sort=-committerdate refs/heads refs/remotes --format="%(authordate:format:%y-%m-%d.%a %H:%M %z)|%(color:red)%(objectname:short)|%(color:yellow)%(refname:short)%(color:reset)|%(color:reset)%(authorname): %(color:green)%(subject)" --color=always | column -ts"|" | less -FX }
 | 
					gbes() { git for-each-ref --sort=-committerdate refs/heads refs/remotes --format="%(authordate:format:%y-%m-%d.%a %H:%M %z)|%(color:red)%(objectname:short)|%(color:yellow)%(refname:short)%(color:reset)|%(color:reset)%(authorname): %(color:green)%(subject)" --color=always | column -ts"|" | less -FX }
 | 
				
			||||||
piv-agent()
 | 
					piv-agent() { eval "$($DOTFILES/tools/sagent.sh $@)" }
 | 
				
			||||||
{
 | 
					 | 
				
			||||||
    local agent_file="/tmp/piv-agent-$(whoami)"
 | 
					 | 
				
			||||||
    if [[ -f $agent_file ]]; then
 | 
					 | 
				
			||||||
        source $agent_file
 | 
					 | 
				
			||||||
    fi
 | 
					 | 
				
			||||||
    # they are: ubuntu, macos, respectively
 | 
					 | 
				
			||||||
    local SO_PATHS=( "/usr/lib64/opensc-pkcs11.so" "/usr/local/lib/opensc-pkcs11.so" )
 | 
					 | 
				
			||||||
    local SO_FILE
 | 
					 | 
				
			||||||
    for SO_FILE in ${SO_PATHS[*]}; do
 | 
					 | 
				
			||||||
        if [[ -f "$SO_FILE" ]]; then
 | 
					 | 
				
			||||||
            break
 | 
					 | 
				
			||||||
        fi
 | 
					 | 
				
			||||||
    done
 | 
					 | 
				
			||||||
    if [[ ! -f "$SO_FILE" ]]; then
 | 
					 | 
				
			||||||
        echo "error: opensc-pkcs11.so not found"
 | 
					 | 
				
			||||||
        return 1
 | 
					 | 
				
			||||||
    fi
 | 
					 | 
				
			||||||
    if ! ps -p "$SSH_AGENT_PID" > /dev/null; then
 | 
					 | 
				
			||||||
        echo "launch agent using: $SO_FILE"
 | 
					 | 
				
			||||||
        ret=$(ssh-agent -P "$SO_FILE")
 | 
					 | 
				
			||||||
        echo "$ret" > $agent_file
 | 
					 | 
				
			||||||
        eval "$ret"
 | 
					 | 
				
			||||||
        if ! ps -p $SSH_AGENT_PID > /dev/null; then
 | 
					 | 
				
			||||||
            echo "failed to launch agent"
 | 
					 | 
				
			||||||
            return 1
 | 
					 | 
				
			||||||
        fi
 | 
					 | 
				
			||||||
    else
 | 
					 | 
				
			||||||
        echo "using existing agent: $SSH_AGENT_PID"
 | 
					 | 
				
			||||||
    fi
 | 
					 | 
				
			||||||
    if ! ssh-add -l > /dev/null; then
 | 
					 | 
				
			||||||
        echo "no keys found in this agent, let's add some"
 | 
					 | 
				
			||||||
        ssh-add -s "$SO_FILE"
 | 
					 | 
				
			||||||
    fi
 | 
					 | 
				
			||||||
    pgrep ssh-agent | grep -v $SSH_AGENT_PID | xargs kill -9
 | 
					 | 
				
			||||||
    echo "now available keys:"
 | 
					 | 
				
			||||||
    ssh-add -l
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
# key bindings
 | 
					# key bindings
 | 
				
			||||||
bindkey "^b" beginning-of-line  # ctrl+b
 | 
					bindkey "^b" beginning-of-line  # ctrl+b
 | 
				
			||||||
 | 
				
			|||||||
@ -75,11 +75,11 @@ fmt_warning() {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fmt_info() {
 | 
					fmt_info() {
 | 
				
			||||||
    printf '%sinfo: %s\n' "${FMT_RESET}" "$*" >&1
 | 
					    printf '%sinfo: %s\n' "${FMT_RESET}" "$*" >&2
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fmt_note() {
 | 
					fmt_note() {
 | 
				
			||||||
    printf '%s%s%s\n' "${FMT_GREEN}" "$*" "${FMT_RESET}" >&1
 | 
					    printf '%s%s%s\n' "${FMT_GREEN}" "$*" "${FMT_RESET}" >&2
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
setup_color() {
 | 
					setup_color() {
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										105
									
								
								tools/sagent.sh
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										105
									
								
								tools/sagent.sh
									
									
									
									
									
										Executable file
									
								
							@ -0,0 +1,105 @@
 | 
				
			|||||||
 | 
					#!/bin/bash
 | 
				
			||||||
 | 
					set -e
 | 
				
			||||||
 | 
					THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd )
 | 
				
			||||||
 | 
					export DFS_COLOR=1
 | 
				
			||||||
 | 
					source "$THIS_DIR/common.sh"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					find_so_file()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    local SO_PATHS=( "/usr/lib64/opensc-pkcs11.so" "/usr/local/lib/opensc-pkcs11.so" )
 | 
				
			||||||
 | 
					    local SO_FILE
 | 
				
			||||||
 | 
					    for SO_FILE in ${SO_PATHS[*]}; do
 | 
				
			||||||
 | 
					        if [[ -f "$SO_FILE" ]]; then
 | 
				
			||||||
 | 
					            echo "$SO_FILE"
 | 
				
			||||||
 | 
					            return
 | 
				
			||||||
 | 
					        fi
 | 
				
			||||||
 | 
					    done
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					create_agent()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    local SO_FILE=$(find_so_file)
 | 
				
			||||||
 | 
					    if [[ -n "$SO_FILE" ]]; then
 | 
				
			||||||
 | 
					        fmt_note "opensc-pkcs11.so found"
 | 
				
			||||||
 | 
					        SO_FILE="-P $SO_FILE"
 | 
				
			||||||
 | 
					    fi
 | 
				
			||||||
 | 
					    ssh-agent $SO_FILE
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					kill_agent()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    if pgrep -x ssh-agent > /dev/null; then
 | 
				
			||||||
 | 
					        fmt_note "killing existing agent"
 | 
				
			||||||
 | 
					        pkill -9 -x ssh-agent
 | 
				
			||||||
 | 
					    fi
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					add_piv()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    local SO_FILE=$(find_so_file)
 | 
				
			||||||
 | 
					    if [[ -n "$SO_FILE" ]]; then
 | 
				
			||||||
 | 
					        echo ssh-add -s \"$SO_FILE\"
 | 
				
			||||||
 | 
					    else
 | 
				
			||||||
 | 
					        fmt_error "opensc-pkcs11.so not found"
 | 
				
			||||||
 | 
					    fi
 | 
				
			||||||
 | 
					    list
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					list()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    echo echo "available keys:"
 | 
				
			||||||
 | 
					    echo ssh-add -l
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					reset()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    kill_agent
 | 
				
			||||||
 | 
					    all
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					all()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    local agent_file="/tmp/piv-agent-$(whoami)"
 | 
				
			||||||
 | 
					    if [[ -f $agent_file ]]; then
 | 
				
			||||||
 | 
					        source $agent_file > /dev/null
 | 
				
			||||||
 | 
					    else
 | 
				
			||||||
 | 
					        touch $agent_file
 | 
				
			||||||
 | 
					        chmod 600 $agent_file
 | 
				
			||||||
 | 
					    fi
 | 
				
			||||||
 | 
					    if ! ps -p "$SSH_AGENT_PID" > /dev/null; then
 | 
				
			||||||
 | 
					        kill_agent
 | 
				
			||||||
 | 
					        fmt_note "launching a new agent"
 | 
				
			||||||
 | 
					        create_agent | tee $agent_file
 | 
				
			||||||
 | 
					    else
 | 
				
			||||||
 | 
					        fmt_note "using existing agent: $SSH_AGENT_PID"
 | 
				
			||||||
 | 
					        cat $agent_file
 | 
				
			||||||
 | 
					    fi
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					route()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    if [[ $# -eq 0 ]]; then
 | 
				
			||||||
 | 
					        all
 | 
				
			||||||
 | 
					        return
 | 
				
			||||||
 | 
					    fi
 | 
				
			||||||
 | 
					    case $1 in
 | 
				
			||||||
 | 
					        kill)
 | 
				
			||||||
 | 
					            kill_agent
 | 
				
			||||||
 | 
					            ;;
 | 
				
			||||||
 | 
					        piv)
 | 
				
			||||||
 | 
					            add_piv
 | 
				
			||||||
 | 
					            ;;
 | 
				
			||||||
 | 
					        reset)
 | 
				
			||||||
 | 
					            reset
 | 
				
			||||||
 | 
					            ;;
 | 
				
			||||||
 | 
					        list|ls)
 | 
				
			||||||
 | 
					            list
 | 
				
			||||||
 | 
					            ;;
 | 
				
			||||||
 | 
					        *)
 | 
				
			||||||
 | 
					            fmt_error "unknown command: $1"
 | 
				
			||||||
 | 
					            ;;
 | 
				
			||||||
 | 
					    esac
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					route "$@"
 | 
				
			||||||
@ -18,6 +18,7 @@ grep -q ".zshrc2" ~/.zshrc
 | 
				
			|||||||
dfs version
 | 
					dfs version
 | 
				
			||||||
dfs log 1
 | 
					dfs log 1
 | 
				
			||||||
dfs beacon gh.ci $GITHUB_SHA
 | 
					dfs beacon gh.ci $GITHUB_SHA
 | 
				
			||||||
 | 
					sagent
 | 
				
			||||||
z ~
 | 
					z ~
 | 
				
			||||||
test ~ -ef "$(pwd)"
 | 
					test ~ -ef "$(pwd)"
 | 
				
			||||||
dogo
 | 
					dogo
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user