epm-search 4.18 KB
Newer Older
1 2
#!/bin/sh
#
3 4
# Copyright (C) 2012, 2013, 2016-2020  Etersoft
# Copyright (C) 2012, 2013, 2016-2020  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
	packagekit)
38 39
		CMD="pkcon search name"
		;;
40
	urpm-rpm)
41 42
		# urpmq does not support --
		CMD="urpmq -y"
43
		;;
44
	pkgsrc)
45
		CMD="pkg_info -x --"
46
		;;
47 48 49
	pkgng)
		CMD="pkg search -i --"
		;;
50
	emerge)
51
		CMD="emerge --search --"
52
		;;
53
	pacman)
54
		CMD="pacman -Ss --"
55
		;;
56
	aura)
57
		CMD="aura -As --"
58
		;;
59
	yum-rpm)
60
		CMD="yum search --"
61
		;;
62
	dnf-rpm)
63
		CMD="dnf search --"
64
		;;
65
	zypper-rpm)
66
		CMD="zypper search -d --"
67 68 69 70
		;;
	mpkg)
		CMD="mpkg search"
		;;
71 72 73
	apk)
		CMD="apk search"
		;;
74 75 76
	tce)
		CMD="tce-ab"
		;;
77 78 79
	conary)
		CMD="conary repquery"
		;;
80
	npackd)
81
		docmd npackdcl search --query="$string" --status=all
Vitaly Lipatov's avatar
Vitaly Lipatov committed
82
		return
83
		;;
84 85 86
	chocolatey)
		CMD="chocolatey list"
		;;
87
	slackpkg)
88
		# FIXME
89
		echo "Note: case sensitive search"
90 91 92 93 94 95
		if [ -n "$verbose" ] ; then
			CMD="/usr/sbin/slackpkg search"
		else
			LANG=C docmd /usr/sbin/slackpkg search $string | grep " - " | sed -e 's|.* - ||g'
			return
		fi
96
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
97 98 99
	opkg)
		CMD="opkg find"
		;;
100 101 102
	homebrew)
		CMD="brew search"
		;;
103 104 105
	guix)
		CMD="guix package -A"
		;;
106 107 108
	android)
		CMD="pm list packages"
		;;
109 110 111
	aptcyg)
		CMD="apt-cyg searchall"
		;;
112 113 114
	xbps)
		CMD="xbps-query -s"
		;;
115 116 117
	appget|winget)
		CMD="$PMTYPE search"
		;;
118
	*)
119
		fatal "Have no suitable search command for $PMTYPE"
120 121 122
		;;
esac

123
LANG=C docmd $CMD $string
124 125 126 127 128 129
}

# produce grep sequence
__epm_search_make_grep()
{
	local i
130 131 132 133
	[ -z "$*" ] && return

	local list=
	local listN=
134
	for i in $@ ; do
135
		case "$i" in
136 137
			~*)
				# will clean from ~ later (and have the bug here with empty arg if run with one ~ only)
138 139 140 141 142 143
				listN="$listN $i"
				;;
			*)
				list="$list $i"
				;;
		esac
144 145 146
	done

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

149
	if [ -n "$short" ] ; then
150 151 152
		echon " | sed -e \"s| .*||g\""
	fi

153
	[ -n "$listN" ] && echon " | egrep -i -v -- \"$listN\""
154 155 156

	# 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
157

158 159 160 161 162 163
	# 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
164 165 166 167 168
	fi

	# FIXME: move from it
	#isatty || return

169 170
	# TODO: sorts word by length from large to short

171 172
	local COLO=""
	# rule for colorife
173
	for i in $list $listN; do
174 175 176
		[ -n "$COLO" ] && COLO="$COLO|"
		COLO="$COLO$i"
	done
177

178
	# TODO: use some colorifer instead grep (check grep adove too)
179
	if [ -n "$list" ] ; then
180
		echon " | egrep -i $EGREPCOLOR -- \"($COLO)\""
181
	fi
182 183
}

184 185
__epm_search_internal()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
186
	[ -n "$pkg_filenames" ] || fatal "Search: search argument(s) is missed"
187 188 189 190 191 192 193 194 195

	# it is useful for first time running
	update_repo_if_needed soft

	warmup_bases

	__epm_search_output $(get_firstarg $pkg_filenames) | grep "$pkg_filenames"
}

196
# copied from korinf/tools/run-script/scripts/search
197

198 199
epm_search()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
200
	[ -n "$pkg_filenames" ] || fatal "Search: search argument(s) is missed"
201

202
	# it is useful for first time running
203
	update_repo_if_needed soft
204

205 206
	warmup_bases

207
	# FIXME: do it better
Vitaly Lipatov's avatar
Vitaly Lipatov committed
208 209
	local MGS
	MGS=$(eval __epm_search_make_grep $quoted_args)
210
	EXTRA_SHOWDOCMD="$MGS"
211
	eval "__epm_search_output \"$(eval get_firstarg $quoted_args)\" $MGS"
212
}