Class: Bixby::CommandSpec
- Inherits:
-
Object
- Object
- Bixby::CommandSpec
- Defined in:
- lib/bixby_common/command_spec.rb
Instance Attribute Summary collapse
-
#args ⇒ Object
Returns the value of attribute args.
-
#bundle ⇒ Object
Returns the value of attribute bundle.
-
#command ⇒ Object
Returns the value of attribute command.
-
#digest ⇒ Object
Returns the value of attribute digest.
-
#env ⇒ Object
Returns the value of attribute env.
-
#repo ⇒ Object
Returns the value of attribute repo.
-
#stdin ⇒ Object
Returns the value of attribute stdin.
Instance Method Summary collapse
-
#bundle_dir ⇒ Object
resolve the given bundle.
- #bundle_exists? ⇒ Boolean
- #command_exists? ⇒ Boolean
- #command_file ⇒ Object
- #config_file ⇒ Object
- #digest_file ⇒ Object
-
#execute ⇒ Array<FixNum, String, String>
Execute this command.
-
#initialize(params = nil) ⇒ CommandSpec
constructor
Create new CommandSpec.
- #load_config ⇒ Object
- #load_digest ⇒ Object
- #relative_path ⇒ Object
- #update_digest ⇒ Object
-
#validate ⇒ Boolean
Validate the existence of this Command on the local system.
Methods included from Hashify
Methods included from Jsonify
Constructor Details
#initialize(params = nil) ⇒ CommandSpec
Create new CommandSpec
18 19 20 21 22 23 24 |
# File 'lib/bixby_common/command_spec.rb', line 18 def initialize(params = nil) return if params.nil? or params.empty? params.each{ |k,v| self.send("#{k}=", v) if self.respond_to? "#{k}=" } digest = load_digest() @digest = digest["digest"] if digest end |
Instance Attribute Details
#args ⇒ Object
Returns the value of attribute args.
13 14 15 |
# File 'lib/bixby_common/command_spec.rb', line 13 def args @args end |
#bundle ⇒ Object
Returns the value of attribute bundle.
13 14 15 |
# File 'lib/bixby_common/command_spec.rb', line 13 def bundle @bundle end |
#command ⇒ Object
Returns the value of attribute command.
13 14 15 |
# File 'lib/bixby_common/command_spec.rb', line 13 def command @command end |
#digest ⇒ Object
Returns the value of attribute digest.
13 14 15 |
# File 'lib/bixby_common/command_spec.rb', line 13 def digest @digest end |
#env ⇒ Object
Returns the value of attribute env.
13 14 15 |
# File 'lib/bixby_common/command_spec.rb', line 13 def env @env end |
#repo ⇒ Object
Returns the value of attribute repo.
13 14 15 |
# File 'lib/bixby_common/command_spec.rb', line 13 def repo @repo end |
#stdin ⇒ Object
Returns the value of attribute stdin.
13 14 15 |
# File 'lib/bixby_common/command_spec.rb', line 13 def stdin @stdin end |
Instance Method Details
#bundle_dir ⇒ Object
resolve the given bundle
63 64 65 66 67 68 69 |
# File 'lib/bixby_common/command_spec.rb', line 63 def bundle_dir if @repo == "local" and Module.constants.include? :AGENT_ROOT then # only resolve the special "local" repo for Agents return File.(File.join(AGENT_ROOT, "../repo", @bundle)) end File.join(BundleRepository.path, self.relative_path) end |
#bundle_exists? ⇒ Boolean
75 76 77 |
# File 'lib/bixby_common/command_spec.rb', line 75 def bundle_exists? File.exists? self.bundle_dir end |
#command_exists? ⇒ Boolean
83 84 85 |
# File 'lib/bixby_common/command_spec.rb', line 83 def command_exists? File.exists? self.command_file end |
#command_file ⇒ Object
79 80 81 |
# File 'lib/bixby_common/command_spec.rb', line 79 def command_file File.join(self.bundle_dir, "bin", @command) end |
#config_file ⇒ Object
87 88 89 |
# File 'lib/bixby_common/command_spec.rb', line 87 def config_file command_file + ".json" end |
#digest_file ⇒ Object
99 100 101 |
# File 'lib/bixby_common/command_spec.rb', line 99 def digest_file File.join(self.bundle_dir, "digest") end |
#execute ⇒ Array<FixNum, String, String>
Execute this command
31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/bixby_common/command_spec.rb', line 31 def execute if @stdin and not @stdin.empty? then temp = Tempfile.new("input-") temp << @stdin temp.flush temp.close cmd = "sh -c 'cat #{temp.path} | #{self.command_file}" else cmd = "sh -c '#{self.command_file}" end cmd += @args ? " #{@args}'" : "'" status, stdout, stderr = system_exec(cmd) end |
#load_config ⇒ Object
91 92 93 94 95 96 97 |
# File 'lib/bixby_common/command_spec.rb', line 91 def load_config if File.exists? config_file then MultiJson.load(File.read(config_file)) else {} end end |
#load_digest ⇒ Object
103 104 105 106 107 108 109 |
# File 'lib/bixby_common/command_spec.rb', line 103 def load_digest begin return MultiJson.load(File.read(digest_file)) rescue => ex end nil end |
#relative_path ⇒ Object
71 72 73 |
# File 'lib/bixby_common/command_spec.rb', line 71 def relative_path File.join(@repo, @bundle) end |
#update_digest ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/bixby_common/command_spec.rb', line 111 def update_digest path = self.bundle_dir sha = Digest::SHA2.new bundle_sha = Digest::SHA2.new digests = [] Dir.glob("#{path}/**/*").sort.each do |f| next if File.directory? f || File.basename(f) == "digest" bundle_sha.file(f) sha.reset() digests << { :file => f.gsub(/#{path}\//, ''), :digest => sha.file(f).hexdigest() } end @digest = { :digest => bundle_sha.hexdigest(), :files => digests } File.open(path+"/digest", 'w'){ |f| f.write(MultiJson.dump(@digest, :pretty => true) + "\n") } end |
#validate ⇒ Boolean
Validate the existence of this Command on the local system
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/bixby_common/command_spec.rb', line 51 def validate if not bundle_exists? then raise BundleNotFound.new("repo = #{@repo}; bundle = #{@bundle}") end if not command_exists? then raise CommandNotFound.new("repo = #{@repo}; bundle = #{@bundle}; command = #{@command}") end return true end |