class EtcdTools::Cli::Etcd2Yaml

Public Class Methods

new() click to toggle source
# File lib/etcd-tools/cli/etcd2yaml.rb, line 36
def initialize
  self.optparse

  begin
    @etcd = etcd_connect @options[:url]
  rescue ::Etcd::ClusterConnectError
    $stderr.puts "Failed to connect to ETCD cluster"
    exit! 1
  end

  begin
    hash = @etcd.get_hash @options[:root_path]
    puts YAML.dump hash
  rescue Exception => e
    $stderr.puts "Import failed"
    $stderr.puts e.message
    exit! 1
  end
end

Public Instance Methods

optparse() click to toggle source
# File lib/etcd-tools/cli/etcd2yaml.rb, line 10
def optparse
  @options = Hash.new

  @options[:url] = ENV['ETCDCTL_ENDPOINT']
  @options[:url] ||= "http://127.0.0.1:2379"
  @options[:root_path] = "/"

  OptionParser.new do |opts|
    opts.banner = "Parses ETCD tree into structured YAML\n\nUsage: #{$0} [OPTIONS]"
    opts.separator ""
    opts.separator "Connection options:"
    opts.on("-u", "--url HOST", "URL endpoint of the ETCD service (ETCDCTL_ENDPOINT envvar also applies) [DEFAULT: http://127.0.0.1:4001]") do |param|
      @options[:url] = param
    end
    opts.separator ""
    opts.separator "Common options:"
    opts.on("-r", "--root-path PATH", "root PATH of ETCD tree to extract the data from [DEFAULT: /]") do |param|
      @options[:root_path] = param
    end
    opts.on_tail("-h", "--help", "show usage") do |param|
      puts opts;
      exit! 0
    end
  end.parse!
end