Class: Transcriptic::CLI::Base

Inherits:
Thor
  • Object
show all
Includes:
UI
Defined in:
lib/transcriptic/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from UI

#arrow, #confirm, #confirm_billing, #confirm_command, #confirm_quote, #deprecate, disable_error_capture, #display, #display_row, #display_table, enable_error_capture, #error, #error_with_failure, extended, extended_into, #fail, #format_bytes, #format_date, #format_with_bang, #get_terminal_environment, included, included_into, #indent, #info, #json_decode, #json_encode, #longest, #mute!, #output, #output_with_arrow, #output_with_bang, #output_with_indent, #quantify, #quiet?, #redisplay, #say, #say_status, #set_buffer, #string_distance, #suggestion, #time_ago, #truncate, #unmute!, #wait_spinner, #warn

Constructor Details

#initialize(*args) ⇒ Base

Returns a new instance of Base.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/transcriptic/cli.rb', line 22

def initialize(*args)
  super(*args)

  Transcriptic.logger.level = ::Logger::INFO

  if @options[:debug]
    Transcriptic.logger.level = ::Logger::DEBUG
  end

  if @options[:quiet]
    Transcriptic.ui.mute!
  end

  @options = options.dup # unfreeze frozen options Hash from Thor
end

Class Method Details

.dispatch(meth, given_args, given_opts, config) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/transcriptic/cli.rb', line 7

def dispatch(meth, given_args, given_opts, config)
  unless (given_args & ['-h', '--help']).empty?
    if given_args.length == 1
      # transcriptic --help
      super
    else
      command = given_args.first
      super(meth, ['help', command].compact, nil, config)
    end
  else
    super
  end
end

Instance Method Details

#analyze(object_path, *params) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/transcriptic/cli.rb', line 142

def analyze(object_path, *params)
  raw = options.raw ? "--raw" : ""
  linearize = options.linearize ? "--linearize" : ""
  iterations = options.iterations ? options.iterations : 5
  require_labfile!
  update
  Transcriptic::SBT.stage
  data = `target/start #{object_path} --verify #{raw} #{linearize} --iterations #{iterations} #{params.join(" ")}`
  if not options.raw
    File.open("logs/#{object_path}", 'w') { |file| file.write(data) }
    data = data.split("\n")[-1]
    json = json_decode(data)
    output_with_arrow "Verification successful for #{object_path}"
    output_with_arrow "Logged output during verification to logs/#{object_path}"
    output_with_indent "Expected cost: $#{json["price_avg"]}, maximum observed cost: $#{json["price_max"]}"
  else
    say data
  end
end

#cleanObject



134
135
136
137
138
# File 'lib/transcriptic/cli.rb', line 134

def clean
  require_labfile!
  update
  Transcriptic::SBT.compile
end

#compileObject



127
128
129
130
131
# File 'lib/transcriptic/cli.rb', line 127

def compile
  require_labfile!
  update
  Transcriptic::SBT.compile
end

#launch(object_path) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/transcriptic/cli.rb', line 199

def launch(object_path)
  require_labfile!
  output_with_arrow "Not yet available!"
  return

  fd = create_protocol_fd_for_path(path)
  if 1 == fd
    Transcriptic.ui.error_with_failure "Couldn't packaged protocol filename or directory!"
  end
  if args.empty?
    project_id = "default"
  else
    project_id = args.shift
  end
  run = action("Uploading `#{path}`") {
    transcriptic_client.create_run(fd, project_id)
  }

  if run["success"]
    Transcriptic.ui.output_with_arrow "Protocol received: #{run["protocol_format"]} detected"
    Transcriptic.ui.output_with_arrow "Protocol received: #{run["nsteps"]} steps, expected cost: #{run["exp_cost"]}, total possible cost: #{run["max_cost"]}"
    Transcriptic.ui.output_with_arrow "Protocol received: expected runtime: #{run["exp_time"]}, total possible runtime: #{run["max_time"]}"
    if Transcriptic.ui.confirm_quote
      Transcriptic.ui.display
      Transcriptic.ui.action("Launching") {
        transcriptic_client.launch_run(run["run_id"])
      }
      Transcriptic.ui.output_with_arrow "Protocol run launched.  ID: #{run["run_id"]}"
      Transcriptic.ui.output_with_arrow "Monitor at:  https://secure.transcriptic_client.com/#{run["organization_id"]}/#{run["project_id"]}/runs/#{run["run_id"]}"
    end
  else
    Transcriptic.ui.error_with_failure "Error creating protocol run."
  end
