cmd2json 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 30c35dc495f05fb90247a531378b7308712fa1f0
4
+ data.tar.gz: f8e44d965233d477f9eed080d494a4543363c77a
5
+ SHA512:
6
+ metadata.gz: c3bb7496d632dcf0bbc1651218cabc2224618bb6e52cdfbfb976db2f592542524102895ef40e5f9f769db89a572c2f2b3600a217dd85abd37c391a79d6b444e9
7
+ data.tar.gz: 38b407c7805f77ee108c51f82431adb23aca673a49010c347ebb41220e0b44b5789ef5df0feb88d53289459493937ce82d123cd5aa8ec7aa1cb2b81ddefdf74a
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (C) 2013 Michael Grosser <michael@grosser.it>
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/bin/cmd2json ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ require 'optparse'
3
+
4
+ $LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
5
+ require 'cmd2json'
6
+
7
+ options = {add: {}}
8
+ parser = OptionParser.new do |opts|
9
+ opts.banner = <<BANNER
10
+ Covert command output and exit status to json to pipe them atomically into logs
11
+
12
+ Usage:
13
+ cmd2json [options] echo hello
14
+
15
+ Options:
16
+ BANNER
17
+ opts.on("-a", "--add KEY=VALUE", "add a key value pair") { |v| options[:add].send(:[]=, *v.split('=', 2)) }
18
+ opts.on("-h", "--help", "Show this.") { puts opts; exit }
19
+ opts.on('-v', '--version', 'Show Version'){ puts Cmd2Json::VERSION; exit 0 }
20
+ end
21
+
22
+ argv = ARGV.dup
23
+ parser.parse!(Cmd2Json.extract_options!(argv, parser))
24
+
25
+ abort "need command as argument" if argv.size == 0
26
+
27
+ status, output = Cmd2Json.run(argv, options)
28
+ puts output
29
+ exit status
@@ -0,0 +1,3 @@
1
+ module Cmd2Json
2
+ VERSION = "0.1.0"
3
+ end
data/lib/cmd2json.rb ADDED
@@ -0,0 +1,31 @@
1
+ require 'json'
2
+
3
+ module Cmd2Json
4
+ def self.run(argv, options={})
5
+ begin
6
+ out = ""
7
+ IO.popen(argv, err: [:child, :out]) { |io| out = io.read }
8
+ status = $?.exitstatus
9
+ rescue Errno::ENOENT
10
+ out = $!.message
11
+ status = 1
12
+ end
13
+ result = {message: out, exit: status, timestamp: Time.now.to_s}
14
+ [status, result.merge(options[:add] || {}).to_json]
15
+ end
16
+
17
+ # split options from argv so we can reuse the rest as command
18
+ # -a 1 echo hello -> -a 1 + echo hello
19
+ def self.extract_options!(argv, parser)
20
+ list = parser.top.list
21
+ options = []
22
+ loop do
23
+ arg = argv.first
24
+ break unless option = list.detect { |o| o.short.include?(arg) || o.long.include?(arg) }
25
+
26
+ options << argv.shift
27
+ options << argv.shift if option.arg
28
+ end
29
+ options
30
+ end
31
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cmd2json
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Michael Grosser
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-12-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description:
28
+ email: michael@grosser.it
29
+ executables:
30
+ - cmd2json
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - MIT-LICENSE
35
+ - bin/cmd2json
36
+ - lib/cmd2json.rb
37
+ - lib/cmd2json/version.rb
38
+ homepage: https://github.com/grosser/cmd2json
39
+ licenses:
40
+ - MIT
41
+ metadata: {}
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: 2.0.0
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubyforge_project:
58
+ rubygems_version: 2.5.1
59
+ signing_key:
60
+ specification_version: 4
61
+ summary: Covert command output and exit status to json to pipe them atomically into
62
+ logs
63
+ test_files: []