1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
#!/usr/bin/env ruby
require 'optparse'
require 'unleash'
require 'unleash/client'
require 'unleash/context'
options = {
variant: false,
verbose: false,
quiet: false,
url: 'http://localhost:4242',
demo: false,
disable_metrics: true,
sleep: 0.1
}
OptionParser.new do |opts|
opts.banner = "Usage: #{__FILE__} [options] feature [key1=val1] [key2=val2]"
opts.on("-V", "--variant", "Fetch variant for feature") do |v|
options[:variant] = v
end
opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
options[:verbose] = v
end
opts.on("-q", "--quiet", "Quiet mode, minimum output only") do |v|
options[:quiet] = v
end
opts.on("-uURL", "--url=URL", "URL base for the Unleash feature toggle service") do |u|
options[:url] = u
end
opts.on("-d", "--demo", "Demo load by looping, instead of a simple lookup") do |d|
options[:demo] = d
end
opts.on("-m", "--[no-]metrics", "Enable metrics reporting") do |m|
options[:disable_metrics] = !m
end
opts.on("-sSLEEP", "--sleep=SLEEP", Float, "Sleep interval between checks (seconds) in demo") do |s|
options[:sleep] = s
end
opts.on("-h", "--help", "Prints this help") do
puts opts
exit
end
end.parse!
feature_name = ARGV.shift
raise 'feature_name is required. see --help for usage.' unless feature_name
options[:verbose] = false if options[:quiet]
log_level = \
if options[:quiet]
Logger::ERROR
elsif options[:verbose]
Logger::DEBUG
else
Logger::WARN
end
@unleash = Unleash::Client.new(
url: options[:url],
app_name: 'unleash-client-ruby-cli',
disable_metrics: options[:metrics],
log_level: log_level
)
context_params = ARGV.map{ |e| e.split("=") }.map{ |k, v| [k.to_sym, v] }.to_h
context_properties = context_params.reject{ |k, _v| [:user_id, :session_id, :remote_address].include? k }
context_params.select!{ |k, _v| [:user_id, :session_id, :remote_address].include? k }
context_params.merge!(properties: context_properties) unless context_properties.nil?
unleash_context = Unleash::Context.new(context_params)
if options[:verbose]
puts "Running configuration:"
p options
puts "feature: #{feature_name}"
puts "context_args: #{ARGV}"
puts "context_params: #{context_params}"
puts "context: #{unleash_context}"
puts ""
end
if options[:demo]
loop do
enabled = @unleash.is_enabled?(feature_name, unleash_context)
print enabled ? '.' : '|'
sleep options[:sleep]
end
elsif options[:variant]
variant = @unleash.get_variant(feature_name, unleash_context)
puts " For feature \'#{feature_name}\' got variant \'#{variant}\'"
else
if @unleash.is_enabled?(feature_name, unleash_context)
puts " \'#{feature_name}\' is enabled according to unleash"
else
puts " \'#{feature_name}\' is disabled according to unleash"
end
end
@unleash.shutdown
|