|
176 | 176 | # Helper Functions #
|
177 | 177 | ####################
|
178 | 178 |
|
| 179 | + # The global list of containers defined in the compose stack |
| 180 | + # |
| 181 | + # TODO: Parse container-compose.yml and read in valid service containers |
| 182 | + # instead of hardcoding them here |
| 183 | + # |
| 184 | + containers=('app' 'database') |
| 185 | + |
179 | 186 | function validateContainer {
|
180 | 187 | local args=("$@")
|
181 | 188 |
|
182 |
| - local containers=('app' 'database') |
183 | 189 | local targetContainer="${args[0]}"
|
184 | 190 |
|
185 | 191 | # Validate the targetContainer name
|
186 | 192 | if [[ " ${containers[*]} " =~ [[:space:]]${targetContainer}[[:space:]] ]]; then
|
187 | 193 | return 0
|
188 | 194 | else
|
189 |
| - error "Error! Invalid container: $targetContainer" |
190 |
| - error "Valid containers: ${containers[*]}" |
191 | 195 | return 1
|
192 | 196 | fi
|
193 | 197 | }
|
|
264 | 268 |
|
265 | 269 | function build {
|
266 | 270 | local args=("$@")
|
267 |
| - local targetContainer="${args[0]}" |
| 271 | + local targetContainer="${args[0]}" # TODO: Support multiple containers |
268 | 272 |
|
269 | 273 | # Check if a targetContainer is supplied;
|
270 | 274 | # otherwise, all containers will be built
|
|
273 | 277 | validateContainer "$targetContainer"
|
274 | 278 | # Check if a validation error occurred
|
275 | 279 | if [[ "$?" -ne 0 ]]; then
|
| 280 | + error "Error! Invalid container: $targetContainer" |
| 281 | + error "Valid containers: ${containers[*]}" |
| 282 | + |
276 | 283 | return "$?"
|
277 | 284 | fi
|
278 | 285 | # "Shift out" the targetContainer arg from the list of args
|
|
294 | 301 | #########
|
295 | 302 | # start #
|
296 | 303 | #########
|
297 |
| - |
| 304 | + |
| 305 | + # Either `compose start`s a set of existing containers given their stack service names as args, |
| 306 | + # or `compose up`s the entire stack. Use -- to seperate between list of containers and command args. |
| 307 | + # |
| 308 | + # Note: If `compose up` is determined for use, all args will be passed to that command instaed |
| 309 | + # |
| 310 | + # Note: `compose start` only starts existing stopped containers, |
| 311 | + # while `compose up` creates them from the stack |
| 312 | + # |
298 | 313 | function start {
|
299 |
| - podman-compose up |
300 |
| - return "$?" |
| 314 | + local args=("$@") |
| 315 | + |
| 316 | + # Short circut no args to compose-up |
| 317 | + if [[ -z "${args[@]}" ]]; then |
| 318 | + podman-compose up "${args[@]}" |
| 319 | + return "$?" |
| 320 | + fi |
| 321 | + |
| 322 | + # Seperate passed container args from passed command args, whether applicable or not. |
| 323 | + # |
| 324 | + # For this function, only the compose-start command requires such a distinction, |
| 325 | + # but the function determines whether to compose-start or compose-up depending on |
| 326 | + # the targetContainers supplied anyways. |
| 327 | + # |
| 328 | + # While doing all the work upfront maybe wasteful, it's more readable at the moment |
| 329 | + |
| 330 | + local separatorHasBeenFound=false |
| 331 | + |
| 332 | + local argsList1=() # targetContainer args |
| 333 | + local argsList2=() # args for the command |
| 334 | + |
| 335 | + for currentArg in "${args[@]}"; do |
| 336 | + [[ "$currentArg" == '--' ]] |
| 337 | + local argIsSeperator="$?" |
| 338 | + |
| 339 | + if [[ "$separatorHasBeenFound" = false && "$argIsSeperator" -eq 0 ]]; then |
| 340 | + separatorHasBeenFound=true |
| 341 | + elif [[ "$separatorHasBeenFound" = false ]]; then |
| 342 | + # No seperator yet; arg belongs in the first bucket |
| 343 | + argsList1+=("$currentArg") |
| 344 | + else |
| 345 | + # Seperator found; arg belongs in the second bucket |
| 346 | + argsList2+=("$currentArg") |
| 347 | + fi |
| 348 | + done |
| 349 | + |
| 350 | + # In an attempt to compose-start, look for at least one valid container from the targetContainers |
| 351 | + # otherwise, the container stack will be upped, and both argsLists will be passed to that command |
| 352 | + |
| 353 | + # Assume the first set of args are targetContainers |
| 354 | + local targetContainers="$argsList1" |
| 355 | + local validTargetContainers=() |
| 356 | + |
| 357 | + for targetContainer in "$targetContainers"; do |
| 358 | + |
| 359 | + validateContainer "$targetContainer" |
| 360 | + # If validation was successful, add the container to the set of validTargetContainers for processing; |
| 361 | + if [[ "$?" -eq 0 ]]; then |
| 362 | + validTargetContainers+="$targetContainer" |
| 363 | + else # otherwise, indicate its invalidity and omit it from the list of validTargetContainers |
| 364 | + error "Invalid container: $targetContainer" |
| 365 | + fi |
| 366 | + done |
| 367 | + |
| 368 | + if (( ${#validTargetContainers[@]} != 0 )); then |
| 369 | + |
| 370 | + # Valid targetContainers found; |
| 371 | + # gather args and compose-start |
| 372 | + local commandArgs="$argsList2" |
| 373 | + podman-compose start "${validTargetContainers[@]}" "${commandArgs[@]}" |
| 374 | + return "$?" |
| 375 | + |
| 376 | + else |
| 377 | + |
| 378 | + # No valid targetContainers to start supplied; |
| 379 | + # compose-up instead, using all supplied args |
| 380 | + argsList1+=("${argsList2[@]}") |
| 381 | + podman-compose up "${args[@]}" |
| 382 | + return "$?" |
| 383 | + |
| 384 | + fi |
301 | 385 | }
|
| 386 | + alias up='start' |
302 | 387 | alias default='start'
|
303 | 388 | alias run='default' # TODO: Make a TASKFILE_RUN_RUNS_DEFAULT=1 flag that automatically runs the
|
304 | 389 | # "default" task if no args are supplied to run. That way, we optionally
|
|
334 | 419 | ########
|
335 | 420 |
|
336 | 421 | function down {
|
337 |
| - podman-compose down |
338 |
| - return "$?" |
| 422 | + local args=("$@") |
| 423 | + local targetContainer="${args[0]}" # TODO: Support multiple containers |
| 424 | + |
| 425 | + # Check if a targetContainer is supplied; |
| 426 | + # otherwise, all containers will be downed |
| 427 | + if [[ -n "$targetContainer" ]] then |
| 428 | + |
| 429 | + validateContainer "$targetContainer" |
| 430 | + # Check if a validation error occurred |
| 431 | + if [[ "$?" -ne 0 ]]; then |
| 432 | + error "Error! Invalid container: $targetContainer" |
| 433 | + error "Valid containers: ${containers[*]}" |
| 434 | + |
| 435 | + return "$?" |
| 436 | + fi |
| 437 | + # "Shift out" the targetContainer arg from the list of args |
| 438 | + args=("${args[@]:1}") |
| 439 | + |
| 440 | + podman-compose down "$targetContainer" "${args[@]}" |
| 441 | + return "$?" |
| 442 | + |
| 443 | + else |
| 444 | + |
| 445 | + # Down the stack |
| 446 | + podman-compose down "${args[@]}" |
| 447 | + return "$?" |
| 448 | + |
| 449 | + fi |
339 | 450 | }
|
340 | 451 | alias clean='down'
|
341 | 452 |
|
|
350 | 461 | validateContainer "$targetContainer"
|
351 | 462 | # Check if a validation error occurred
|
352 | 463 | if [[ "$?" -ne 0 ]]; then
|
| 464 | + error "Error! Invalid container: $targetContainer" |
| 465 | + error "Valid containers: ${containers[*]}" |
| 466 | + |
353 | 467 | return "$?"
|
354 | 468 | fi
|
355 | 469 | # "Shift out" the targetContainer arg from the list of args
|
|
367 | 481 | function restart {
|
368 | 482 | stop
|
369 | 483 | down
|
370 |
| - start |
| 484 | + start #TODO: Use alias 'up' when it works |
371 | 485 | return "$?"
|
372 | 486 | }
|
373 | 487 |
|
|
0 commit comments