end

#listObject



87
88
89
90
91
92
93
94
95
96
# File 'lib/transcriptic/cli.rb', line 87

def list
  ret = transcriptic_client.list_runs
  if ret.empty?
    say "No runs for #{transcriptic_client.user}"
    return
  end
  ret.each do |run|
    say "#{run["title"]}\t#{run["id"]}\t#{run["partials"].length} partials\t#{run["status"]}"
  end
end

#loginObject



39
40
41
42
# File 'lib/transcriptic/cli.rb', line 39

def 
  Transcriptic::Auth.
  say "Authentication successful."
end

#logoutObject



45
46
47
48
# File 'lib/transcriptic/cli.rb', line 45

def logout
  Transcriptic::Auth.logout
  say "Local credentials cleared."
end

#logs(run_id) ⇒ Object



73
74
75
# File 'lib/transcriptic/cli.rb', line 73

def logs(run_id)
  transcriptic_client.read_logs(run_id)
end

#lookup(term) ⇒ Object



110
111
112
113
114
115
# File 'lib/transcriptic/cli.rb', line 110

def lookup(term)
  ret = transcriptic_client.search_resources(term)
  ret["results"].each do |result|
    output_with_arrow "#{result[:resource_id]}: #{result[:name]}"
  end
end

#new(name) ⇒ Object



68
69
70
# File 'lib/transcriptic/cli.rb', line 68

def new(name)
  Transcriptic::ProjectGenerator.new([File.join(Dir.pwd, name), name], options).generate
end

#publishObject



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/transcriptic/cli.rb', line 163

def publish
  labfile = require_labfile!
  update
  Transcriptic::SBT.clean
  compiled_jar_path = Transcriptic::SBT.stage
  if compiled_jar_path
    dependencies = labfile.dependencies.map {|d| "#{d[:group]}.#{d[:name]}/#{d[:version]}" }.join(", ")
    output_with_arrow "Preparing to publish..."
    output_with_arrow "Project name: #{labfile.options[:name]}, Author: #{labfile.options[:author]}, Email: #{labfile.options[:email]}"
    if labfile.options[:description]
      output_with_arrow "Description: #{labfile.options[:description]}"
    else
      display format_with_bang("Warning: No description provided.")
    end
    if labfile.dependencies.length > 0
      output_with_arrow "Dependencies: #{dependencies}}"
    end
    if confirm(format_with_bang("Are you sure you want to publish version #{labfile.options[:version]} of this project? (y/N)"))
      output_with_arrow "Uploading #{compiled_jar_path} to Transcriptic..."

      signature = transcriptic_client.sign_upload(labfile, File.basename(compiled_jar_path))
      res = transcriptic_client.create_or_get_protocol(labfile, signature)
      if res["success"]
        transcriptic_client.upload_to_s3(compiled_jar_path, signature)
        output_with_arrow "Revision #{labfile.options[:version]} of #{labfile.options[:name]} published!"
      else
        errors = res["errors"].map { |e| e[1].map { |m| "#{e[0]} #{m}" }.join(", ") }.join(", ")
        display format_with_bang("Errors occurred: #{errors}")
      end
    else
      display "Aborting."
    end
  end
end

#status(run_id) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/transcriptic/cli.rb', line 78

def status(run_id)
  ret = transcriptic_client.run_info(run_id)
  if ret.nil?
    error "#{run_id} not found for #{transcriptic_client.user}"
  end
  say ret["status"]
end

#tokenObject



51
52
53
# File 'lib/transcriptic/cli.rb', line 51

def token
  say Transcriptic::Auth.api_key
end

#updateObject



118
119
120
121
122
123
124
# File 'lib/transcriptic/cli.rb', line 118

def update
  require_labfile!
  output_with_arrow "Updating dependencies..."
  labfile = Transcriptic::Labfile.from_file(Transcriptic.find_labfile)
  Transcriptic::DependenciesGenerator.new([Dir.pwd, labfile.dependencies], options).invoke_all
  Transcriptic::ProjectGenerator.new([Dir.pwd, labfile.options[:name]], options).update_build_version(labfile)
end