Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,19 @@ Then, install DockerC with the following command:

```bash
/bin/sh -c "$(curl -fsSL https://raw.githubusercontent.com/matiboux/dockerc/HEAD/install.sh)"
# Usage: /bin/sh -c "$(cat install.sh)" -- [--help] [--install-dir <dir>] [tag]
```

Take a look at the [install.sh](install.sh) script to see what it does.

If you get the error `Failure writing output to destination`,
try to run the command again with `sudo` or grant yourself write permissions to install directory (`/usr/local/bin` by default).
If you get a cURL error, check the following:

```bash
sudo /bin/sh -c "$(curl -fsSL https://raw.githubusercontent.com/matiboux/dockerc/HEAD/install.sh)"
```
- cURL error `22`: Requested tag was not found. Verify your network and that the tag exists.

- cURL error `23`: Failed to install DockerC. Verify your permissions in the install directory or try to run the command again with `sudo`.
```bash
sudo /bin/sh -c "$(curl -fsSL https://raw.githubusercontent.com/matiboux/dockerc/HEAD/install.sh)"
```


## Usage
Expand Down
34 changes: 13 additions & 21 deletions dockerc
Original file line number Diff line number Diff line change
Expand Up @@ -240,14 +240,7 @@ fi
INSTALL_TAG=''
if [ -n "$DOCKERC_INSTALL_TAG" ]; then
# Use from argument or environment variable
if
[ "$DOCKERC_INSTALL_TAG" = 'HEAD' ] || [ "$DOCKERC_INSTALL_TAG" = 'head' ] ||
[ "$DOCKERC_INSTALL_TAG" = '.' ] || [ "$DOCKERC_INSTALL_TAG" = '-' ]
then
INSTALL_TAG='HEAD'
else
INSTALL_TAG="$DOCKERC_INSTALL_TAG"
fi
INSTALL_TAG="$DOCKERC_INSTALL_TAG"
fi

DOCKER_PRESET=''
Expand Down Expand Up @@ -492,12 +485,14 @@ get_latest_version() {
)

if [ -n "$LATEST_VERSION_JSON" ]; then
# Extract version from matched string
# Remove `"tag_name": "` (13 characters) and `",` (2 characters)
if [ "$(uname -s)" = 'Darwin' ]; then
# MacOS
LATEST_VERSION="${LATEST_VERSION_JSON:14:$((${#LATEST_VERSION_JSON}-14-2))}"
LATEST_VERSION="${LATEST_VERSION_JSON:13:$((${#LATEST_VERSION_JSON}-13-2))}"
else
# Linux
LATEST_VERSION=$(expr substr "$LATEST_VERSION_JSON" $(expr 1 + 14) $(expr length "$LATEST_VERSION_JSON" - 14 - 2))
LATEST_VERSION=$(expr substr "$LATEST_VERSION_JSON" $(expr 1 + 13) $(expr length "$LATEST_VERSION_JSON" - 13 - 2))
fi

echo "$LATEST_VERSION"
Expand All @@ -512,7 +507,9 @@ if [ "$DOCKERC_PRINT_VERSION" = 'true' ]; then

if [ "$DRY_RUN" = 'false' ]; then
LATEST_VERSION=$(get_latest_version)
if [ "$VERSION" != "$LATEST_VERSION" ]; then
if [ -z "$LATEST_VERSION" ]; then
echo 'Notice: Failed to get DockerC latest version.' >&2
elif [ "$VERSION" != "$LATEST_VERSION" ]; then
echo "Notice: DockerC is not up to date, latest version is $LATEST_VERSION!" >&2
fi
fi
Expand All @@ -528,29 +525,24 @@ fi
if [ "$UPDATE" = 'true' ]; then
# Update DockerC & exit

if [ ! -d "$INSTALL_DIR" ]; then
echo 'Error: Install directory does not exist.' >&2
exit 1
fi

