epm-search 3.67 KB
Newer Older
1 2
#!/bin/sh
#
3 4
# Copyright (C) 2012, 2013, 2016-2017  Etersoft
# Copyright (C) 2012, 2013, 2016-2017  Vitaly Lipatov <lav@etersoft.ru>
5
#
6 7 8
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
9 10 11 12 13
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU Affero General Public License for more details.
15
#
16 17
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
18 19
#

20
load_helper epm-check_updated_repo
21
load_helper epm-sh-warmup
22

23
__epm_search_output()
24
{
25 26
local CMD
local string="$1"
27 28
case $PMTYPE in
	apt-rpm|apt-dpkg)
29
		CMD="apt-cache search --"
30
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
31 32 33 34 35 36
	aptitude-dpkg)
		CMD="aptitude search --"
		;;
	deepsolver-rpm)
		CMD="ds-require --"
		;;
37
	urpm-rpm)
38 39
		# urpmq does not support --
		CMD="urpmq -y"
40
		;;
41
	pkgsrc)
42
		CMD="pkg_info -x --"
43
		;;
44 45 46
	pkgng)
		CMD="pkg search -i --"
		;;
47
	emerge)
48
		CMD="emerge --search --"
49
		;;
50
	pacman)
51
		CMD="pacman -Ss --"
52
		;;
53
	aura)
54
		CMD="aura -As --"
55
		;;
56
	yum-rpm)
57
		CMD="yum search --"
58
		;;
59
	dnf-rpm)
60
		CMD="dnf search --"
61
		;;
62
	zypper-rpm)
63
		CMD="zypper search --"
64 65 66 67
		;;
	mpkg)
		CMD="mpkg search"
		;;
68 69 70
	apk)
		CMD="apk search"
		;;
71 72 73
	tce)
		CMD="tce-ab"
		;;
74 75 76
	conary)
		CMD="conary repquery"
		;;
77
	npackd)
78
		docmd npackdcl search --query="$string" --status=all
Vitaly Lipatov's avatar
Vitaly Lipatov committed
79
		return
80
		;;
81 82 83
	chocolatey)
		CMD="chocolatey list"
		;;
84
	slackpkg)
85
		# FIXME
86
		echo "Note: case sensitive search"
87
		CMD="/usr/sbin/slackpkg search"
88
		;;
89 90 91
	homebrew)
		CMD="brew search"
		;;
92 93 94
	guix)
		CMD="guix package -A"
		;;
95 96 97
	android)
		CMD="pm list packages"
		;;
98 99 100
	aptcyg)
		CMD="apt-cyg searchall"
		;;
101 102 103
	xbps)
		CMD="xbps-query -s"
		;;
104
	*)
105
		fatal "Have no suitable search command for $PMTYPE"
106 107 108
		;;
esac

109
LANG=C docmd $CMD $string
110 111 112 113 114 115
}

# produce grep sequence
__epm_search_make_grep()
{
	local i
116 117 118 119
	[ -z "$*" ] && return

	local list=
	local listN=
120
	for i in $@ ; do
121
		case "$i" in
122 123
			~*)
				# will clean from ~ later (and have the bug here with empty arg if run with one ~ only)
124 125 126 127 128 129
				listN="$listN $i"
				;;
			*)
				list="$list $i"
				;;
		esac
130 131 132
	done

	#list=$(strip_spaces $list | sed -e "s/ /|/g")
133
	listN=$(strip_spaces $listN | sed -e "s/ /|/g" | sed -e "s/~//g")
134

135
	if [ -n "$short" ] ; then
136 137 138
		echon " | sed -e \"s| .*||g\""
	fi

139
	[ -n "$listN" ] && echon " | egrep -i -v -- \"$listN\""
140 141 142

	# FIXME: The World has not idea how to do grep both string
	# http://stackoverflow.com/questions/10110051/grep-with-two-strings-logical-and-in-regex?rq=1
143

144 145 146 147 148 149
	# Need only if we have more than one word (with one word we will grep for colorify)
	if [ "$(echo "$list" | wc -w)" -gt 1 ] ; then
		for i in $list ; do
			# FIXME -n on MacOS?
			echon " | egrep -i -- \"$i\""
		done
150 151 152 153 154
	fi

	# FIXME: move from it
	#isatty || return

155 156
	# TODO: sorts word by length from large to short

157 158
	local COLO=""
	# rule for colorife
159
	for i in $list $listN; do
160 161 162
		[ -n "$COLO" ] && COLO="$COLO|"
		COLO="$COLO$i"
	done
163

164
	# TODO: use some colorifer instead grep (check grep adove too)
165
	if [ -n "$list" ] ; then
166
		echon " | egrep -i $EGREPCOLOR -- \"($COLO)\""
167
	fi
168 169 170
}

# copied from korinf/tools/run-script/scripts/search
171

172 173
epm_search()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
174
	[ -n "$pkg_filenames" ] || fatal "Search: missing search argument(s)"
175

176
	# it is useful for first time running
177
	update_repo_if_needed soft
178

179 180
	warmup_bases

181
	# FIXME: do it better
Vitaly Lipatov's avatar
Vitaly Lipatov committed
182 183
	local MGS
	MGS=$(eval __epm_search_make_grep $quoted_args)
184
	EXTRA_SHOWDOCMD="$MGS"
185
	eval "__epm_search_output \"$(eval get_firstarg $quoted_args)\" $MGS"
186
}