-
Notifications
You must be signed in to change notification settings - Fork 166
Add --deflate option to data-pull and action-account scripts #9424
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,10 +25,12 @@ def initialize(argv:, stdout:, stderr:, subtask_class:, banner:) | |
| :format, | ||
| :show_help, | ||
| :requesting_issuers, | ||
| :deflate, | ||
| keyword_init: true, | ||
| ) do | ||
| alias_method :include_missing?, :include_missing | ||
| alias_method :show_help?, :show_help | ||
| alias_method :deflate?, :deflate | ||
| end | ||
|
|
||
| def config | ||
|
|
@@ -37,6 +39,7 @@ def config | |
| format: :table, | ||
| show_help: false, | ||
| requesting_issuers: [], | ||
| deflate: false, | ||
| ) | ||
| end | ||
|
|
||
|
|
@@ -59,13 +62,23 @@ def run | |
| stderr.puts "*Messages*:\n#{result.messages.map { |message| " • #{message}" }.join("\n")}" | ||
| end | ||
|
|
||
| if result.json | ||
| if config.deflate? | ||
| require 'zlib' | ||
| require 'base64' | ||
| stdout.puts Base64.encode64( | ||
| Zlib::Deflate.deflate( | ||
| (result.json || result.table).to_json, | ||
| Zlib::BEST_COMPRESSION, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We also have MessagePack if we'd rather avoid the Base64 encoding
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. re:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just confirmed that message pack is binary, I think it'll run into the same issue that the binary DEFLATE data had, which is that SSM command seems to eat it. The original reason I chose JSON was this blog post that seemed to indicate JSON + GZIP yielded smaller payloads than MSGPACK
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah got it, sounds good! |
||
| ), | ||
| ) | ||
| elsif result.json | ||
| stdout.puts result.json.to_json | ||
| else | ||
| self.class.render_output(result.table, format: config.format, stdout: stdout) | ||
| end | ||
| end | ||
|
|
||
| # rubocop:disable Metrics/BlockLength | ||
| def option_parser | ||
| @option_parser ||= OptionParser.new do |opts| | ||
| opts.banner = banner | ||
|
|
@@ -92,13 +105,18 @@ def option_parser | |
| config.format = :json | ||
| end | ||
|
|
||
| opts.on('--deflate', 'Use DEFLATE compression on the output') do | ||
| config.deflate = true | ||
| end | ||
|
|
||
| opts.on('--[no-]include-missing', <<~STR) do |include_missing| | ||
| Whether or not to add rows in the output for missing inputs, defaults to on | ||
| STR | ||
| config.include_missing = include_missing | ||
| end | ||
| end | ||
| end | ||
| # rubocop:enable Metrics/BlockLength | ||
|
|
||
| # @param [Array<Array<String>>] rows | ||
| def self.render_output(rows, format:, stdout: STDOUT) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| require 'spec_helper' | ||
| require 'script_base' | ||
|
|
||
| RSpec.describe ScriptBase do | ||
| let(:subtask_class) do | ||
| Class.new do | ||
| def run(args:, config:) # rubocop:disable Lint/UnusedMethodArgument | ||
| ScriptBase::Result.new( | ||
| table: [ | ||
| %w[header1 header2], | ||
| %w[value1 value2], | ||
| ], | ||
| subtask: 'example-subtask', | ||
| uuids: %w[a b c], | ||
| ) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| let(:stdout) { StringIO.new } | ||
| let(:stderr) { StringIO.new } | ||
|
|
||
| describe '#run' do | ||
| let(:argv) { [] } | ||
|
|
||
| subject(:base) do | ||
| ScriptBase.new( | ||
| argv:, | ||
| stdout:, | ||
| stderr:, | ||
| subtask_class:, | ||
| banner: '', | ||
| ) | ||
| end | ||
|
|
||
| context 'with --deflate' do | ||
| let(:argv) { %w[--deflate] } | ||
|
|
||
| it 'applies DEFLATE compression to the output' do | ||
| base.run | ||
|
|
||
| table = subtask_class.new.run(args: nil, config: nil).table | ||
|
|
||
| expect(JSON.parse(Zlib::Inflate.inflate(Base64.decode64(stdout.string)))).to eq(table) | ||
| end | ||
| end | ||
| end | ||
| end |
Uh oh!
There was an error while loading. Please reload this page.