Sha256: 9fb20996f74c7745c0976ad0663d0312bd928e79dbadbc92f860c570e1daeb70

Contents?: true

Size: 1.47 KB

Versions: 2

Compression:

Stored size: 1.47 KB

Contents

#!/usr/bin/env ruby

$:.unshift File.join(File.dirname(__FILE__), '../lib')

require 'logger'
require 'optparse'

require 'yq'

$stdout.sync = $stderr.sync = true
LOGGER = Logger.new($stderr) unless defined?(LOGGER)
LOGGER.level = Logger::INFO

@options = {
  context: :main_loop,
  output: :yaml
}

def prereqs
  ARGV << '--help' if ARGV.empty?

  opts = OptionParser.new do |opts|
    opts.banner = "Usage: #{File.basename($0)}"
    opts.separator ''
    opts.on_tail('-h', '--help', 'Show this message') do
      puts opts
      exit 0
    end
    opts.on('--verbose', '-v', 'verbose') do
      LOGGER.level = Logger::DEBUG
    end
    opts.on('--develop', 'Run in developer mode') do
      @options[:context] = :develop
    end
    opts.on('-r', '--raw-output') do
      @options[:output] = :raw
    end
    opts.on('--json-output') do
      @options[:output] = :json
    end
  end
  opts.parse!

  required_vars = %w[
  ]
  required_vars.each do |v|
    raise "#{v} must be specified in the environment variables" unless ENV[v]
  end

  raise 'jq not installed' unless Yq.which('jq')
end

def start
  @yaml = $stdin.read

  puts Yq.search_yaml(ARGV[0], @yaml, output: @options[:output])
rescue StandardError => e
  LOGGER.error "Unhandled exception #{e.class}: #{e.message}"
  e.backtrace.each do |v|
    LOGGER.debug v
  end
  raise
end

def develop
  require 'pry'
  LOGGER.level = Logger::DEBUG
  LOGGER.info 'Develop mode'
  start
end

def main_loop
  start
end

prereqs
send(@options[:context])

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
yq-0.5.0 bin/yq
yq-0.4.1 bin/yq