Skip to content

Commit

Permalink
Add a regions command
Browse files Browse the repository at this point in the history
Displays a list of regions. Closes #17.
  • Loading branch information
blom committed Apr 23, 2013
1 parent 531643a commit d9769a4
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/tugboat/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,11 @@ def keys
Middleware.sequence_ssh_keys.call({})
end

desc "regions", "Show regions"
def regions
Middleware.sequence_regions.call({})
end

desc "version", "Show version"
def version
say "Tugboat #{Tugboat::VERSION}"
Expand Down
11 changes: 11 additions & 0 deletions lib/tugboat/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module Middleware
autoload :SnapshotDroplet, "tugboat/middleware/snapshot_droplet"
autoload :ListImages, "tugboat/middleware/list_images"
autoload :ListSSHKeys, "tugboat/middleware/list_ssh_keys"
autoload :ListRegions, "tugboat/middleware/list_regions"

# Start the authorization flow.
# This writes a ~/.tugboat file, which can be edited manually.
Expand Down Expand Up @@ -140,5 +141,15 @@ def self.sequence_ssh_keys
use ListSSHKeys
end
end

# Display a list of regions
def self.sequence_regions
::Middleware::Builder.new do
use InjectConfiguration
use CheckConfiguration
use InjectClient
use ListRegions
end
end
end
end
17 changes: 17 additions & 0 deletions lib/tugboat/middleware/list_regions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Tugboat
module Middleware
class ListRegions < Base
def call(env)
ocean = env["ocean"]
regions = ocean.regions.list.regions

say "Regions:"
regions.each do |region|
say "#{region.name} (id: #{region.id})"
end

@app.call(env)
end
end
end
end
28 changes: 28 additions & 0 deletions spec/cli/regions_cli_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require 'spec_helper'

describe Tugboat::CLI do
include_context "spec"

before :each do
@cli = Tugboat::CLI.new
end

describe "regions" do
it "shows a list" do
stub_request(:get, "https://api.digitalocean.com/regions?api_key=#{api_key}&client_id=#{client_key}").
to_return(:status => 200, :body => fixture("show_regions"))

@cli.regions

expect($stdout.string).to eq <<-eos
Regions:
Region 1 (id: 1)
Region 2 (id: 2)
Region 3 (id: 3)
eos

expect(a_request(:get, "https://api.digitalocean.com/regions?api_key=#{api_key}&client_id=#{client_key}")).
to have_been_made
end
end
end
17 changes: 17 additions & 0 deletions spec/fixtures/show_regions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"status": "OK",
"regions": [
{
"id": 1,
"name": "Region 1"
},
{
"id": 2,
"name": "Region 2"
},
{
"id": 3,
"name": "Region 3"
}
]
}

0 comments on commit d9769a4

Please sign in to comment.