Class: Rbcli::Scriptwrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/rbcli/scriptwrapping/scriptwrapper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, envvars = nil, block = nil, script = false) ⇒ Scriptwrapper

Returns a new instance of Scriptwrapper.



52
53
54
55
56
57
# File 'lib/rbcli/scriptwrapping/scriptwrapper.rb', line 52

def initialize path, envvars = nil, block = nil, script = false
	@path = path
	@envvars = envvars || {}
	@block = block
	@script = script
end

Instance Attribute Details

#path ⇒ Object (readonly)

Returns the value of attribute path.



59
60
61
# File 'lib/rbcli/scriptwrapping/scriptwrapper.rb', line 59

def path
  @path
end

Instance Method Details

#execute(params, args, global_opts, config) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/rbcli/scriptwrapping/scriptwrapper.rb', line 61

def execute params, args, global_opts, config
	####
	#### The following code will flatten one level of the hashes into separate variables
	#### It is deprecated in favor of passing along json to be parsed with JQ
	####
	# env_hash = {}
	# {
	# 		'__PARAMS' => params,
	# 		'__ARGS' => args,
	# 		'__GLOBAL' => global_opts,
	# 		'__CONFIG' => config
	# }.each do |name, hsh|
	# 	hsh.each do |k, v|
	# 		env_hash["#{name}_#{k.upcase}"] = v.to_json
	# 	end
	# end
	# env_hash.merge!(@envvars.deep_stringify!) unless @envvars.nil?

	if @block || @script
		env_hash = {
				'__RBCLI_PARAMS' => params.to_json,
				'__RBCLI_ARGS' => args.to_json,
				'__RBCLI_GLOBAL' => global_opts.to_json,
				'__RBCLI_CONFIG' => config.to_json,
				'__RBCLI_MYVARS' => @envvars.to_json
		}
		if @block
			path = @block.call params, args, global_opts, config
		else
			path = @path
		end
		system(env_hash, path)
	else
		system(@envvars.collect{|k,v| [k.to_s, v]}.to_h, @path)
	end

	# IO.popen(env_hash, path) do |io|
	# 	while (line = io.gets) do
	# 		puts line
	# 	end
	# end
end