if [ -z "$INSTALL_TAG" ]; then
# Default install tag is latest released version
INSTALL_TAG="$(get_latest_version)"
INSTALL_TAG_ARG=''
if [ -n "$INSTALL_TAG" ]; then
INSTALL_TAG_ARG=" \"$INSTALL_TAG\""
fi

if [ "$QUIET" = 'false' ]; then
# Print install script command
if [ "$DOCKERC_FIRST_NEWLINE" != 'false' ]; then
echo ''
fi
echo "> curl -fsSL \"https://raw.githubusercontent.com/matiboux/dockerc/HEAD/install.sh\" | /bin/sh -s -- \"${INSTALL_DIR}\" \"${INSTALL_TAG}\""
echo "> curl -fsSL \"https://raw.githubusercontent.com/matiboux/dockerc/HEAD/install.sh\" | /bin/sh -s -- --install-dir \"${INSTALL_DIR}\"${INSTALL_TAG_ARG}"
echo ''
fi

if [ "$DRY_RUN" = 'false' ]; then
# Run install script command
echo "Updating DockerC to ${INSTALL_TAG}..."
exec sh -c "curl -fsSL \"https://raw.githubusercontent.com/matiboux/dockerc/HEAD/install.sh\" | /bin/sh -s -- \"${INSTALL_DIR}\" \"${INSTALL_TAG}\""
exec sh -c "curl -fsSL \"https://raw.githubusercontent.com/matiboux/dockerc/HEAD/install.sh\" | /bin/sh -s -- --install-dir \"${INSTALL_DIR}\"${INSTALL_TAG_ARG}"
fi

exit ${ERROR_CODE:-0}
Expand Down
150 changes: 104 additions & 46 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,79 +8,135 @@
# This project is not affiliated with Docker, Inc.

ERROR_CODE=''
DOCKERC_PARSE_ARGUMENTS='true'

# Parse options arguments
while [ "$DOCKERC_PARSE_ARGUMENTS" = 'true' ] && [ "$#" -gt 0 ]; do
# Parse arguments
# Dummy while loop to allow breaking
while true; do

case "$1" in
# Parse options arguments
while [ "$#" -gt 0 ]; do

'--help' | '-h' )
DOCKERC_PRINT_HELP='true'
shift
;;
case "$1" in

'--install-dir' | '-i' )
shift
# Get installation directory argument
if [ -z "$1" ]; then
echo 'Error: Missing installation directory.' >&2
'--help' | '-h' )
# Print help
DOCKERC_PRINT_HELP='true'
ERROR_CODE=1
# Stop parsing arguments
DOCKERC_PARSE_ARGUMENTS='false'
shift
;;

'--install-dir' | '-i' )
shift
# Check for installation directory argument
if [ -z "$1" ]; then
echo 'Error: Missing installation directory.' >&2
DOCKERC_PRINT_HELP='true'
ERROR_CODE=1
break
fi
# Set installation directory
DOCKERC_INSTALL_DIR="$1"
shift
;;

* )
break
fi
DOCKERC_INSTALL_DIR="$1"
shift
;;
;;

* )
# Unknown option, maybe first argument
# Stop parsing options
break
;;
esac

esac
done

done

if [ "$DOCKERC_PARSE_ARGUMENTS" = 'true' ]; then
# Parse positional arguments
if [ "$DOCKERC_PRINT_HELP" = 'true' ]; then
# Stop parsing arguments
break
fi

# Parse install tag optional argument
if [ "$#" -gt 0 ]; then
DOCKERC_INSTALL_TAG="$1"
shift
# Check for optional positional arguments
if [ "$#" -le 0 ]; then
# No more arguments
break
fi

fi
# Parse first optional positional argument
# Parse install tag argument
DOCKERC_INSTALL_TAG="$1"
shift

# Stop parsing arguments
break

done

