Added find and find-first.

This commit is contained in:
2025-06-28 21:50:34 -07:00
parent 1efb518fdf
commit bee04ce498
4 changed files with 262 additions and 0 deletions

View File

@@ -0,0 +1,75 @@
name: find-first
description: "Find the first file matching a pattern and extract detailed information about it."
inputs:
path:
description: "Path to search in."
required: false
default: "."
pattern:
description: "Pattern to search for (supports glob patterns)."
required: true
type:
description: "Type of files to find (f=files, d=directories, l=links)."
required: false
default: "f"
maxDepth:
description: "Maximum depth to search."
required: false
default: ""
minDepth:
description: "Minimum depth to search."
required: false
default: ""
size:
description: "Size constraint (e.g., +100k, -1M)."
required: false
default: ""
modified:
description: "Modified time constraint (e.g., -1, +7)."
required: false
default: ""
additionalArgs:
description: "Additional arguments to pass to find command."
required: false
default: ""
outputs:
found:
description: "Whether a file was found (true/false)."
value: ${{ steps.extract.outputs.found }}
fullPath:
description: "Full path of the first found file."
value: ${{ steps.extract.outputs.fullPath }}
directory:
description: "Directory containing the file."
value: ${{ steps.extract.outputs.directory }}
directoryName:
description: "Name of the directory containing the file."
value: ${{ steps.extract.outputs.directoryName }}
fileName:
description: "Full filename with extension."
value: ${{ steps.extract.outputs.fileName }}
baseName:
description: "Filename without extension."
value: ${{ steps.extract.outputs.baseName }}
extension:
description: "File extension (without dot)."
value: ${{ steps.extract.outputs.extension }}
runs:
using: "composite"
steps:
- name: "Find files."
id: find
uses: act/common/utils/find@master
with:
path: ${{ inputs.path }}
pattern: ${{ inputs.pattern }}
type: ${{ inputs.type }}
maxDepth: ${{ inputs.maxDepth }}
minDepth: ${{ inputs.minDepth }}
size: ${{ inputs.size }}
modified: ${{ inputs.modified }}
additionalArgs: ${{ inputs.additionalArgs }}
- name: "Extract information about first file."
id: extract
run: sh "${{ github.action_path }}/find-first.sh" "${{ steps.find.outputs.files }}"
shell: bash

View File

@@ -0,0 +1,70 @@
#!/bin/bash
# Get the files list from the find output
FILES_LIST="$1"
if [[ ! -f "$GITHUB_OUTPUT" ]]; then
# Write to stdout if the output file doesn't exist.
GITHUB_OUTPUT="/dev/fd/1"
fi
# Function to output file information
function output_file_info
{
local found="$1"
local full_path="$2"
local directory="$3"
local directory_name="$4"
local file_name="$5"
local base_name="$6"
local extension="$7"
echo "found=$found" >> "$GITHUB_OUTPUT"
echo "fullPath=$full_path" >> "$GITHUB_OUTPUT"
echo "directory=$directory" >> "$GITHUB_OUTPUT"
echo "directoryName=$directory_name" >> "$GITHUB_OUTPUT"
echo "fileName=$file_name" >> "$GITHUB_OUTPUT"
echo "baseName=$base_name" >> "$GITHUB_OUTPUT"
echo "extension=$extension" >> "$GITHUB_OUTPUT"
}
# Check if any files were found
if [[ -z "$FILES_LIST" ]]; then
output_file_info "false"
exit 0
fi
# Get the first file from the list
FIRST_FILE=$(echo "$FILES_LIST" | head -n 1)
if [[ -z "$FIRST_FILE" ]]; then
output_file_info "false"
exit 0
fi
# Extract path information
FULL_PATH="$FIRST_FILE"
DIRECTORY=$(dirname "$FULL_PATH")
DIRECTORY_NAME=$(basename "$DIRECTORY")
FILE_NAME=$(basename "$FULL_PATH")
# Extract filename parts
if [[ "$FILE_NAME" == *.* ]]; then
# File has an extension
BASE_NAME="${FILE_NAME%.*}"
EXTENSION="${FILE_NAME##*.}"
else
# File has no extension
BASE_NAME="$FILE_NAME"
EXTENSION=""
fi
# Handle special cases for directory names
if [[ "$DIRECTORY" == "." ]]; then
DIRECTORY_NAME="."
elif [[ "$DIRECTORY" == "/" ]]; then
DIRECTORY_NAME="/"
fi
# Output all the extracted information using the function
output_file_info "true" "$FULL_PATH" "$DIRECTORY" "$DIRECTORY_NAME" "$FILE_NAME" "$BASE_NAME" "$EXTENSION"