1#!/usr/bin/env bash
2
3set -e
4
5function _check_setup {
6  # Checking git and secret-plugin setup:
7  local is_tree
8  is_tree=$(_is_inside_git_tree)
9  if [[ "$is_tree" -ne 0 ]]; then
10    _abort "not in dir with git repo. Use 'git init' or 'git clone', then in repo use 'git secret init'"
11  fi
12
13  # Checking if the '.gitsecret' dir (or as set by SECRETS_DIR) is not ignored:
14  _secrets_dir_is_not_ignored
15
16  # Checking gpg setup:
17  local keys_dir
18  keys_dir=$(_get_secrets_dir_keys)
19
20  local secring="$keys_dir/secring.gpg"
21  if [[ -f $secring ]] && [[ -s $secring ]]; then
22    # secring.gpg exists and is not empty,
23    # someone has imported a private key.
24    _abort 'it seems that someone has imported a secret key.'
25  fi
26}
27
28
29function _incorrect_usage {
30  echo "git-secret: abort: $1"
31  usage
32  exit "$2"
33}
34
35
36function _show_version {
37  echo "$GITSECRET_VERSION"
38  exit 0
39}
40
41
42function _init_script {
43  if [[ $# == 0 ]]; then
44    _incorrect_usage 'no input parameters provided.' 126
45  fi
46
47  # Parse plugin-level options:
48  local dry_run=0
49
50  while [[ $# -gt 0 ]]; do
51    local opt="$1"
52
53    case "$opt" in
54      # Options for quick-exit strategy:
55      --dry-run)
56        dry_run=1
57        shift;;
58
59      --version) _show_version;;
60
61      *) break;;  # do nothing
62    esac
63  done
64
65  if [[ "$dry_run" == 0 ]]; then
66    # Checking for proper set-up:
67    _check_setup
68
69    # Routing the input command:
70    local function_exists
71    function_exists=$(_function_exists "$1")
72
73    if [[ "$function_exists" == 0 ]] && [[ ! $1 == _* ]]; then
74      $1 "${@:2}"
75    else  # TODO: elif [[ $(_plugin_exists $1) == 0 ]]; then
76      _incorrect_usage "command $1 not found." 126
77    fi
78  fi
79}
80
81
82_init_script "$@"
83