From b92d4b21a16600748d72f25349cb50443f1582d1 Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Sun, 31 May 2015 20:39:54 -0700 Subject: [PATCH] Add UI::Interface#color? --- lib/vagrant/ui.rb | 10 ++++++++++ plugins/provisioners/ansible/provisioner.rb | 8 ++++---- plugins/provisioners/chef/provisioner/base.rb | 2 +- plugins/provisioners/puppet/provisioner/puppet.rb | 2 +- .../provisioners/puppet/provisioner/puppet_server.rb | 2 +- test/unit/vagrant/ui_test.rb | 12 ++++++++++++ 6 files changed, 29 insertions(+), 7 deletions(-) diff --git a/lib/vagrant/ui.rb b/lib/vagrant/ui.rb index d26633f2f..ba982bc05 100644 --- a/lib/vagrant/ui.rb +++ b/lib/vagrant/ui.rb @@ -63,6 +63,11 @@ module Vagrant define_method(method) { |*args| } end + # @return [false] + def color? + return false + end + # For machine-readable output. # # @param [String] type The type of the data @@ -336,6 +341,11 @@ module Vagrant white: 37, } + # @return [true] + def color? + return true + end + # This is called by `say` to format the message for output. def format_message(type, message, **opts) # Get the format of the message before adding color. diff --git a/plugins/provisioners/ansible/provisioner.rb b/plugins/provisioners/ansible/provisioner.rb index b328e13d8..15e915d19 100644 --- a/plugins/provisioners/ansible/provisioner.rb +++ b/plugins/provisioners/ansible/provisioner.rb @@ -73,10 +73,10 @@ module VagrantPlugins # When Ansible output is piped in Vagrant integration, its default colorization is # automatically disabled and the only way to re-enable colors is to use ANSIBLE_FORCE_COLOR. - env["ANSIBLE_FORCE_COLOR"] = "true" if @machine.env.ui.is_a?(Vagrant::UI::Colored) + env["ANSIBLE_FORCE_COLOR"] = "true" if @machine.env.ui.color? # Setting ANSIBLE_NOCOLOR is "unnecessary" at the moment, but this could change in the future # (e.g. local provisioner [GH-2103], possible change in vagrant/ansible integration, etc.) - env["ANSIBLE_NOCOLOR"] = "true" unless @machine.env.ui.is_a?(Vagrant::UI::Colored) + env["ANSIBLE_NOCOLOR"] = "true" if !@machine.env.ui.color? # ANSIBLE_SSH_ARGS is required for Multiple SSH keys, SSH forwarding and custom SSH settings env["ANSIBLE_SSH_ARGS"] = ansible_ssh_args unless ansible_ssh_args.empty? @@ -211,7 +211,7 @@ module VagrantPlugins " -p #{docker_host_ssh_info[:port]} -i #{docker_host_ssh_info[:private_key_path][0]}" # Use same options than plugins/providers/docker/communicator.rb - # Note: this could be improved (DRY'ed) by sharing these settings. + # Note: this could be improved (DRY'ed) by sharing these settings. proxy_cmd += " -o Compression=yes -o ConnectTimeout=5 -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" proxy_cmd += " -o ForwardAgent=yes" if @ssh_info[:forward_agent] @@ -226,7 +226,7 @@ module VagrantPlugins # Set IdentitiesOnly=yes to avoid authentication errors when the host has more than 5 ssh keys. # Notes: - # - Solaris/OpenSolaris/Illumos uses SunSSH which doesn't support the IdentitiesOnly option. + # - Solaris/OpenSolaris/Illumos uses SunSSH which doesn't support the IdentitiesOnly option. # - this could be improved by sharing logic with lib/vagrant/util/ssh.rb ssh_options << "-o IdentitiesOnly=yes" unless Vagrant::Util::Platform.solaris? diff --git a/plugins/provisioners/chef/provisioner/base.rb b/plugins/provisioners/chef/provisioner/base.rb index aed44eaf0..c7d6aa3a7 100644 --- a/plugins/provisioners/chef/provisioner/base.rb +++ b/plugins/provisioners/chef/provisioner/base.rb @@ -54,7 +54,7 @@ module VagrantPlugins # This returns the command to run Chef for the given client # type. def build_command(client) - builder = CommandBuilder.new(@config, client, windows?, @machine.env.ui.is_a?(Vagrant::UI::Colored)) + builder = CommandBuilder.new(@config, client, windows?, @machine.env.ui.color?) return builder.build_command end diff --git a/plugins/provisioners/puppet/provisioner/puppet.rb b/plugins/provisioners/puppet/provisioner/puppet.rb index 36b5261f9..05d7d2bd8 100644 --- a/plugins/provisioners/puppet/provisioner/puppet.rb +++ b/plugins/provisioners/puppet/provisioner/puppet.rb @@ -182,7 +182,7 @@ module VagrantPlugins options << "--hiera_config=#{@hiera_config_path}" end - if !@machine.env.ui.is_a?(Vagrant::UI::Colored) + if !@machine.env.ui.color? options << "--color=false" end diff --git a/plugins/provisioners/puppet/provisioner/puppet_server.rb b/plugins/provisioners/puppet/provisioner/puppet_server.rb index ea3a67308..9c9e9e6b7 100644 --- a/plugins/provisioners/puppet/provisioner/puppet_server.rb +++ b/plugins/provisioners/puppet/provisioner/puppet_server.rb @@ -68,7 +68,7 @@ module VagrantPlugins end # Disable colors if we must - if !@machine.env.ui.is_a?(Vagrant::UI::Colored) + if !@machine.env.ui.color? options << "--color=false" end diff --git a/test/unit/vagrant/ui_test.rb b/test/unit/vagrant/ui_test.rb index b4a7d7d89..e7f2299dd 100644 --- a/test/unit/vagrant/ui_test.rb +++ b/test/unit/vagrant/ui_test.rb @@ -73,6 +73,12 @@ describe Vagrant::UI::Basic do end end + context "#color?" do + it "returns false" do + expect(subject.color?).to be(false) + end + end + context "#detail" do it "outputs details" do expect(subject).to receive(:safe_puts).with { |message, **opts| @@ -95,6 +101,12 @@ end describe Vagrant::UI::Colored do include_context "unit" + describe "#color?" do + it "returns true" do + expect(subject.color?).to be(true) + end + end + describe "#detail" do it "colors output nothing by default" do expect(subject).to receive(:safe_puts).with("\033[0mfoo\033[0m", anything)