From 4c249c0ce4186baddfc5999f2509e52ad51bcd45 Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Sun, 6 Sep 2015 11:10:43 -0700 Subject: [PATCH] (#1) Use config file for defaults This commit allows vmfloaty to read from a dotfile for some simple configuration defaults --- README.md | 12 ++++++++++++ lib/vmfloaty.rb | 21 +++++++++++++++------ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 806a0f1..fb4ef6c 100644 --- a/README.md +++ b/README.md @@ -36,3 +36,15 @@ gem install vmfloaty -t, --trace Display backtrace when an error occurs ``` + +### vmfloaty dotfile + +If you do not wish to continuely specify various config options with the cli, you can have a dotfile in your home directory for some defaults. For example: + +```yaml +#file at /Users/me/.vmpooler.yml +url: 'http://vmpooler.mycompany.net' +user: 'brian' +``` + +Now vmfloaty will use those config files if no flag was specified. diff --git a/lib/vmfloaty.rb b/lib/vmfloaty.rb index 4004d10..73743a6 100644 --- a/lib/vmfloaty.rb +++ b/lib/vmfloaty.rb @@ -2,6 +2,7 @@ require 'rubygems' require 'commander' +require 'yaml' require 'vmfloaty/auth' require 'vmfloaty/http' require 'vmfloaty/pooler' @@ -13,7 +14,10 @@ class Vmfloaty program :version, '0.2.0' program :description, 'A CLI helper tool for Puppet Labs vmpooler to help you stay afloat' + config = read_config + command :get do |c| + puts config c.syntax = 'floaty get [options]' c.summary = 'Gets a vm or vms based on the os flag' c.description = '' @@ -24,13 +28,13 @@ class Vmfloaty c.option '--os STRING', String, 'Operating systems to retrieve' c.action do |args, options| token = options.token - user = options.user - url = options.url + user = options.user ||= config['user'] + url = options.url ||= config['url'] os_types = options.os pass = password "Enter your password please:", '*' unless options.token - token = Auth.get_token(user, url, password) + token = Auth.get_token(user, url, pass) end unless os_types.nil? @@ -51,7 +55,7 @@ class Vmfloaty c.action do |args, options| # Do something or c.when_called Floaty::Commands::Query filter = options.filter - url = options.url + url = options.url ||= config['url'] Pooler.list(url, filter) end @@ -88,7 +92,7 @@ class Vmfloaty c.option '--url STRING', String, 'URL of vmpooler' c.action do |args, options| hosts = options.hosts - url = options.url + url = options.url ||= config['url'] Pool.delete(hosts, url) end @@ -123,7 +127,7 @@ class Vmfloaty c.example 'Gets the current vmpooler status', 'floaty status --url http://vmpooler.example.com' c.option '--url STRING', String, 'URL of vmpooler' c.action do |args, options| - url = options.url + url = options.url ||= config['url'] Pooler.status(url) end @@ -142,4 +146,9 @@ class Vmfloaty run! end + + def read_config + conf = YAML.load_file("#{Dir.home}/.vmfloaty.yml") + conf + end end