vow 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.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/lib/vow/helper.rb +32 -0
  3. data/lib/vow.rb +56 -0
  4. metadata +45 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4456531301553115ebfeb1339b015dad92abf6ce673020844272edba0165d7af
4
+ data.tar.gz: a640b5a1e8d6eb0ed463deda0119b48e9bd395400c6233352a75ba81169d4d40
5
+ SHA512:
6
+ metadata.gz: b06d4f2206ded53da0e3283e22eb31477ee9e3d06c1cf314af62212beab72a0ed05bfb826af76ffd26e1838ee5bd54229834e561b100f72e8f54e12db325da1f
7
+ data.tar.gz: 5410b0895240928b8b059d118e5417aa483af82c38f2943c708541b375b9ba70f7aec2987de0263b29fc9179a9ab350866de0050b731a3f474e35d2ed2c5beff
data/lib/vow/helper.rb ADDED
@@ -0,0 +1,32 @@
1
+ module Vow::Helper
2
+ def self.refine_command(command)
3
+ refined = command.flatten.select {|a| !a.nil?}
4
+
5
+ if refined.length == 0
6
+ raise "Nothing to run"
7
+ end
8
+
9
+ cmdname = refined[0]
10
+ if cmdname.include?(' ')
11
+ raise "Command name does not follow safe pattern: '#{cmdname}'"
12
+ end
13
+
14
+ refined
15
+ end
16
+
17
+ def self.printable_command(command)
18
+ simple_command = true
19
+ command.each do |word|
20
+ unless %r|^[[[:alnum:]].+_/-]+$|.match?(word)
21
+ simple_command = false
22
+ break
23
+ end
24
+ end
25
+
26
+ if simple_command
27
+ return command.join(' ')
28
+ else
29
+ return command.to_s
30
+ end
31
+ end
32
+ end
data/lib/vow.rb ADDED
@@ -0,0 +1,56 @@
1
+ ###
2
+ # = Overview
3
+ #
4
+ # Vow is a Kernel.system() wrapper.
5
+ # Vow makes it more convenient to construct external commands to run and deal with their exit codes.
6
+ #
7
+ # = Usage
8
+ #
9
+ # In the simplest form, Vow.system() can be used as a drop-in replacement for Kernel.system():
10
+ #
11
+ # # Execute command
12
+ # Vow.system('echo', 'Hello, World!')
13
+
14
+ module Vow
15
+
16
+ def self.system(*command, **args)
17
+ may_raise = args.delete(:may_raise)
18
+
19
+ expected_exit_codes = (args.delete(:returns) or 0)
20
+ expected_exit_codes = [expected_exit_codes].flatten
21
+
22
+ command = Helper.refine_command(command)
23
+
24
+ if Kernel.system(*command, **args).nil?
25
+ if may_raise
26
+ raise "Failed to execute command: #{Helper.printable_command(command)}"
27
+ else
28
+ return nil
29
+ end
30
+ end
31
+
32
+ exit_code = $?.exitstatus
33
+ if expected_exit_codes.include?(exit_code)
34
+ return true
35
+ else
36
+ if may_raise
37
+ raise "Unexpected error code (#{exit_code}) from command: #{Helper.printable_command(command)}"
38
+ else
39
+ return false
40
+ end
41
+ end
42
+ end
43
+
44
+ def self.silent(*command, **args)
45
+ args[:out] = '/dev/null'
46
+ args[:err] = '/dev/null'
47
+ self.system(*command, **args)
48
+ end
49
+
50
+ def self.strict(*command, **args)
51
+ args[:may_raise] = true
52
+ self.system(*command, **args)
53
+ end
54
+ end
55
+
56
+ require_relative 'vow/helper'
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vow
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ruslan Nugmanov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-09-18 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email: ruslan@nugmanov.net
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/vow.rb
20
+ - lib/vow/helper.rb
21
+ homepage: https://gitlab.com/nugmanov/vow
22
+ licenses:
23
+ - ISC
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubygems_version: 3.2.22
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: system() call wrapper that makes it convenient to execute external commands
44
+ in safe manner
45
+ test_files: []