aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/logs.sh
blob: a553c4d2ffb657fca7c922a25a73035a85b8cc30 (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
77
78
79
80
81
82
83
84
85
86
#!/bin/bash
###############################
# usage: logs.sh [ -l ARG ] [ -n ARG ]
#  -l ARG: name of log file to display.
#          valid values are 'all', 'sonar', 'web', 'ce' and 'es' (case insensitive).
#          default value is 'all'.
#  -n ARG: number of log line to display. Value is passed to n option of tail.
#          default value is 25
###############################

set -euo pipefail

DEFAULT_LOG="all"
DEFAULT_LINES="25"
LOGS="sonar web ce es"

function toLower() {
  echo "$1" | tr '[:upper:]' '[:lower:]'
}

function checkLogArgument() {
  local logArg="$1"
  local lowerLogArg=$(toLower $logArg)

  if [ "$lowerLogArg" == "$DEFAULT_LOG" ]; then
    return
  fi

  for t in $LOGS; do
    if [ "$lowerLogArg" == "$t" ]; then
      return
    fi
  done

  echo "Unsupported -l argument $logArg"
  exit 1
}

function buildTailArgs() {
  local logArg=$(toLower $logArg)
  local logLines="$2"
  local res=""

  for t in $LOGS; do
    if [ "$logArg" == "$DEFAULT_LOG" ] || [ "$logArg" == "$t" ]; then
      res="$res -Fn $logLines $SQ_HOME/logs/$t.log"
    fi
  done

  echo "$res"
}

function doTail() {
  local logArg="$1"
  local logLines="${2:-"$DEFAULT_LINES"}"
  TAIL_ARG=$(buildTailArgs "$logArg" "$logLines")
  tail $TAIL_ARG
}

# check the script was called to avoid execute when script is only sourced
script_name=$(basename "$0")
if [ "$script_name" == "logs.sh" ]; then
  LOG="$DEFAULT_LOG"
  LINES="$DEFAULT_LINES"
  while getopts ":l:n:" opt; do
    case "$opt" in
      l) LOG=${OPTARG:=$DEFAULT_LOG}
         ;;
      n) LINES=${OPTARG:=$DEFAULT_LINES}
         ;;
      \?)
        echo "Unsupported option $OPTARG" >&2
        exit 1
        ;;
    esac
  done

  checkLogArgument "$LOG"

  ROOT=$(pwd)
  cd sonar-application/build/distributions/sonarqube-*
  SQ_HOME=$(pwd)
  cd "$ROOT"

  doTail "$LOG" "$LINES"
fi