-
Notifications
You must be signed in to change notification settings - Fork 14
JSON-Output Option for stdout. #116
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
Open
Ra0R
wants to merge
7
commits into
wolfgangw:master
Choose a base branch
from
Ra0R:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
dbc8344
dcp_inspect: Added json output option
8a21b67
Update copyright year to 2026
Ra0R d07fcb1
dcp_inspect: Add JSON output option handling
43e3c25
Merge branch 'master' of https://github.com/Ra0R/backports
2862a1b
dcp_inspect: Add ANSI color code stripping for JSON output
9979a05
dcp_inspect: Refactor JSON output handling to use block syntax for AN…
0980f8c
dcp_inspect: Remove superfluous spaces
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -190,6 +190,7 @@ class Options | |
| options.verbosity = [ 'debug', 'dev' ] | ||
| options.logfile = nil | ||
| options.logfile_append = nil | ||
| options.json_output = false | ||
| options.logfile_autolog = nil | ||
| options.overwrite_logfile = false | ||
| options.verbosity_choices = [ 'quiet', 'errors', 'hints', 'siginfo', 'info', 'cpl', 'debug', 'dev', 'trace_func' ] | ||
|
|
@@ -225,6 +226,9 @@ BANNER | |
| opts.on( '--la', '--logfile-append path', String, 'Append full report to logfile at path' ) do |p| | ||
| options.logfile_append = p | ||
| end | ||
| opts.on( '--json', 'Emit structured JSON to stdout (suppresses normal output)' ) do | ||
| options.json_output = true | ||
| end | ||
| opts.on( '--autolog', "Write full report to $DCP_INSPECT_DIR. (Default: Don't)" ) do | ||
| options.logfile_autolog = true | ||
| end | ||
|
|
@@ -755,6 +759,12 @@ class String | |
| def invert; "\e[7m#{self}\e[27m" end | ||
| end | ||
|
|
||
| # strips ANSI color codes from a string | ||
| # used for json output | ||
| def strip_ansi( str ) | ||
| str.gsub( /\e\[[0-9;]*m/, '' ) | ||
| end | ||
|
|
||
| # turn items like '100KB' or '1.5 GB' to bytes | ||
| def bytes_from_nice_bytes( nice_bytes ) | ||
| parts = nice_bytes.downcase.split( /(kb|mb|gb)/ ) | ||
|
|
@@ -4611,7 +4621,7 @@ def dcp_inspect( options, arg ) | |
| info << 'Found ' + [ am_info, pkl_info, cpl_info ].join( ', ' ) | ||
| info << "#{ amount( 'Error', errors ) }#{ errors.size == 0 ? ' ✅' : ' ❌' }, #{ amount( 'Hint', hints ) }" | ||
|
|
||
| return { :errors => errors, :hints => hints, :siginfo => siginfo, :info => info, :am_files => am_files, :pkls => pkls, :cpls => cpls, :pkls_missing => pkls_missing, :cpls_missing => cpls_missing } | ||
| return { :errors => errors, :hints => hints, :siginfo => siginfo, :info => info, :am_files => am_files, :pkls => pkls, :cpls => cpls, :pkls_missing => pkls_missing, :cpls_missing => cpls_missing, :composition_summaries => composition_summaries, :packages_size_bytes => packages_size_actual } | ||
| end # dcp_inspect | ||
|
|
||
|
|
||
|
|
@@ -4707,6 +4717,14 @@ begin | |
| if ENV[ 'DCP_INSPECT_AUTOLOG' ] | ||
| options.logfile_autolog = true | ||
| end | ||
| if options.json_output | ||
| options.verbosity = [ 'quiet' ] | ||
| # TODO: Or should the JSON output be written to a file instead of stdout? Then we could have autolog and logfile options for JSON output as well. | ||
| # For now, just disable all logging except JSON output | ||
| options.logfile = nil | ||
| options.logfile_append = nil | ||
| options.logfile_autolog = nil | ||
| end | ||
| @logger = DLogger.new( prefix = '', options ) | ||
|
|
||
|
|
||
|
|
@@ -4860,6 +4878,37 @@ begin | |
|
|
||
| # Inspection | ||
| inspection = dcp_inspect( options, args[ 0 ] ) | ||
|
|
||
| # JSON output | ||
| if options.json_output | ||
| require 'json' | ||
| payload = { | ||
| :meta => { | ||
| :tool => AppName, | ||
| :version => AppVersion, | ||
| :asdcplib_version => ASDCPVersion, | ||
| :ruby => RubyVersionPlatform, | ||
| :run_at => RunDatetime.iso8601, | ||
| :path => Pathname( args[ 0 ] ).realpath.to_s, | ||
| }, | ||
| :summary => { | ||
| :assetmap_count => inspection[ :am_files ].size, | ||
| :package_count => inspection[ :pkls ].size, | ||
| :composition_count => inspection[ :cpls ].size, | ||
| :total_size_bytes => inspection[ :packages_size_bytes ], | ||
| :error_count => inspection[ :errors ].size, | ||
| :hint_count => inspection[ :hints ].size, | ||
| }, | ||
| :compositions => inspection[ :composition_summaries ], | ||
| :errors => inspection[ :errors ].map { |s| strip_ansi( s ) }, | ||
| :hints => inspection[ :hints ].map { |s| strip_ansi( s ) }, | ||
| :siginfo => inspection[ :siginfo ].map { |s| strip_ansi( s ) }, | ||
| :info => inspection[ :info ].map { |s| strip_ansi( s ) }, | ||
| } | ||
| puts JSON.pretty_generate( payload ) | ||
| exit inspection[ :errors ].size == 0 ? DCP_OK : DCP_ERROR | ||
|
Owner
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. Exiting here would leave @dcp_inspect_tmp dangling |
||
| end | ||
|
|
||
| print_inspection_messages( inspection ) unless @logger.is_quiet | ||
|
|
||
| # Remove @dcp_inspect_temp | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, should reuse the autolog and logfile mechanisms