aboutsummaryrefslogtreecommitdiff
path: root/nexus_bash.sh
blob: 82d135c648050615a17b80ce9434c59b3ae68ef6 (plain) (blame)
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
#!/bin/bash
#
# Global Variables
#
NEXUS_BASH_ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
NEXUS_BASH_INTERFACE_ROOT="${NEXUS_BASH_ROOT}/interface.d"

#
# Global Arguments
#
# NEXUS_URL: str
#   HTTP[S] address of the Nexus server
#   e.g. NEXUS_URL="https://nexus.server.tld"
#
NEXUS_URL=${NEXUS_URL:-}

# NEXUS_AUTH: str
#   Provide user credentials to the Nexus server. Username and password are delimited by ':'
#   e.g. NEXUS_AUTH="username:password"
NEXUS_AUTH=${NEXUS_AUTH:-}

# NEXUS_BASH_VERBOSE: int
#   Toggle verbose output mode
#   e.g. NEXUS_BASH_VERBOSE=1
NEXUS_BASH_VERBOSE=${NEXUS_BASH_VERBOSE:-0}

#
# Common Functions
#

# Check NEXUS_URL is a valid Nexus server
# @return non-zero indicates failure
nexus_available() {
    response=$(curl --head -X GET "$NEXUS_URL/service/rest/v1/status" | head -n 1 | awk '{print $2}')
    if (( $response != 200 )); then
        return 1
    fi
    return 0
}

# Populate interfaces
for iface in $(find "${NEXUS_BASH_INTERFACE_ROOT}" -type f -name '*.sh'); do
    source "$iface"
done

nexus_json_begin() {
    python -c "import json, sys; data=json.load(sys.stdin); $*"
}

nexus_json_len() {
    nexus_json_begin "print(len(data))"
}

nexus_json_value() {
    nexus_json_begin "dict(data[$1]).get('$2', '')"
}

nexus_json_pairs() {
    if (( $# == 1 )); then
        nexus_json_begin "[print('{}=\'{}\''.format(k, v)) for (k, v) in dict(data[$1]).items() if k != 'attributes']"
    fi
}