Initial commit
commit
d780b45f0c
@ -0,0 +1,9 @@
|
|||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright © 2023 Ruakij
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
@ -0,0 +1,57 @@
|
|||||||
|
recursivefilecmd
|
||||||
|
==
|
||||||
|
|
||||||
|
Execute a command on files with a specific extension recursively in a directory tree.
|
||||||
|
|
||||||
|
<br>
|
||||||
|
|
||||||
|
# 1. Overview
|
||||||
|
## 1.1. Usage
|
||||||
|
|
||||||
|
```sh
|
||||||
|
./recursivefilecmd.sh <extension> <path> <command ..>
|
||||||
|
```
|
||||||
|
|
||||||
|
<br>
|
||||||
|
|
||||||
|
### 1.1.1. Arguments
|
||||||
|
|
||||||
|
- `extension`: The file extension to search for.
|
||||||
|
- `path`: The root directory to start searching for files.
|
||||||
|
- `command`: The command to execute on each file.
|
||||||
|
|
||||||
|
<br>
|
||||||
|
|
||||||
|
### 1.1.2. Examples
|
||||||
|
|
||||||
|
```sh
|
||||||
|
./recursivefilecmd.sh 'hcl' 'services/' nomad run -detach
|
||||||
|
```
|
||||||
|
|
||||||
|
```sh
|
||||||
|
./recursivefilecmd.sh 'yaml' 'config/' kubectl apply -f
|
||||||
|
```
|
||||||
|
|
||||||
|
<br>
|
||||||
|
|
||||||
|
## 1.2. Behaviour
|
||||||
|
|
||||||
|
The file-name including its path are added to the end of the given command.
|
||||||
|
|
||||||
|
All files are executed in-sequence.
|
||||||
|
|
||||||
|
When `path` is a file which matches the extension, only it is executed.<br>
|
||||||
|
When the extension doesnt match, the directory is used instead, walking up up to 3 times, searching for files matching the extension.
|
||||||
|
|
||||||
|
<br>
|
||||||
|
|
||||||
|
# 2. Development
|
||||||
|
## 2.1. Features
|
||||||
|
|
||||||
|
Maybe i'll add some more features, here a few i can think of:
|
||||||
|
|
||||||
|
- [ ] Convert project to golang
|
||||||
|
- [ ] Add options for optional things
|
||||||
|
- [ ] Replace `extension`-filter with a regex-match
|
||||||
|
- [ ] Make recursive optional
|
||||||
|
- [ ] Parameterize passed-filepath (e.g. # for all & #1 for regex-group 1)
|
@ -0,0 +1,45 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
extension=$1
|
||||||
|
path=$2
|
||||||
|
cmd="${@:3}"
|
||||||
|
|
||||||
|
execInDir() {
|
||||||
|
file=$1
|
||||||
|
|
||||||
|
# get folder
|
||||||
|
folder=$(dirname "$file")
|
||||||
|
# get local file
|
||||||
|
filename=$(basename "$file")
|
||||||
|
(
|
||||||
|
# switch to it
|
||||||
|
cd "$folder"
|
||||||
|
# exec cmd with filename behind it
|
||||||
|
$cmd $filename
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
# Check if path is file and matches extension
|
||||||
|
if [[ -f "$path" && "${path##*.}" = "$extension" ]]; then
|
||||||
|
execInDir $path
|
||||||
|
else
|
||||||
|
if [[ -f "$path" ]]; then
|
||||||
|
# Strip any filename
|
||||||
|
path=$(dirname "$path")
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Try 3 times
|
||||||
|
for i in {0..3}; do
|
||||||
|
# Get files with extension
|
||||||
|
files=$(find "$path" -iname "*.$extension")
|
||||||
|
if [[ "$files" = "" ]]; then
|
||||||
|
# If there are none, go up and search again
|
||||||
|
path="${path}/.."
|
||||||
|
else
|
||||||
|
for file in $files; do
|
||||||
|
execInDir $file
|
||||||
|
done
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
fi
|
Loading…
Reference in New Issue