diff --git a/lib/tugboat/middleware/list_droplets.rb b/lib/tugboat/middleware/list_droplets.rb index 75edced..977d9f9 100644 --- a/lib/tugboat/middleware/list_droplets.rb +++ b/lib/tugboat/middleware/list_droplets.rb @@ -5,15 +5,22 @@ class ListDroplets < Base def call(env) ocean = env["ocean"] - ocean.droplets.list.droplets.each do |droplet| + droplet_list = ocean.droplets.list.droplets - if droplet.status == "active" - status_color = GREEN - else - status_color = RED - end + if droplet_list.empty? + say "You don't appear to have any droplets.", :red + say "Try creating one with #{GREEN}\`tugboat create\`" + else + droplet_list.each do |droplet| + + if droplet.status == "active" + status_color = GREEN + else + status_color = RED + end - say "#{droplet.name} (ip: #{droplet.ip_address}, status: #{status_color}#{droplet.status}#{CLEAR}, region: #{droplet.region_id}, id: #{droplet.id})" + say "#{droplet.name} (ip: #{droplet.ip_address}, status: #{status_color}#{droplet.status}#{CLEAR}, region: #{droplet.region_id}, id: #{droplet.id})" + end end @app.call(env) diff --git a/spec/cli/droplets_cli_spec.rb b/spec/cli/droplets_cli_spec.rb index e532ba9..1212dcf 100644 --- a/spec/cli/droplets_cli_spec.rb +++ b/spec/cli/droplets_cli_spec.rb @@ -4,7 +4,7 @@ include_context "spec" describe "droplets" do - it "shows a list" do + it "shows a list when droplets exist" do stub_request(:get, "https://api.digitalocean.com/droplets?api_key=#{api_key}&client_id=#{client_key}"). to_return(:status => 200, :body => fixture("show_droplets")) @@ -18,6 +18,20 @@ expect(a_request(:get, "https://api.digitalocean.com/droplets?api_key=#{api_key}&client_id=#{client_key}")).to have_been_made end + + it "returns an error when no droplets exist" do + stub_request(:get, "https://api.digitalocean.com/droplets?api_key=#{api_key}&client_id=#{client_key}"). + to_return(:status => 200, :body => fixture("show_droplets_empty")) + + @cli.droplets + + expect($stdout.string).to eq <<-eos +You don't appear to have any droplets. +Try creating one with \e[32m`tugboat create` + eos + + expect(a_request(:get, "https://api.digitalocean.com/droplets?api_key=#{api_key}&client_id=#{client_key}")).to have_been_made + end end end diff --git a/spec/fixtures/show_droplets_empty.json b/spec/fixtures/show_droplets_empty.json new file mode 100644 index 0000000..bc2c10a --- /dev/null +++ b/spec/fixtures/show_droplets_empty.json @@ -0,0 +1,4 @@ +{ + "status":"OK", + "droplets":[] +} \ No newline at end of file