Our team often has indeterminate access issues to our Aritfactory binaries. These issues can go missed because we may have our binaries locally cached and our projects builds may still succeed until there is new dependencies that might throw an error.
I wrote a simple script to try to access Artifactory and alert us if there are issues. My bash isn't great so wondering if there's a more elegant way to test this.
#!/usr/bin/env bash -e
result=$(curl -sSf --header "X-JFrog-Art-Api: $ARTIFACTORY_API_KEY" -XPOST "https://bin.tcc.li/artifactory/api/security/token" -d "username=$ARTIFACTORY_SVC_ACCT" -d "scope=member-of-groups:*" > /dev/null && echo "success")
if [ $result = 'success' ]; then
echo -e $result
echo -e "Your general Artifactory access is successful\n"
else
echo -e $result
echo -e "Connection to Artifactory failed\n"
fi
1 Answer 1
Looks OK. I only have four suggestions:
Use
printf
rather thanecho
. See Why is printf better than echo?Put the URL at the end of the command, not in the middle. And put it in a variable, e.g.
$URL
.Use
\
and newlines and indentation to make your code more readable. Long lines, especially those wider than your terminal/editing-window are a PITA. e.g.URL='https://bin.tcc.li/artifactory/api/security/token' result=$(curl -sSf -XPOST \ --header "X-JFrog-Art-Api: $ARTIFACTORY_API_KEY" \ -d "username=$ARTIFACTORY_SVC_ACCT" \ -d "scope=member-of-groups:*" \ "$URL" > /dev/null && echo "success")
If necessary use variables to shorten the lines. A few variable assignments aren't going to make any noticeable impact on performance (unless done thousands of times in a loop - so avoid doing that), but can make your code a lot easier to read.
e.g. the
X-JFrog-Art-Api: $ARTIFACTORY_API_KEY
header and the -XPOST URL can both be put into variables, as can theusername=
andscope=
-d
data options.You don't need command substitution to get a
$result
variable. You're already testing the exit code fromcurl
with&& echo success
- you can do that directly in theif
statement.
#!/bin/bash
URL='https://bin.tcc.li/artifactory/api/security/token'
API_KEY="X-JFrog-Art-Api: $ARTIFACTORY_API_KEY"
username_data="username=$ARTIFACTORY_SVC_ACCT"
scope_data='scope=member-of-groups:*'
if curl -sSf -XPOST \
--header "$API_KEY" \
-d "$username_data" \
-d "$scope_data" \
"$URL" > /dev/null 2>&1; then
result='success'
printf "%s" "$result"
printf "%s\n" "Your general Artifactory access is successful"
else
printf "%s\n" "Connection to Artifactory failed"
fi
and you only need the result=success
if you're going to use that variable later in the script. I can't see any benefit in printing it either.
Or you could capture the actual output from curl
into $result
(rather than just "success" or nothing) if you have a use for it, and still use curl's exit code for the if
statement.