Class: Bixby::CommandSpec

Inherits:
Object
  • Object
show all
Includes:
Hashify, Jsonify
Defined in:
lib/bixby_common/command_spec.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Hashify

#to_hash

Methods included from Jsonify

included, #to_json

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

#argsObject

Returns the value of attribute args.



13
14
15
# File 'lib/bixby_common/command_spec.rb', line 13

def args
  @args
end

#bundleObject

Returns the value of attribute bundle.



13
14
15
# File 'lib/bixby_common/command_spec.rb', line 13

def bundle
  @bundle
end

#commandObject

Returns the value of attribute command.



13
14
15
# File 'lib/bixby_common/command_spec.rb', line 13

def command
  @command
end

#digestObject

Returns the value of attribute digest.



13
14
15
# File 'lib/bixby_common/command_spec.rb', line 13

def digest
  @digest
end

#envObject

Returns the value of attribute env.



13
14
15
# File 'lib/bixby_common/command_spec.rb', line 13

def env
  @env
end

#repoObject

Returns the value of attribute repo.



13
14
15
# File 'lib/bixby_common/command_spec.rb', line 13

def repo
  @repo
end

#stdinObject

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_dirObject

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.expand_path(File.join(AGENT_ROOT, "../repo", @bundle))
  end
  File.join(BundleRepository.path, self.relative_path)
end

#bundle_exists?Boolean

Returns:

  • (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

Returns:

  • (Boolean)


83
84
85
# File 'lib/bixby_common/command_spec.rb', line 83

def command_exists?
  File.exists? self.command_file
end

#command_fileObject



79
80
81
# File 'lib/bixby_common/command_spec.rb', line 79

def command_file
  File.join(self.bundle_dir, "bin", @command)
end

#config_fileObject



87
88
89
# File 'lib/bixby_common/command_spec.rb', line 87

def config_file
  command_file + ".json"
end

#digest_fileObject



99
100
101
# File 'lib/bixby_common/command_spec.rb', line 99

def digest_file
  File.join(self.bundle_dir, "digest")
end

#executeArray<FixNum, String, String>

Execute this command

Parameters:

  • cmd (String)

    Command string to execute

Returns:

  • (Array<FixNum, String, String>)

    status code, stdout, stderr



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_configObject



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_digestObject



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_pathObject



71
72
73
# File 'lib/bixby_common/command_spec.rb', line 71

def relative_path
  File.join(@repo, @bundle)
end

#update_digestObject



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

#validateBoolean

Validate the existence of this Command on the local system

Returns:

  • (Boolean)

    returns true if available, else raises error

Raises:



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