Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 4433998

Browse files
committedSep 17, 2024·
Added "search" action to "line_in_file" function
1 parent eb73040 commit 4433998

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed
 

‎docs/content/docs/3.project-templates/99.create-your-own-template.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ line_in_file [--file FILE] [--action ACTION] [ARGUMENTS...]
125125
| Parameter | Description | Required | Default |
126126
|-----------|-------------|----------|---------|
127127
| `--file FILE` | Specifies the target file(s) to modify. Can be used multiple times for multiple files. | Yes | N/A |
128-
| `--action ACTION` | Determines the operation to perform. Available actions: `ensure`, `replace`, `after`, `exact`. | No | `ensure` |
128+
| `--action ACTION` | Determines the operation to perform. Available actions: `ensure`, `replace`, `after`, `exact`, `search`. | No | `ensure` |
129129
| `ARGUMENTS` | The content to manipulate, which varies based on the chosen action. | Yes | N/A |
130130

131131
#### Actions
@@ -136,6 +136,7 @@ line_in_file [--file FILE] [--action ACTION] [ARGUMENTS...]
136136
| `replace` | Replaces a line containing specific content with a new line. | Two arguments: search content and replacement line. | Appends the replacement line to the file. |
137137
| `after` | Inserts a new line after a line containing specific content. | Two arguments: search content and line to insert. | Appends both the search content and new line to the file. |
138138
| `exact` | Performs an exact replacement of text within the file. | Two arguments: exact text to replace and replacement text. | Returns an error. |
139+
| `search` | Searches for specified content in the file. | One argument: content to search for. | Returns false (exit code 1). |
139140

140141
#### Usage Examples
141142

‎lib/functions.sh

+8-4
Original file line numberDiff line numberDiff line change
@@ -642,16 +642,13 @@ line_in_file() {
642642
return 1
643643
fi
644644

645-
if [[ ${#args[@]} -eq 0 ]]; then
645+
if [[ ${#args[@]} -eq 0 && "$action" != "search" ]]; then
646646
echo "Error: No content specified" >&2
647647
return 1
648648
fi
649649

650650
# Process each file
651651
for file in "${files[@]}"; do
652-
# Create file if it doesn't exist
653-
[[ -f "$file" ]] || touch "$file"
654-
655652
case $action in
656653
ensure)
657654
for line in "${args[@]}"; do
@@ -700,6 +697,13 @@ ${args[1]}" "$file"
700697
return 1
701698
fi
702699
;;
700+
search)
701+
if grep -qF -- "${args[0]}" "$file"; then
702+
return 0 # True, content found
703+
else
704+
return 1 # False, content not found
705+
fi
706+
;;
703707
*)
704708
echo "Error: Invalid action '$action'" >&2
705709
return 1

0 commit comments

Comments
 (0)
Please sign in to comment.