thor_tasks 0.0.9 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +17 -0
- data/lib/tasks/apt.thor +74 -19
- data/lib/thor_tasks/version.rb +1 -1
- metadata +2 -2
data/README.rdoc
CHANGED
@@ -76,3 +76,20 @@ The tasks are:
|
|
76
76
|
thor mongo:start
|
77
77
|
thor mongo:restart
|
78
78
|
thor mongo:stop
|
79
|
+
|
80
|
+
== Apt tasks
|
81
|
+
|
82
|
+
These tasks are helpers for apt-cache in Linux.
|
83
|
+
|
84
|
+
=== apt:search
|
85
|
+
|
86
|
+
thor apt:search "terms" # or thor apt:s
|
87
|
+
|
88
|
+
Pretty-prints the output from apt-cache search.
|
89
|
+
|
90
|
+
=== apt:show
|
91
|
+
|
92
|
+
thor apt:show "package" # or thor apt:w
|
93
|
+
thor apt:show "package --desc # to only show the description
|
94
|
+
|
95
|
+
Cleans up the output from apt-cache show.
|
data/lib/tasks/apt.thor
CHANGED
@@ -1,17 +1,13 @@
|
|
1
|
+
require 'rainbow'
|
2
|
+
|
1
3
|
class Apt < Thor
|
2
4
|
include Thor::Actions
|
3
5
|
|
4
|
-
map "s" => :search
|
6
|
+
map "s" => :search,
|
7
|
+
"w" => :show
|
5
8
|
|
6
9
|
desc "search \"terms\"", "runs apt-cache search and pretty-prints the output"
|
7
10
|
def search(terms)
|
8
|
-
# attempt to load rainbow for pretty-print
|
9
|
-
begin
|
10
|
-
require 'rainbow'
|
11
|
-
pp = true
|
12
|
-
rescue GEM::LoadError
|
13
|
-
pp = false
|
14
|
-
end
|
15
11
|
# run apt-cache search
|
16
12
|
results = `apt-cache search "#{terms}"`.split("\n")
|
17
13
|
# create a Hash out of the results
|
@@ -23,19 +19,78 @@ class Apt < Thor
|
|
23
19
|
end
|
24
20
|
# pretty print the results
|
25
21
|
results.each do |k,v|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
print " #{v}\n".foreground(:white).bright
|
33
|
-
else
|
34
|
-
# print the name, padding, and description
|
35
|
-
puts "#{k + " " * (longest - k.length) + " " + v}"
|
36
|
-
end
|
22
|
+
# print the name in bright yellow
|
23
|
+
print "#{k}".foreground(:yellow).bright
|
24
|
+
# pad the names with spaces
|
25
|
+
print "#{" " * (longest - k.length)}"
|
26
|
+
# print the description in green
|
27
|
+
print " #{v}\n".foreground(:white).bright
|
37
28
|
end
|
38
29
|
# restore the terminal color
|
39
30
|
print "".reset
|
40
31
|
end
|
32
|
+
|
33
|
+
desc "show \"package\" [--desc]", "cleans up apt-cache show output for a package"
|
34
|
+
method_option :desc, :type => :boolean, :aliases => "-d" # shows only the description
|
35
|
+
def show(package)
|
36
|
+
# get the apt-cache show output
|
37
|
+
output = `apt-cache show \"#{package}\"`.split("\n")
|
38
|
+
return false if output.length == 0
|
39
|
+
# we will split this into 3 parts: parameters, description, and footnote
|
40
|
+
params = {}
|
41
|
+
desc = ""
|
42
|
+
ftn = nil
|
43
|
+
# iterate through each line
|
44
|
+
output.each do |l|
|
45
|
+
# unless the first character is a space, we have a parameter
|
46
|
+
unless l[0] == " "
|
47
|
+
# get the name - before the first colon
|
48
|
+
p = l.partition(":")
|
49
|
+
# get the value
|
50
|
+
params[p[0]] = p[2].strip
|
51
|
+
else
|
52
|
+
# if the line is a space and a period, we are entering the footnote
|
53
|
+
if l == " ." # footnote
|
54
|
+
# toggle the footnote
|
55
|
+
ftn = ""
|
56
|
+
else
|
57
|
+
# add to the description, unless we are in the footnote
|
58
|
+
desc += l.strip if ftn.nil?
|
59
|
+
# add to teh footnote, unless we are in the description
|
60
|
+
ftn += l.strip unless ftn.nil?
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
# find the longest key in the parameters
|
65
|
+
longest = 0
|
66
|
+
params.each do |k,v|
|
67
|
+
longest = k.length > longest ? k.length : longest
|
68
|
+
end
|
69
|
+
|
70
|
+
# start by printing the description parameter (title of the package)
|
71
|
+
print params["Description"].foreground(:green).bright.underline + "\n\n"
|
72
|
+
|
73
|
+
# print the long description
|
74
|
+
print desc.foreground(:white).bright + "\n\n"
|
75
|
+
# print the footnote
|
76
|
+
print ftn.foreground(:blue) + "\n\n"
|
77
|
+
|
78
|
+
# ommit the details if we only want the description
|
79
|
+
unless options[:desc]
|
80
|
+
params.each do |k,v|
|
81
|
+
print_param(params, k, longest)
|
82
|
+
end
|
83
|
+
puts "\n"
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
no_tasks do
|
88
|
+
def print_param(params, name, longest)
|
89
|
+
unless params[name].nil?
|
90
|
+
print "#{name} ".foreground(:blue).bright
|
91
|
+
print " " * (longest - name.length)
|
92
|
+
print params[name].foreground(:green) + "\n"
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
41
96
|
end
|
data/lib/thor_tasks/version.rb
CHANGED