class VcoWorkflows::Cli::Query

Query

Public Class Methods

source_root() click to toggle source

Thor

# File lib/vcoworkflows/cli/query.rb, line 30
def self.source_root
  File.dirname(__FILE__)
end

Public Instance Methods

query() click to toggle source

Process the subcommand rubocop:disable CyclomaticComplexity, PerceivedComplexity

# File lib/vcoworkflows/cli/query.rb, line 36
def query
  config = VcoWorkflows::Config.new(url:        options[:server],
                                    username:   options[:username],
                                    password:   options[:password],
                                    verify_ssl: options[:verify_ssl])

  if options[:dry_run]
    puts "\nQuerying against vCO REST endpoint:\n  #{options[:server]}"
    puts "Will search for workflow: '#{workflow}'"
    puts "Will query workflow by GUID (#{options[:id]})" if options[:id]
    return
  end

  # Get the workflow
  puts "\nRetrieving workflow '#{workflow}' ..."
  wf = VcoWorkflows::Workflow.new(workflow,
                                  id:     options[:id],
                                  config: config)

  puts ''
  if options[:execution_id]
    puts "Fetching data for execution #{options[:execution_id]}..."
    execution = wf.token(options[:execution_id])
    if options[:state]
      puts "Execution started at #{Time.at(execution.start_date / 1000)}"
      puts "Execution #{execution.state} at #{Time.at(execution.end_date / 1000)}"
    else
      puts ''
      if options[:show_json]
        puts execution.to_json
      else
        puts execution
      end
    end

    if options[:logs]
      puts ''
      wflog = wf.log(options[:execution_id])
      if options[:show_json]
        puts wflog.to_json
      else
        puts wflog
      end
    end
  else
    puts wf unless options[:executions]
  end

  # Last thing we're checking for, so if it's not wanted, just return
  return unless options[:executions]
  puts "\nWorkflow:   #{wf.name}"
  puts "ID:           #{wf.id}"
  puts "Description:  #{wf.description}"
  puts "Version:      #{wf.version}"
  puts "\nExecutions: "
  executions = {}
  wf.executions.each_value { |attrs| executions[attrs['startDate']] = attrs }
  keys = executions.keys.sort
  keys = keys.slice(keys.size - options[:last], keys.size) if options[:last] > 0
  keys.each do |timestamp|
    dataline = timestamp.to_s
    dataline << " [#{executions[timestamp]['id']}]"
    dataline << " #{executions[timestamp]['state']}"
    puts dataline
  end
end