epm-search 3.6 KB
Newer Older
1 2
#!/bin/sh
#
Vitaly Lipatov's avatar
Vitaly Lipatov committed
3 4
# Copyright (C) 2012,2013  Etersoft
# Copyright (C) 2012,2013  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 21
load_helper epm-check_updated_repo

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

102
LANG=C docmd $CMD $string
103 104
}

105
# TODO: use ^ as first word, use ~ for negate, for epmqp too
106 107 108 109
# produce grep sequence
__epm_search_make_grep()
{
	local i
110 111 112 113
	[ -z "$*" ] && return

	local list=
	local listN=
114
	for i in $@ ; do
115 116 117 118 119 120 121 122 123
		case "$i" in
			^*)
				# will clean from ^ later (and have the bug here with empty arg if run with one ^ only)
				listN="$listN $i"
				;;
			*)
				list="$list $i"
				;;
		esac
124 125 126
	done

	#list=$(strip_spaces $list | sed -e "s/ /|/g")
127
	listN=$(strip_spaces $listN | sed -e "s/ /|/g" | sed -e "s/\^//g")
128

129 130 131 132
	if [ "$short" ] ; then
		echon " | sed -e \"s| .*||g\""
	fi

133
	[ -n "$listN" ] && echon " | egrep -i -v -- \"$listN\""
134 135 136

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

138 139 140 141 142 143
	# 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
144 145 146 147 148
	fi

	# FIXME: move from it
	#isatty || return

149 150
	# TODO: sorts word by length from large to short

151 152
	local COLO=""
	# rule for colorife
153
	for i in $list $listN; do
154 155 156
		[ -n "$COLO" ] && COLO="$COLO|"
		COLO="$COLO$i"
	done
157

158
	# TODO: use some colorifer instead grep (check grep adove too)
159
	if [ -n "$list" ] ; then
160
		echon " | egrep -i $EGREPCOLOR -- \"($COLO)\""
161
	fi
162 163 164
}

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

166 167
epm_search()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
168
	[ -n "$pkg_filenames" ] || fatal "Search: missing search argument(s)"
169

170
	# it is useful for first time running
171
	update_repo_if_needed soft
172

173
	# FIXME: do it better
Vitaly Lipatov's avatar
Vitaly Lipatov committed
174 175
	local MGS
	MGS=$(eval __epm_search_make_grep $quoted_args)
176
	EXTRA_SHOWDOCMD="$MGS"
177
	eval "__epm_search_output \"$(eval get_firstarg $quoted_args)\" $MGS"
178
}