support options --json and --quiet

This commit is contained in:
Tilman Kranz 2025-05-30 22:07:53 +02:00
parent 75de33bb30
commit 0ad7f26102
3 changed files with 119 additions and 20 deletions

View file

@ -36,6 +36,9 @@ environment variable `XDG_DATA_DIRS`.
- `-v`, `--verbose`: Print directives in found .desktop files.
- `-j`, `--json`: Print .desktop files found and directives to display
as JSON data structure (see also EXAMPLES below).
- `-d STRING`, `--show-directive STRING`: Instead of printing all directives
when searching in verbose mode, print only the directive given by `STRING`
(e.g. "Name"). Option can be repeated to print multiple directives.
@ -47,17 +50,37 @@ environment variable `XDG_DATA_DIRS`.
## RETURN VALUES
The command returns 0 if .desktop files were found that match the specified
criteria, 1 on usage error and 2 if no matching .desktop files were found.
criteria, 1 if no matching .desktop files were found and 127 on usage error.
## EXAMPLE
## EXAMPLES
Search for the string "firefox" in all .desktop files found in the path
specified by `XDG_DATA_DIRS`. To match, the string "firefox" must be contained
in either the "Exec" or the "Comment" directive. List the matching files, also
show the "Exec" and "TryExec" directives in each found file.
`xdg-desktop-search -s Exec -s Comment -v -d Exec -d TryExec firefox`
Search for .desktop files containing GVim in their "Name" directive and
determine the value of the "Exec" directive in the files found. Display
results as JSON data structure:
`xdg-desktop-search -j -d Exec -s Name GVim`
Result:
```
xdg-desktop-search -s Exec -s Comment -v -d Exec -d TryExec firefox
[
{
"desktop": "/usr/share/applications/gvim.desktop",
"results": [
{
"directive": "Exec",
"value": "gvim -f %F"
}
]
}
]
```
## AUTHOR AND COPYRIGHT
@ -70,7 +93,7 @@ xdg-desktop-search is distributed under the terms and conditions of the MIT
license <https://opensource.org/license/mit>.
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
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
@ -79,7 +102,7 @@ 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
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

View file

