cmds 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +95 -1
- data/lib/cmds/util.rb +29 -31
- data/lib/cmds/version.rb +1 -1
- data/spec/spec_helper.rb +1 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 821f3c58f93958b7b377f6fb912a23f10bb7b4a3
|
4
|
+
data.tar.gz: 99bf03427c619cd9462c9425000c48e1b00361df
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1ef68ea914815640394af242902cde624b9ba55cd7c19244169021f2367e26a853f7c582275727e22dd44d25c64df24a53e9d52c39f8c9c642ad7ca259f3a640
|
7
|
+
data.tar.gz: 91e660c34a7e9386b3e523fa1eb8070a7e037e43640c56822e7fed11e05a87e231d3f4e2ebdaf3a77ef476754b2947fd082acd140775d790ffc6333bb652c9c6
|
data/README.md
CHANGED
@@ -88,6 +88,89 @@ Cmds 'PGPASSWORD=%{password} pg_dump %{opts} %{database} > %{filepath}',
|
|
88
88
|
|
89
89
|
|
90
90
|
|
91
|
+
## architecture
|
92
|
+
|
93
|
+
Cmds is based around a central `Cmds` class that takes a template for the command and a few options and operates by either wrapping the results in a `Cmds::Result` instance or streaming the results to `IO` objects or handler blocks. the Cmds` `augmented with a health helping of connivence methods for creating and executing a `Cmds` instance in common ways.
|
94
|
+
|
95
|
+
### constructor
|
96
|
+
|
97
|
+
the `Cmds` constructor looks like
|
98
|
+
|
99
|
+
```
|
100
|
+
Cmds(template:String, opts:Hash)
|
101
|
+
```
|
102
|
+
|
103
|
+
a brief bit about the arguments:
|
104
|
+
|
105
|
+
* `template`
|
106
|
+
* a `String` template processed with ERB against positional and keyword arguments.
|
107
|
+
* `opts`
|
108
|
+
* `:args`
|
109
|
+
* an `Array` of positional substitutions for the template.
|
110
|
+
* assigned to `@args`.
|
111
|
+
* defaults to an empty `Array`.
|
112
|
+
* `:kwds`
|
113
|
+
* a `Hash` of keyword substitutions for the template.
|
114
|
+
* assigned to `@kwds`.
|
115
|
+
* defaults to an empty `Hash`.
|
116
|
+
* `:input`
|
117
|
+
* a `String` to provide as standard input.
|
118
|
+
* assigned to `@input`.
|
119
|
+
* defaults to `nil`.
|
120
|
+
* `:assert`
|
121
|
+
* if this tests true, the execution of the command will raise an error on a nonzero exit status.
|
122
|
+
* assigned to `@assert`.
|
123
|
+
* defaults to `False`.
|
124
|
+
|
125
|
+
### execution
|
126
|
+
|
127
|
+
you can provide three types of arguments when executing a command:
|
128
|
+
|
129
|
+
1. positional arguments for substitution
|
130
|
+
2. keyword arguments for substitution
|
131
|
+
3. input to stdin
|
132
|
+
|
133
|
+
all `Cmds` instance execution methods have the same form for accepting these:
|
134
|
+
|
135
|
+
1. positional arguments are provided in an optional array that must be the first argument:
|
136
|
+
|
137
|
+
`Cmds "cp <%= arg %> <%= arg %>", [src_path, dest_path]`
|
138
|
+
|
139
|
+
note that the arguments need to be enclosed in square braces. Cmds does **NOT** use *splat for positional arguments because it would make a `Hash` final parameter ambiguous.
|
140
|
+
|
141
|
+
2. keyword arguments are provided as optional hash that must be the last argument:
|
142
|
+
|
143
|
+
`Cmds "cp <%= src %> <%= dest %>", src: src_path, dest: dest_path`
|
144
|
+
|
145
|
+
in this case, curly braces are not required since Ruby turns the trailing keywords into a `Hash` provided as the last argument (or second-to-last argument in the case of a block included in the method signature).
|
146
|
+
|
147
|
+
3. input and output is handled with blocks:
|
148
|
+
|
149
|
+
`Cmds(“wc -l”){ “one\ntwo\nthree\n” }
|
150
|
+
|
151
|
+
Cmds.stream './test/tick.rb <%= times %>', times: times do |io|
|
152
|
+
io.on_out do |line|
|
153
|
+
# do something with the output line
|
154
|
+
end
|
155
|
+
|
156
|
+
io.on_err do |line|
|
157
|
+
# do something with the error line
|
158
|
+
end
|
159
|
+
end`
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
## templates
|
164
|
+
|
165
|
+
command templates are processed with [eRuby](https://en.wikipedia.org/wiki/ERuby), which many people know as [ERB](http://ruby-doc.org/stdlib-2.2.2/libdoc/erb/rdoc/ERB.html). you may know ERB from [Rails](http://guides.rubyonrails.org/layouts_and_rendering.html).
|
166
|
+
|
167
|
+
actually, Cmds uses [Erubis](http://www.kuwata-lab.com/erubis/). which is the same thing Rails uses; calm down.
|
168
|
+
|
169
|
+
this takes care of a few things:
|
170
|
+
|
171
|
+
1. automatically shell escape values substituted into templates with [`Shellwords.escape`](http://ruby-doc.org/stdlib-2.2.2/libdoc/shellwords/rdoc/Shellwords.html#method-c-escape). it doesn't always do the prettiest job, but `Shellwords.escape` is part of Ruby's standard library and seems to work pretty well.
|
172
|
+
2. allow for fairly nice and readable logical structures like `if` / `else` in the command template. you've probably built html like this at some point. of course, the full power of Ruby is also available, though you probably won't find yourself needing much beyond some simple control structures.
|
173
|
+
|
91
174
|
## substitutions
|
92
175
|
|
93
176
|
substitutions can be positional, keyword, or both.
|
@@ -304,8 +387,20 @@ prod_playbook.call playbook: "setup.yml", inventory: "inventory/prod"
|
|
304
387
|
|
305
388
|
|
306
389
|
|
390
|
+
## input
|
391
|
+
|
392
|
+
```
|
393
|
+
c = Cmds.new("wc", input: "blah blah blah).call
|
394
|
+
```
|
395
|
+
|
396
|
+
|
397
|
+
|
307
398
|
## future..?
|
308
399
|
|
400
|
+
### exec
|
401
|
+
|
402
|
+
want to be able to use to exec commands
|
403
|
+
|
309
404
|
### formatters
|
310
405
|
|
311
406
|
kinda like `sprintf` formatters or string escape helpers in Rails, they would be exposed as functions in ERB and as format characters in the shorthand versions:
|
@@ -330,4 +425,3 @@ other formatters could include
|
|
330
425
|
* `l` or `,` for comma-separated list (which some commands like as input)
|
331
426
|
* `y` for YAML
|
332
427
|
* `p` for path, joining with `File.join`
|
333
|
-
|
data/lib/cmds/util.rb
CHANGED
@@ -89,49 +89,47 @@ class Cmds
|
|
89
89
|
end
|
90
90
|
end # ::expand_sub
|
91
91
|
|
92
|
-
# substitute values into a command, escaping them for the shell and
|
93
|
-
# offering convenient expansions for some structures.
|
92
|
+
# substitute values into a command template, escaping them for the shell and
|
93
|
+
# offering convenient expansions for some structures. uses ERB templating,
|
94
|
+
# so logic is supported as well.
|
94
95
|
#
|
95
|
-
#
|
96
|
+
# check out the {file:README.md#substitutions README} for details on use.
|
96
97
|
#
|
97
|
-
#
|
98
|
+
# @param template [String] command template with token to be replaced /
|
99
|
+
# expanded / escaped.
|
98
100
|
#
|
99
|
-
#
|
100
|
-
#
|
101
|
-
#
|
102
|
-
#
|
103
|
-
#
|
104
|
-
#
|
105
|
-
# `subs` is either:
|
106
|
-
#
|
107
|
-
# - an Array when `cmd` has positional placeholders
|
108
|
-
# - a Hash when `cmd` has keyword placeholders.
|
109
|
-
#
|
110
|
-
# the elements of the `subs` array or values of the `subs` hash are:
|
101
|
+
# @param args [Array] positional substitutions for occurances of
|
102
|
+
# - `<%= arg %>`
|
103
|
+
# - `%s`
|
104
|
+
#
|
105
|
+
# tokens in the `template` parameter.
|
111
106
|
#
|
112
|
-
#
|
107
|
+
# @param kwds [Hash] keyword subsitutions for occurances of the form
|
108
|
+
#
|
109
|
+
# - `<%= key %>`
|
110
|
+
# - `%{key}`
|
111
|
+
# - `%<key>s`
|
113
112
|
#
|
114
|
-
#
|
115
|
-
# # => 'git diff some\ path/to\ somewhere'
|
113
|
+
# as well as optional
|
116
114
|
#
|
117
|
-
#
|
115
|
+
# - `<%= key? %>`
|
116
|
+
# - `%{key?}`
|
117
|
+
# - `%<key?>s`
|
118
118
|
#
|
119
|
-
#
|
120
|
-
#
|
121
|
-
#
|
122
|
-
#
|
123
|
-
#
|
124
|
-
#
|
125
|
-
#
|
126
|
-
# }
|
127
|
-
# # => 'psql --host=localhost --port=12345 --username=bingo\ bob blah < /where\ ever/it/is.psql'
|
119
|
+
# tokens in the `template` parameter (where `key` is replaced with the
|
120
|
+
# symbol name in the hash).
|
121
|
+
#
|
122
|
+
# @return [String] formated command string suitable for execution.
|
123
|
+
#
|
124
|
+
# @raise [TypeError] if `args` is not an {Array}.
|
125
|
+
# @raise [TypeError] if `kwds` is not a {Hash}.
|
128
126
|
#
|
129
|
-
def self.sub
|
127
|
+
def self.sub template, args = [], kwds = {}
|
130
128
|
raise TypeError.new("args must be an Array") unless args.is_a? Array
|
131
129
|
raise TypeError.new("kwds must be an Hash") unless kwds.is_a? Hash
|
132
130
|
|
133
131
|
context = ERBContext.new(args, kwds)
|
134
|
-
erb = ShellEruby.new(
|
132
|
+
erb = ShellEruby.new replace_shortcuts(template)
|
135
133
|
|
136
134
|
NRSER.squish erb.result(context.get_binding)
|
137
135
|
end # ::sub
|
data/lib/cmds/version.rb
CHANGED
data/spec/spec_helper.rb
CHANGED