summaryrefslogtreecommitdiffstats
path: root/pkgs/shell/with-ssh
blob: 65459e2d6b44be8aae6feb6e0ce31665f8342eb9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#! /bin/sh
#!buildShellBin prepend-path=openssh
#
# usage: with-ssh [--add[=KEY] ...] [COMMAND [ARGS ...]]
#

set -efu

case ${WITH_SSH_STAGE-1} in
  1)
    export WITH_SSH_STAGE=2
    exec ssh-agent "$0" "$@"
    ;;
  2)
    if test $# = 0; then
      set -- "$SHELL"
    fi
    case "$1" in
      --add)
        shift
        ssh-add
        exec "$0"
        ;;
      --add=*)
        key=$1; shift
        key=${key#--add=}
        ssh-add "$key"
        exec "$0" "$@"
        ;;
      *)
        exec "$@"
    esac
    ;;
  *)
    echo "with-ssh: error: bad stage: $WITH_SSH_STAGE" >&2
    exit 1
esac