From caab8d67850b92cb691fa837b661039a4675f19e Mon Sep 17 00:00:00 2001 From: Justin Ko Date: Thu, 4 Nov 2010 10:06:09 -0600 Subject: [PATCH] add api_token to project options file --- lib/relish/commands/base.rb | 32 +++++++++++++++++++------------ lib/relish/commands/config.rb | 4 +++- lib/relish/commands/push.rb | 2 +- spec/relish/commands/base_spec.rb | 13 ++----------- 4 files changed, 26 insertions(+), 25 deletions(-) diff --git a/lib/relish/commands/base.rb b/lib/relish/commands/base.rb index 3b44f2e..0ae367a 100644 --- a/lib/relish/commands/base.rb +++ b/lib/relish/commands/base.rb @@ -7,40 +7,40 @@ class Base GLOBAL_OPTIONS_FILE = File.join(File.expand_path('~'), '.relish') LOCAL_OPTIONS_FILE = '.relish' - attr_accessor :args + attr_writer :args def initialize(args = []) - @args = args + @args = clean_args(args) @param = get_param @options = get_options end def organization - @options['--organization'] || parsed_options_file['organization'] + @options['organization'] end def project - @options['--project'] || parsed_options_file['project'] + @options['project'] + end + + def api_token + @options['api_token'] end def url - "http://#{@options['--host'] || DEFAULT_HOST}/api" + "http://#{@options['host'] || DEFAULT_HOST}/api" end def resource RestClient::Resource.new(url) end - - def api_token - parsed_options_file['api_token'] - end - + def get_param - args.shift if args.size.odd? + @args.shift if @args.size.odd? end def get_options - parsed_options_file.merge(Hash[*args]) + parsed_options_file.merge(Hash[*@args]) end def parsed_options_file @@ -51,6 +51,14 @@ def parsed_options_file end end + def clean_args(args) + cleaned = [] + args.each do |arg| + cleaned << arg.sub('--', '') + end + cleaned + end + end end end \ No newline at end of file diff --git a/lib/relish/commands/config.rb b/lib/relish/commands/config.rb index 9ecd339..1ec86f9 100644 --- a/lib/relish/commands/config.rb +++ b/lib/relish/commands/config.rb @@ -15,7 +15,9 @@ def show end def add - raise args.inspect + File.open(LOCAL_OPTIONS_FILE, 'a') do |f| + f.write(YAML::dump(Hash[*@args])) + end end end diff --git a/lib/relish/commands/push.rb b/lib/relish/commands/push.rb index 9505c8e..0cb639d 100644 --- a/lib/relish/commands/push.rb +++ b/lib/relish/commands/push.rb @@ -36,7 +36,7 @@ def parameters end def version - @options['--version'] || @options['-v'] + @options['version'] end def files_as_tar_gz diff --git a/spec/relish/commands/base_spec.rb b/spec/relish/commands/base_spec.rb index 520f8a6..7c0a1de 100644 --- a/spec/relish/commands/base_spec.rb +++ b/spec/relish/commands/base_spec.rb @@ -6,7 +6,7 @@ module Command {:organization => 'rspec', :project => 'rspec-core'}.each do |meth, name| describe "##{meth}" do - context 'passed in command line as full arg' do + context 'passed in command line' do let(:base) { described_class.new(["--#{meth}", name]) } it 'returns the value' do @@ -14,15 +14,6 @@ module Command end end - context 'passed in command line as short arg' do - let(:short_arg) { meth.to_s[0,1] } - let(:base) { described_class.new(["-#{short_arg}", name]) } - - it 'returns the value' do - base.send(meth).should eq(name) - end - end - context 'contained in the options file' do let(:base) { described_class.new } @@ -123,7 +114,7 @@ module Command it 'combines the args and options file' do base.get_options.should eq( - {'project' => 'rspec-core', '--project' => 'rspec-core'} + {'project' => 'rspec-core'} ) end end