Module: Gfspark::Config
- Included in:
- App
- Defined in:
- lib/gfspark/config.rb
Constant Summary collapse
- COLORS =
[ :black, :red, :green, :yellow, :blue, :magenta, :cyan, :white]
Instance Method Summary collapse
- #detect_width_and_height! ⇒ Object
- #help(options = {}) ⇒ Object
- #load_default_settings ⇒ Object
- #load_default_settings_from_home ⇒ Object
- #load_default_settings_with_traversing_to_root(dir = Dir::pwd) ⇒ Object
- #opt_parser ⇒ Object
- #parse_options(args) ⇒ Object
- #set_ssl_options! ⇒ Object
- #try_default(args) ⇒ Object
- #try_path(args) ⇒ Object
- #try_url(args) ⇒ Object
Instance Method Details
#detect_width_and_height! ⇒ Object
76 77 78 79 80 81 82 83 84 |
# File 'lib/gfspark/config.rb', line 76 def detect_width_and_height! @stty_height, @stty_width = `stty size`.split.map(&:to_i) width = ((@stty_width - 18) / 2).floor @options[:width] ||= width @width = @options[:width].to_i height = @options[:height].to_i @height = height.zero? ? 10 : height end |
#help(options = {}) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/gfspark/config.rb', line 52 def help( = {}) puts @opt_parse_obj. puts <<-MSG Examples: gfspark http://your.gf.com/view_graph/your_service/your_section/your_graph?t=h gfspark your_service/your_section/your_graph h --url=http://your.gf.com/view_graph gfspark your_service your_section your_graph h --url=http://your.gf.com/view_graph MSG puts " Options:" puts @opt_parse_obj.summarize puts <<-MSG -t option detail: #{Gfspark::Graph::RANGE_DEFS.map{|k,v,_| sprintf(" %3s : %s", k, v)}.join("\n")} MSG end |
#load_default_settings ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/gfspark/config.rb', line 86 def load_default_settings settings = load_default_settings_with_traversing_to_root || load_default_settings_from_home || {} settings = Hash[*settings.map{|k,v| [k.to_sym, v]}.flatten] @url = settings[:url] if settings[:url] @service = settings[:service] if settings[:service] @section = settings[:section] if settings[:section] @graph = settings[:graph] if settings[:graph] settings end |
#load_default_settings_from_home ⇒ Object
106 107 108 109 |
# File 'lib/gfspark/config.rb', line 106 def load_default_settings_from_home filename = File.join("~/", ".gfspark") return YAML.load_file filename if File.exists? filename end |
#load_default_settings_with_traversing_to_root(dir = Dir::pwd) ⇒ Object
98 99 100 101 102 103 104 |
# File 'lib/gfspark/config.rb', line 98 def load_default_settings_with_traversing_to_root(dir = Dir::pwd) filename = File.join(dir, '.gfspark') return YAML.load_file filename if File.exists? filename parent = File.dirname dir load_default_settings_with_traversing_to_root(parent) unless dir == parent end |
#opt_parser ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/gfspark/config.rb', line 113 def opt_parser OptionParser.new{|opts| opts. = "gfspark : Growth Forecast on Terminal\n\nusage: gfspark <url|path|service_name> [section_name] [graph_name]" opts.on("--url=VALUE", "Your GrowthForecast URL"){|v| @options[:url] = v} opts.on('-u=USER', '--user=USER'){|v| @options[:username] = v } opts.on('-p=PASS', '--pass=PASS'){|v| @options[:password] = v } opts.on("-t=VALUE", "--type=VALUE", "Range of Graph"){|v| @options[:t] = v} opts.on("--gmode=VALUE", "graph mode: gauge or subtract (default is gauge)"){|v| @options[:gmode] = v} opts.on("--from=VALUE", "Start date of graph (2011/12/08 12:10:00) required if t=c or sc"){|v| @options[:from] = v} opts.on("--to=VALUE", "End date of graph (2011/12/08 12:10:00) required if t=c or sc"){|v| @options[:to] = v} opts.on("-h=VALUE", "--height=VALUE", "graph height (default 10"){|v| @options[:height] = v} opts.on("-w=VALUE", "--width=VALUE", "graph width (default is deteced from $COLUMNS)"){|v| @options[:width] = v} opts.on("-c=VALUE", "--color=VALUE", "Color of graph bar (#{COLORS.join("/")})"){|v| @options[:color] = v if COLORS.include?(v.to_sym)} opts.on("-x=VALUE", "--x-axis-label=VALUE", "Show x axis labels (hide/show/simple: default is show)"){|v| @options[:x_axis_label] = v} opts.on("-n", "--non-fullwidth-font", "Show bar symbol as fullwidth"){ @options[:non_fullwidth_font] = true } opts.on("--sslnoverify", "don't verify SSL"){|v| @options[:sslNoVerify] = true} opts.on("--sslcacert=v", "SSL CA CERT"){|v| @options[:sslCaCert] = v} opts.on("--debug", "debug print"){@debug= true } } end |
#parse_options(args) ⇒ Object
71 72 73 74 |
# File 'lib/gfspark/config.rb', line 71 def (args) @opt_parse_obj.parse!(args) args end |
#set_ssl_options! ⇒ Object
42 43 44 45 46 47 48 49 50 |
# File 'lib/gfspark/config.rb', line 42 def @ssl_options = {} if @options.key?(:sslNoVerify) && RUBY_VERSION < "1.9.0" @ssl_options[:ssl_verify_mode] = OpenSSL::SSL::VERIFY_NONE end if @options.key?(:sslCaCert) @ssl_options[:ssl_ca_cert] = @options[:sslCaCert] end end |
#try_default(args) ⇒ Object
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/gfspark/config.rb', line 31 def try_default(args) @service = args.shift @section = args.shift @graph = args.shift @options[:t] = args.shift if range_arg?(args.first) (args) true end |
#try_path(args) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/gfspark/config.rb', line 18 def try_path(args) return false unless args.first=~ %r(^([^/]+)/([^/]+)/([^/]+)$) @service = $1 @section = $2 @graph = $3 args.shift @options[:t] = args.shift if range_arg?(args.first) (args) true end |
#try_url(args) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/gfspark/config.rb', line 4 def try_url(args) return false unless args.first =~ /http?/ u = URI.parse(args.shift) @url = u.to_s.gsub(u.request_uri, '') @graph, @section, @service = u.path.split('/').reverse if u.query queries = Hash[*u.query.split("&").map{|_| k,v = _.split("=");[k.to_sym, v]}.flatten] @options.merge!(queries) end (args) true end |