-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.sh
executable file
·170 lines (142 loc) · 3.87 KB
/
install.sh
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/bin/sh
# DockerC Installer
# Wrapper for docker compose commands in your project.
# https://github.com/matiboux/dockerc
# MIT License
# Copyright (c) 2023 Matiboux
# This project is not affiliated with Docker, Inc.
ERROR_CODE=''
# Parse arguments
# Dummy while loop to allow breaking
while true; do
# Parse options arguments
while [ "$#" -gt 0 ]; do
case "$1" in
'--help' | '-h' )
# Print help
DOCKERC_PRINT_HELP='true'
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
;;
esac
done
if [ "$DOCKERC_PRINT_HELP" = 'true' ]; then
# Stop parsing arguments
break
fi
# Check for optional positional arguments
if [ "$#" -le 0 ]; then
# No more arguments
break
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 ''
echo 'Options:'
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'
echo ' (defaults to latest version)'
exit ${ERROR_CODE:-0}
fi
# 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='latest'
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
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
docker --help > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo 'Error: Docker is not installed.' >&2
exit 1
fi
# Check that docker compose is installed
docker compose --help > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo 'Error: Docker compose is not installed.' >&2
exit 1
fi
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 to DockerC.' >&2
exit 1
fi
echo "DockerC ($INSTALL_TAG) installed successfully at '$INSTALL_DIR/dockerc'!"