-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbashpp-oci
More file actions
executable file
·75 lines (67 loc) · 2.96 KB
/
bashpp-oci
File metadata and controls
executable file
·75 lines (67 loc) · 2.96 KB
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
#!/usr/bin/env bash
#
# Copyright 2024-2025 The Linux Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# bashpp-oci
# Extension handler for downloading packages from OCI container images.
#
# Description:
# This script serves as an extension handler for bashpp to pull packages from OCI container
# images. When bashpp encounters an OCI repository URL (default or oci:// prefix), it delegates
# the package retrieval to this handler. The handler pulls the package, extracts the specified
# include file, and installs it to the appropriate directory. It supports nested package paths
# and maintains proper directory structure.
#
# Usage:
# bashpp-oci <repository> <include>
# Called automatically by bashpp when processing OCI repository URLs
#
# Parameters:
# repository - The OCI repository containing the package image
# include - The path to the file to include, in the format "package/function" or
# "package/subpackage/function" for nested paths
#
# Outputs:
# No direct output, but creates files in the include directory and updates .gitignore
# if running within a git repository.
#
# Note:
# Requires an OCI client (docker by default) to be installed.
# The OCI client can be configured using the OCI_CLIENT environment variable.
# The image is expected to be tagged as 'latest' and follow the naming convention
# 'repository/bashp-package:latest'.
# Supports nested package paths and maintains proper directory structure.
# Git integration has been improved to handle nested paths correctly.
declare -r INCLUDE_DIR=${INCLUDE_DIR:-${INCLUDE_DIR:?must be set}}
declare -r repository=${1:-${repository:?must be set}}
declare -r include=${2:-${include:?must be set}}
declare -r ociClient=${OCI_CLIENT:-docker}
command -v "$ociClient" &>/dev/null || exit 1
declare -r package=${include%%/*}
declare -r image="$repository/bashp-$package:latest"
if "$ociClient" manifest inspect "$image" &>/dev/null; then
mkdir -p "$INCLUDE_DIR/$(dirname "$include")"
id=$("$ociClient" create "$image" none)
declare -r id
# shellcheck disable=SC2064
trap "'$ociClient' rm '$id' >/dev/null" EXIT
"$ociClient" cp "$id:/$include" "$INCLUDE_DIR/$include"
if command -v git &>/dev/null && git rev-parse --is-inside-work-tree &>/dev/null; then
declare -r function=${include#*/}
declare -r gitignore="$INCLUDE_DIR/$package/.gitignore"
grep -q "^$function$" "$INCLUDE_DIR/$package/.gitignore" 2>/dev/null ||
echo "$function" >> "$gitignore"
fi
fi