if [ "$DOCKERC_PRINT_HELP" = 'true' ]; then
# Print help & exit
echo "Usage: $0 [options] [tag]"
echo "Usage: $0 --update [options] [tag]"
echo ''
echo 'Options:'
echo ' --help, -h Display this help message'
echo ' --install-dir, -i Installation directory (defaults to /usr/local/bin)'
echo ' -h, --help Print this help message'
echo ' -i, --install-dir Set installation directory'
echo " (defaults to directory '/usr/local/bin')"
echo ''
echo 'Arguments:'
echo ' tag DockerC tag/version to install (defaults to HEAD)'
echo ' tag DockerC tag/version to install'
echo ' (defaults to latest version)'
exit ${ERROR_CODE:-0}
fi

# Get installation directory
# Get install directory
INSTALL_DIR='/usr/local/bin' # Default installation directory
if [ -n "$DOCKERC_INSTALL_DIR" ]; then
# Use from argument or environment variable
INSTALL_DIR="$DOCKERC_INSTALL_DIR"
fi

# Get install tag
INSTALL_TAG='HEAD' # Default required tag
INSTALL_TAG='latest'
if [ -n "$DOCKERC_INSTALL_TAG" ]; then
# Use from argument or environment variable
INSTALL_TAG="$DOCKERC_INSTALL_TAG"
if
[ "$DOCKERC_INSTALL_TAG" = 'HEAD' ] || [ "$DOCKERC_INSTALL_TAG" = 'head' ] ||
[ "$DOCKERC_INSTALL_TAG" = '.' ] || [ "$DOCKERC_INSTALL_TAG" = '-' ]
then
INSTALL_TAG='HEAD'
else
INSTALL_TAG="$DOCKERC_INSTALL_TAG"
fi
fi

if [ ! -d "$INSTALL_DIR" ]; then
echo "Error: Install directory '$INSTALL_DIR' does not exist." >&2
exit 1
fi

get_latest_version() {
# Syntax: '"tag_name": "v1.0.0",'
LATEST_VERSION_JSON=$(
curl -fsSL "https://api.github.com/repos/matiboux/dockerc/releases/latest" 2>/dev/null \
| grep -Eo '"tag_name": "(.+?)",'
)

if [ -n "$LATEST_VERSION_JSON" ]; then
# Extract version from matched string
# Remove `"tag_name": "` (13 characters) and `",` (2 characters)
if [ "$(uname -s)" = 'Darwin' ]; then
# MacOS
LATEST_VERSION="${LATEST_VERSION_JSON:13:$((${#LATEST_VERSION_JSON}-13-2))}"
else
# Linux
LATEST_VERSION=$(expr substr "$LATEST_VERSION_JSON" $(expr 1 + 13) $(expr length "$LATEST_VERSION_JSON" - 13 - 2))
fi

echo "$LATEST_VERSION"
fi
}

if [ "$INSTALL_TAG" = 'latest' ]; then
# Default install tag is latest released version
INSTALL_TAG="$(get_latest_version)"
if [ -z "$INSTALL_TAG" ]; then
echo 'Error: Failed to get DockerC latest version.' >&2
exit 1
fi
fi

# Check that docker is installed
Expand All @@ -97,15 +153,17 @@ if [ $? -ne 0 ]; then
exit 1
fi

curl -fsSL "https://raw.githubusercontent.com/matiboux/dockerc/$INSTALL_TAG/dockerc" -o "$INSTALL_DIR/dockerc"
if [ $? -ne 0 ]; then
echo 'Error: DockerC installation failed.' >&2
curl -fsSL "https://raw.githubusercontent.com/matiboux/dockerc/$INSTALL_TAG/dockerc" 2>/dev/null \
-o "$INSTALL_DIR/dockerc"
CURL_CODE=$?
if [ "$CURL_CODE" -ne 0 ]; then
echo "Error: Failed to download DockerC ($INSTALL_TAG) in '$INSTALL_DIR' (curl error $CURL_CODE)." >&2
exit 1
fi

chmod +x "$INSTALL_DIR/dockerc"
if [ $? -ne 0 ]; then
echo 'Error: Failed to add execute permission.' >&2
echo 'Error: Failed to add execute permission to DockerC.' >&2
exit 1
fi

Expand Down