summaryrefslogtreecommitdiff
path: root/buildscripts/auto_install_db_contrib_tool.sh
blob: 1ba77c12460b6811616d834199f0acda74341575 (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/bin/bash

set -o errexit

echo "+-----------------------------------------------------------------------------------------------------------+"
echo "|   Running a script to automatically install db-contrib-tool (https://github.com/10gen/db-contrib-tool).   |"
echo "+-----------------------------------------------------------------------------------------------------------+"
echo

if [[ -d "/opt/mongodbtoolchain/v3/bin" ]]; then
  export PATH="/opt/mongodbtoolchain/v3/bin:$PATH"
fi

rc_file=""
if [[ -f "$HOME/.bashrc" ]]; then
  rc_file="$HOME/.bashrc"
fi

if [[ -f "$HOME/.zshrc" ]]; then
  rc_file="$HOME/.zshrc"
fi

if ! command -v db-contrib-tool &> /dev/null; then
  if ! python3 -c "import sys; sys.exit(sys.version_info < (3, 7))" &> /dev/null; then
    actual_version=$(python3 -c 'import sys; print(sys.version)')
    echo "You must have python3.7+ installed. Detected version $actual_version."
    echo "To avoid unexpected issues, python3.7+ will not be automatically installed."
    echo "Please, do it yourself."
    echo
    echo "On macOS you can run:"
    echo
    echo "    brew install python3"
    echo
    exit 1
  fi

  if command -v pipx &> /dev/null; then
    echo "Found pipx: $(command -v pipx)."
    echo "Using it to install 'db-contrib-tool'."
    echo

    pipx ensurepath &> /dev/null
    if [[ -f "$rc_file" ]]; then
      source "$rc_file"
    fi

    pipx install db-contrib-tool --python $(command -v python3) --force
    echo
  else
    if ! python3 -m pipx --version &> /dev/null; then
      echo "Couldn't find pipx. Installing it as python3 module:"
      echo "    $(command -v python3) -m pip install pipx"
      echo
      python3 -m pip install pipx
      echo
    else
      echo "Found pipx installed as python3 module:"
      echo "    $(command -v python3) -m pipx --version"
      echo "Using it to install 'db-contrib-tool'."
      echo
    fi

    python3 -m pipx ensurepath &> /dev/null
    if [[ -f "$rc_file" ]]; then
      source "$rc_file"
    fi

    python3 -m pipx install db-contrib-tool --force
    echo
  fi
fi

echo "Please, open a new shell or run:"
echo
echo "    source $rc_file"
echo