epm-search 4.48 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
local CMD
26
local string="$*"
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
epm play $short --list-all | sed -e 's|^ *||g' -e 's|[[:space:]]\+| |g' -e "s|\$| (use \'epm play\' to install it)|"
125 126 127 128 129 130
}

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

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

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

150 151 152 153 154 155 156 157 158 159
	# only apt supports regexps?
	case $PMTYPE in
		apt-*)
			;;
		*)
				list=$(echo "$list" | sed -e "s/[?\^.]/ /g")
				listN=$(echo "$listN" | sed -e "s/[?\^.]/ /g")
			;;
	esac

160
	if [ -n "$short" ] ; then
161 162 163
		echon " | sed -e \"s| .*||g\""
	fi

Vitaly Lipatov's avatar
Vitaly Lipatov committed
164
	[ -n "$listN" ] && echon " | grep -E -i -v -- \"$listN\""
165 166 167

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

169 170 171 172
	# 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?
Vitaly Lipatov's avatar
Vitaly Lipatov committed
173
			echon " | grep -E -i -- \"$i\""
174
		done
175 176 177 178 179
	fi

	# FIXME: move from it
	#isatty || return

180 181
	# TODO: sorts word by length from large to short

182 183
	local COLO=""
	# rule for colorife
184
	for i in $list $listN; do
185 186 187
		[ -n "$COLO" ] && COLO="$COLO|"
		COLO="$COLO$i"
	done
188

189
	# TODO: use some colorifer instead grep (check grep adove too)
190
	if [ -n "$list" ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
191
		echon " | grep -E -i $EGREPCOLOR -- \"($COLO)\""
192
	fi
193 194
}

195 196
__epm_search_internal()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
197
	[ -n "$pkg_filenames" ] || fatal "Search: search argument(s) is missed"
198 199 200 201 202 203 204 205 206

	# it is useful for first time running
	update_repo_if_needed soft

	warmup_bases

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

207
# copied from korinf/tools/run-script/scripts/search
208

209 210
epm_search()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
211
	[ -n "$pkg_filenames" ] || fatal "Search: search argument(s) is missed"
212

213
	# it is useful for first time running
214
	update_repo_if_needed soft
215

216 217
	warmup_bases

218
	# FIXME: do it better
Vitaly Lipatov's avatar
Vitaly Lipatov committed
219 220
	local MGS
	MGS=$(eval __epm_search_make_grep $quoted_args)
221
	EXTRA_SHOWDOCMD="$MGS"
222
	eval "__epm_search_output \"$(eval get_firstarg $quoted_args)\" $MGS"
223
}