@ -7,10 +7,19 @@
# Configuration
IFS=":"
xdg_data_dirs=${XDG_DATA_DIRS:-/usr/share/gnome:$HOME/.local/share/flatpak/exports/share:/var/lib/flatpak/exports/share:/usr/local/share:/usr/share}
xdg_data_dirs_default_array=(
"/usr/share/gnome"
"$HOME/.local/share/flatpak/exports/share"
"/var/lib/flatpak/exports/share"
"/usr/local/share"
"/usr/share"
"/var/lib/snapd/desktop"
)
search_regex=".*"
help=false
json=false
verbose=false
quiet=false
show_directives_regex=".*"
search_directives_regex="(Name|Exec)"
@ -55,8 +64,14 @@ environment variable `XDG_DATA_DIRS`.
- `-h`, `--help`: Print this message and exit.
- `-q`, `--quiet`: Suppress warnings about inconsistenties such as
missing directories or malformed entries in .desktop files.
- `-v`, `--verbose`: Print directives in found .desktop files.
- `-j`, `--json`: Print .desktop files found and directives to display
as JSON data structure (see also EXAMPLES below).
- `-d STRING`, `--show-directive STRING`: Instead of printing all directives
when searching in verbose mode, print only the directive given by `STRING`
(e.g. "Name"). Option can be repeated to print multiple directives.
@ -70,15 +85,35 @@ environment variable `XDG_DATA_DIRS`.
The command returns 0 if .desktop files were found that match the specified
criteria, 1 if no matching .desktop files were found and 127 on usage error.
## EXAMPLE
## EXAMPLES
Search for the string "firefox" in all .desktop files found in the path
specified by `XDG_DATA_DIRS`. To match, the string "firefox" must be contained
in either the "Exec" or the "Comment" directive. List the matching files, also
show the "Exec" and "TryExec" directives in each found file.
```shell
xdg-desktop-search -s Exec -s Comment -v -d Exec -d TryExec firefox
`xdg-desktop-search -s Exec -s Comment -v -d Exec -d TryExec firefox`
Search for .desktop files containing GVim in their "Name" directive and
determine the value of the "Exec" directive in the files found. Display
results as JSON data structure:
`xdg-desktop-search -j -d Exec -s Name GVim`
Result:
```
[
{
"desktop": "/usr/share/applications/gvim.desktop",
"results": [
{
"directive": "Exec",
"value": "gvim -f %F"
}
]
}
]
```
## AUTHOR AND COPYRIGHT
@ -91,7 +126,7 @@ xdg-desktop-search is distributed under the terms and conditions of the MIT
license <https://opensource.org/license/mit>.
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
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
@ -100,7 +135,7 @@ 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
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
@ -133,6 +168,12 @@ parse_options() {
-h|--help)
help=true
;;
-q|--quiet)
quiet=true
;;
-j|--json)
json=true
;;
-v|--verbose)
verbose=true
;;
@ -172,8 +213,14 @@ valid_data_dirs() {
local result=()
for dir in $dirs ; do
if [[ -d $dir/applications ]] && [[ -r $dir/applications ]] ; then
result+=( "$dir" )
if ! [[ -d $dir/applications ]] ; then
"$quiet" || echo "WARNING: No \"applications\" subdirectory found in $dir; skipping." >&2
else
if ! [[ -r $dir/applications ]] ; then
"$quiet" || echo "WARNING: Unable to read \"applications\" subdirectory found in $dir; skipping." >&2
else
result+=( "$(realpath --canonicalize-missing --no-symlinks "$dir")" )
fi
fi
done
@ -184,13 +231,22 @@ valid_data_dirs() {
# Main Program
parse_options "$@"
# shellcheck disable=SC2153
xdg_data_dirs=${XDG_DATA_DIRS:-$(join ":" "${xdg_data_dirs_default_array[@]}")}
"$help" && { usage ; exit 0 ; }
"$help" && {
usage ;
exit 0 ;
}
"$json" && "$verbose" && {
echo "ERROR: \"--json\" and \"--verbose\" are mutually exclusive; aborted." >&2 ;
exit 127 ;
}
found=0
for _data_dir in $(valid_data_dirs "$xdg_data_dirs") ; do
data_dir=$(realpath --canonicalize-missing --no-symlinks "$_data_dir")
for data_dir in $(valid_data_dirs "$xdg_data_dirs") ; do
applications="$data_dir/applications"
[[ -d $applications ]] && for desktop in "$applications"/*.desktop ; do
@ -199,6 +255,13 @@ for _data_dir in $(valid_data_dirs "$xdg_data_dirs") ; do
if "$verbose" ; then
echo "# $desktop"
grep -Ei "$show_directives_regex" "$desktop"
elif "$json" ; then
while read -r grep_result ; do
# shellcheck disable=SC2001
value=$(echo "$grep_result" | sed -e 's/"/\\"/g;s/\(.*\)=\(.*\)/"directive": "\1", "value": "\2"/')
grep_results+=("{$value}")
done < <(grep -Ei "$show_directives_regex" "$desktop")
json_results+=( "{\"desktop\": \"$desktop\", \"results\": [$(join ',' "${grep_results[@]}")]}" )
else
echo "$desktop"
fi
@ -206,6 +269,9 @@ for _data_dir in $(valid_data_dirs "$xdg_data_dirs") ; do
done
done
#"$json" && for json_result in "${json_results[@]}" ; do echo "$json_result" ; done
"$json" && echo "[$(join ',' "${json_results[@]}")]"
if [[ $found -gt 0 ]] ; then
exit 0
else

View file

@ -23,23 +23,33 @@ The path of XDG directories searched can be modified by altering the environment
.IP "\[ci]" 4
\fB\-v\fR, \fB\-\-verbose\fR: Print directives in found \.desktop files\.
.IP "\[ci]" 4
\fB\-j\fR, \fB\-\-json\fR: Print \.desktop files found and directives to display as JSON data structure (see also EXAMPLES below)\.
.IP "\[ci]" 4
\fB\-d STRING\fR, \fB\-\-show\-directive STRING\fR: Instead of printing all directives when searching in verbose mode, print only the directive given by \fBSTRING\fR (e\.g\. "Name")\. Option can be repeated to print multiple directives\.
.IP "\[ci]" 4
\fB\-s STRING\fR, \fB\-\-search\-directive STRING\fR: Instead of searching directives "Name" and "Exec" for strings containing \fBSEARCH\fR, search the given directive instead (e\.g\. "Comment")\. Option can be repeated to search in multiple directives\.
.IP "" 0
.SH "RETURN VALUES"
The command returns 0 if \.desktop files were found that match the specified criteria, 1 on usage error and 2 if no matching \.desktop files were found\.
.SH "EXAMPLE"
The command returns 0 if \.desktop files were found that match the specified criteria, 1 if no matching \.desktop files were found and 127 on usage error\.
.SH "EXAMPLES"
Search for the string "firefox" in all \.desktop files found in the path specified by \fBXDG_DATA_DIRS\fR\. To match, the string "firefox" must be contained in either the "Exec" or the "Comment" directive\. List the matching files, also show the "Exec" and "TryExec" directives in each found file\.
.P
\fBxdg\-desktop\-search \-s Exec \-s Comment \-v \-d Exec \-d TryExec firefox\fR
.P
Search for \.desktop files containing GVim in their "Name" directive and determine the value of the "Exec" directive in the files found\. Display results as JSON data structure:
.P
\fBxdg\-desktop\-search \-j \-d Exec \-s Name GVim\fR
.P
Result:
.P
\fB[ { "desktop": "/usr/share/applications/gvim\.desktop", "results": [ { "directive": "Exec", "value": "gvim \-f %F" } ] } ]\fR
.SH "AUTHOR AND COPYRIGHT"
Copyright \(co 2024 Tilman Kranz \fIt\.kranz@tk\-sls\.de\fR
.SH "LICENSE"
xdg\-desktop\-search is distributed under the terms and conditions of the MIT license \fIhttps://opensource\.org/license/mit\fR\.
.P
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software\(rs), 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:
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:
.P
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software\.
.P
THE SOFTWARE IS PROVIDED “AS IS\(rs, 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\.
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\.