Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from sportngin/update
Adding update command.
- Loading branch information
Showing
8 changed files
with
242 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
require 'hashdiff' | ||
require 'opsicle/output' | ||
|
||
module Opsicle | ||
class Update | ||
attr_reader :client, :type | ||
|
||
def initialize(environment, type) | ||
@client = Client.new(environment) | ||
@type = type | ||
end | ||
|
||
def execute(values, options) | ||
before = describe | ||
update(values) | ||
after = describe | ||
print(before, after) | ||
end | ||
|
||
def describe | ||
api_method = "describe_#{@type}s" | ||
api_opts = { | ||
:"#{@type}_ids" => [client.config.opsworks_config[:"#{@type}_id"]] | ||
} | ||
client.api_call(api_method, api_opts)[:"#{@type}s"][0] | ||
end | ||
|
||
def update(values) | ||
api_method = "update_#{@type}" | ||
api_opts = values.merge(:"#{@type}_id" => client.config.opsworks_config[:"#{@type}_id"]) | ||
client.api_call(api_method, api_opts) | ||
end | ||
|
||
def print(before, after) | ||
diff = HashDiff.diff(before, after) | ||
Output.say("Changes: #{diff.size}") | ||
Output.terminal.say(Terminal::Table.new headings: %w[Change Key Before After], rows: format_diff(diff)) if diff.size > 0 | ||
end | ||
|
||
def format_diff(diff) | ||
diff.map { |change| | ||
case change[0] | ||
when '-' | ||
change.insert(3, nil) | ||
change.map! { |i| Output.format(i, :removal) } | ||
when '+' | ||
change.insert(2, nil) | ||
change.map! { |i| Output.format(i, :addition) } | ||
when '~' | ||
change.map! { |i| Output.format(i, :modification) } | ||
end | ||
change | ||
} | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
require "spec_helper" | ||
require "opsicle" | ||
|
||
module Opsicle | ||
describe Update do | ||
subject { Update.new("env", "type") } | ||
let(:client) { double } | ||
let(:env) { "env" } | ||
let(:type) { "type" } | ||
let(:values) { { :foo => "bar" } } | ||
|
||
before do | ||
allow(Client).to receive(:new).with('env') { client } | ||
allow(client).to receive_message_chain("config.opsworks_config.[]") { 123 } | ||
end | ||
|
||
context "#execute" do | ||
it "should update and print results" do | ||
allow(subject).to receive(:describe) { "tacos" } | ||
expect(subject).to receive(:print).with("tacos", "tacos") | ||
api_opts = values.merge(:type_id => 123) | ||
expect(client).to receive(:api_call).with("update_type", api_opts) | ||
subject.execute(values, api_opts) | ||
end | ||
end | ||
|
||
context "#describe" do | ||
it "should return data for type" do | ||
expect(client).to receive(:api_call).with("describe_types", :type_ids => [123]).and_return(:types => []) | ||
subject.describe | ||
end | ||
end | ||
|
||
context "#update" do | ||
it "should update values for type" do | ||
api_opts = values.merge(:type_id => 123) | ||
expect(client).to receive(:api_call).with("update_type", api_opts) | ||
subject.update(values) | ||
end | ||
end | ||
|
||
context "#print" do | ||
it "should print no changes without table" do | ||
allow(HashDiff).to receive(:diff) { [] } | ||
expect(Output).to receive(:say).with("Changes: 0") { nil } | ||
expect(Output).to_not receive(:terminal) | ||
subject.print(nil, nil) | ||
end | ||
it "should print changes with table" do | ||
allow(HashDiff).to receive(:diff) { [%w[- nyan 1], %w[+ cat 2],%w[~ taco 3 4]] } | ||
expect(Output).to receive(:say).with("Changes: 3") { nil } | ||
expect(Output).to receive_message_chain("terminal.say") | ||
subject.print(nil, nil) | ||
end | ||
end | ||
|
||
context "#format_diff" do | ||
let(:diff) { [%w[- nyan 1], %w[+ cat 2],%w[~ taco 3 4]] } | ||
let(:formatted_diff) { | ||
[%w[r- rnyan r1 r], %w[a+ acat a a2], %w[m~ mtaco m3 m4]] | ||
} | ||
|
||
it "should align columns and colorize" do | ||
allow(Output).to receive(:format).with(anything, :removal) { |arg| "r#{arg}"} | ||
allow(Output).to receive(:format).with(anything, :addition) { |arg| "a#{arg}"} | ||
allow(Output).to receive(:format).with(anything, :modification) { |arg| "m#{arg}"} | ||
expect(subject.format_diff(diff)).to eq(formatted_diff) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
require "spec_helper" | ||
require "opsicle" | ||
|
||
module Opsicle | ||
describe Output do | ||
subject { Output } | ||
let(:terminal) { double(:say => nil, :color => nil) } | ||
let(:msg) { "message" } | ||
let(:colored_msg) { "COLOURmessageCOLOUR" } | ||
|
||
before do | ||
allow(subject).to receive(:terminal).and_return(terminal) | ||
$color = true | ||
$verbose = false | ||
end | ||
|
||
context "#say" do | ||
it "should say a formatted message" do | ||
allow(terminal).to receive(:color).and_return(colored_msg) | ||
expect(terminal).to receive(:say).with(colored_msg) | ||
subject.say(msg) | ||
end | ||
it "should say a message without color" do | ||
$color = false | ||
expect(terminal).to receive(:say).with(msg) | ||
subject.say(msg) | ||
end | ||
end | ||
|
||
context "#format" do | ||
it "should color message" do | ||
allow(terminal).to receive(:color).and_return(colored_msg) | ||
expect(subject.format(msg)).to eq(colored_msg) | ||
end | ||
it "should not color message" do | ||
$color = false | ||
expect(subject.format(msg)).to eq(msg) | ||
end | ||
end | ||
|
||
context "#say_verbose" do | ||
it "should not say a verbose message" do | ||
expect(terminal).to_not receive(:say) | ||
subject.say_verbose(msg) | ||
end | ||
it "should say a verbose message" do | ||
$verbose = true | ||
expect(terminal).to receive(:say) | ||
subject.say_verbose(msg) | ||
end | ||
end | ||
|
||
context "#ask" do | ||
it "should ask" do | ||
expect(terminal).to receive(:ask) | ||
subject.ask | ||
end | ||
end | ||
|
||
end | ||
end |