Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: fog/fog
base: master
...
head fork: fog/fog
compare: cli
Checking mergeability… Don't worry, you can still create the pull request.
  • 3 commits
  • 3 files changed
  • 0 commit comments
  • 1 contributor
Showing with 121 additions and 21 deletions.
  1. +96 −21 bin/fog
  2. +17 −0 tests/bin/cli_tests.rb
  3. +8 −0 tests/bin/helper.rb
View
117 bin/fog
@@ -1,34 +1,74 @@
#!/usr/bin/env ruby
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'fog'))
-require 'irb'
-require 'yaml'
-Fog.credential = ARGV.first ? ARGV.first.to_sym : nil
+
Fog.mock! if ENV['FOG_MOCK']
-if Fog.credentials.empty?
- begin
- Fog::Errors.missing_credentials
- rescue Fog::Errors::LoadError => error
- abort error.message
+
+# fogcli provider:service:collection:command arguments flags
+
+args = ARGV
+
+if args.first.include?(':')
+ # assume first arg with colons is a command
+ command = args.shift
+elsif args.empty? || args.all? {|arg| arg =~ /^--/}
+ # only flags implies interactive
+ unless args.include?('--interactive')
+ args << '--interactive'
end
end
-require 'fog/bin'
+# parse arguments and flags
+# arguments are positional
+# arguments containing = are split and added to a last argument hash
+# flags begin with '--'
+arguments, flags = [], []
+args.each do |arg|
+ case arg
+ when /^--/
+ flags << arg
+ when /=/
+ unless arguments.last.is_a?(Hash)
+ arguments << {}
+ end
+ key, value = arg.split('=')
+ arguments.last[key] = value
+ else
+ arguments << arg
+ end
+end
-providers = Fog.available_providers
-providers = if providers.length > 1
- providers[0...-1].join(', ') << ' and ' << providers[-1]
-else
- providers.first
+# parse flags
+flags.each do |flag|
+ case flag
+ when '--mock'
+ Fog.mock!
+ when /=/
+ key, value = flag.split('=')
+ case key
+ when '--credential'
+ Fog.credential = value.to_sym
+ else
+ Formatador.display_line("[red]Error! Unknown flag #{key} with value #{value}.[/]")
+ return
+ end
+ end
end
-if ARGV.length > 1
- require 'multi_json'
+# check for credentials and bomb out if they are missing
+if Fog.credentials.empty?
+ begin
+ Fog::Errors.missing_credentials
+ rescue Fog::Errors::LoadError => error
+ abort error.message
+ end
+end
- result = instance_eval(ARGV[1..-1].join(' '))
- puts(MultiJson.encode(result))
+# drop into interactive, raise error, or run command
+if flags.include?('--interactive')
-else
+ require 'fog/bin'
+ require 'irb'
ARGV.clear # Avoid passing args to IRB
IRB.setup(nil)
@@ -40,8 +80,13 @@ else
@irb.context.workspace = IRB::WorkSpace.new(binding)
Formatador.display_line('Welcome to fog interactive!')
- Formatador.display_line(":#{Fog.credential} provides #{providers}")
- providers = Fog.providers
+ providers = Fog.available_providers
+ providers_listing = if providers.length > 1
+ providers[0...-1].join(', ') << ' and ' << providers[-1]
+ else
+ providers.first
+ end
+ Formatador.display_line(":#{Fog.credential} provides #{providers_listing}")
# FIXME: hacks until we can `include Fog` in bin
CDN = Fog::CDN
@@ -51,4 +96,34 @@ else
catch(:IRB_EXIT) { @irb.eval_input }
+elsif command.nil?
+
+ Formatador.display_line('[red]Error! Expected either a `provider:service:collection:command` or `--interactive`.[/]')
+
+else # CLI
+
+ provider_symbol, service_symbol, collection_symbol, method_symbol = command.split(':').map {|string| string.to_sym}
+
+ require "fog/#{provider_symbol}"
+
+ if [:cdn, :compute, :dns, :storage].include?(service_symbol)
+ service_string = service_symbol.to_s
+ service = Fog.const_get(service_string[0..0].upcase + service_string[1..-1])
+ connection = service[provider_symbol]
+ else
+ provider = Fog.const_get(Fog.providers[provider_symbol])
+ require "fog/#{provider_symbol}/#{service_symbol}"
+ connection = provider[service_symbol]
+ end
+
+ result = if method_symbol
+ collection = connection.send(collection_symbol)
+ collection.send(method_symbol, *arguments)
+ else
+ # must be a request, instead of a collection, or implied #all
+ connection.send(collection_symbol, *arguments)
+ end
+
+ p result
+
end
View
17 tests/bin/cli_tests.rb
@@ -0,0 +1,17 @@
+Shindo.tests('cli_tests', 'cli') do
+
+ Thread.current[:formatador] = Formatador.new # reset indentation
+
+ EMPTY_SERVER_LISTING = Fog::Compute[:aws].servers.all.inspect + "\n"
+
+ empty_server_listing_implicit_arguments = 'aws:compute:servers --mock'
+ tests(empty_server_listing_implicit_arguments).returns(EMPTY_SERVER_LISTING) do
+ bin(empty_server_listing_implicit_arguments)
+ end
+
+ empty_server_listing_explicit_arguments = 'aws:compute:servers:all --mock'
+ tests(empty_server_listing_explicit_arguments).returns(EMPTY_SERVER_LISTING) do
+ bin(empty_server_listing_explicit_arguments)
+ end
+
+end
View
8 tests/bin/helper.rb
@@ -0,0 +1,8 @@
+require 'fog'
+require 'fog/bin' # for available_providers
+
+BIN = File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'bin', 'fog'))
+
+def bin(arguments)
+ `#{BIN} #{arguments}`
+end

No commit comments for this range

Something went wrong with that request. Please try again.