Skip to content

Commit

Permalink
add a quiet output option to make it easier to use secret values in e…
Browse files Browse the repository at this point in the history
…xternal scripts
  • Loading branch information
wr0ngway committed Sep 20, 2019
1 parent 0959ba6 commit da7770a
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 16 deletions.
2 changes: 0 additions & 2 deletions lib/simplygenius/atmos.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,4 @@ module Atmos
end

require_relative 'atmos/logging'
# SimplyGenius::Atmos::Logging.setup_logging(false, false, nil)

require_relative 'atmos/config'
11 changes: 10 additions & 1 deletion lib/simplygenius/atmos/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ def self.description
:flag, "debug output\n",
default: false

option ["-q", "--quiet"],
:flag, "suppress output\n",
default: false

option ["-c", "--[no-]color"],
:flag, "colorize output (or not)\n (default: $stdout.tty?)"

Expand Down Expand Up @@ -104,7 +108,12 @@ def parse(arguments)
if Atmos.config.nil?
Atmos.config = Config.new(atmos_env, atmos_group)
log = Atmos.config.is_atmos_repo? && log? ? "atmos.log" : nil
Logging.setup_logging(debug?, color?, log)
level = :info
level = :debug if debug?
level = :error if quiet?

Logging.setup_logging(level, color?, log)

UI.color_enabled = color?

Atmos.config.add_user_load_path(*load_path_list)
Expand Down
3 changes: 2 additions & 1 deletion lib/simplygenius/atmos/commands/secret.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ def execute
Atmos.config.provider.auth_manager.authenticate(ENV) do |auth_env|
ClimateControl.modify(auth_env) do
value = Atmos.config.provider.secret_manager.get(key)
logger.info "Secret value for #{key}: #{value}"
logger.info "Secret value for #{key}:"
puts value
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/simplygenius/atmos/logging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ def self.clear
sio.try(:clear)
end

def self.setup_logging(debug, color, logfile)
def self.setup_logging(level, color, logfile)
init_logger

::Logging.logger.root.level = debug ? :debug : :info
::Logging.logger.root.level = level
appenders = []
detail_pattern = '[%d] %-5l %c{2} %m\n'
plain_pattern = '%m\n'
Expand Down
2 changes: 1 addition & 1 deletion spec/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def argv(arg)
end

it "sets log level to debug" do
expect(Logging).to receive(:setup_logging).with(true, any_args)
expect(Logging).to receive(:setup_logging).with(:debug, any_args)
cli.run(['--debug', 'version'])
end

Expand Down
4 changes: 2 additions & 2 deletions spec/commands/secret_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ module Commands
expect(Atmos.config.provider.auth_manager).to receive(:authenticate).and_yield(env)
expect(Atmos.config.provider.secret_manager).to receive(:get).
with("foo").and_return("bar")
cli.run(["get", "foo"])
expect(Logging.contents).to match(/Secret value.*foo.*bar/)
expect { cli.run(["get", "foo"]) }.to output("bar\n").to_stdout
expect(Logging.contents).to match(/Secret value for foo:\n/)
end

end
Expand Down
12 changes: 6 additions & 6 deletions spec/logging_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ module Atmos
describe "setup_logging" do

it "logs at info log level" do
described_class.setup_logging(false, false, nil)
described_class.setup_logging(:info, false, nil)
logger.info("infolog")
expect(Logging.contents).to include("infolog")
logger.debug("debuglog")
expect(Logging.contents).to_not include("debuglog")
end

it "logs at debug log level" do
described_class.setup_logging(true, false, nil)
described_class.setup_logging(:debug, false, nil)
logger.info("infolog")
expect(Logging.contents).to include("infolog")
logger.debug("debuglog")
Expand All @@ -29,7 +29,7 @@ module Atmos
it "can write to logfile" do
within_construct do |c|
expect(File.exist?('foo.log')).to be false
described_class.setup_logging(false, false, 'foo.log')
described_class.setup_logging(:info, false, 'foo.log')
logger.info("howdy")
expect(File.exist?('foo.log')).to be true
expect(File.read('foo.log')).to include("howdy")
Expand All @@ -40,22 +40,22 @@ module Atmos
it "can avoid writing to logfile" do
within_construct do |c|
expect(File.exist?('foo.log')).to be false
described_class.setup_logging(false, false, nil)
described_class.setup_logging(:info, false, nil)
logger.info("howdy")
expect(File.exist?('foo.log')).to be false
expect(Logging.contents).to include("howdy")
end
end

it "logs with color" do
described_class.setup_logging(false, true, nil)
described_class.setup_logging(:info, true, nil)
logger.info("howdy")
a = ::Logging.logger.root.appenders.find {|a| a.try(:layout).try(:color_scheme) }
expect(a).to_not be_nil
end

it "outputs plain text" do
described_class.setup_logging(false, false, nil)
described_class.setup_logging(:info, false, nil)
a = ::Logging.logger.root.appenders.find {|a| a.try(:layout).try(:color_scheme) }
expect(a).to be_nil
end
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def fixture_dir
config.before(:each) do
SimplyGenius::Atmos::UI.color_enabled = false
SimplyGenius::Atmos::Logging.testing = true
SimplyGenius::Atmos::Logging.setup_logging(true, false, nil)
SimplyGenius::Atmos::Logging.setup_logging(:debug, false, nil)
SimplyGenius::Atmos::Logging.clear
end

Expand Down

0 comments on commit da7770a

Please sign in to comment.