-- The 20.1+ Helix Core command-line client has native JSON output via -- the 'p4 -Mj' flag. function mkJSON( argc, argv ) local p4 = P4.P4:new() p4:connect() local results = p4:run( table.unpack( argv ) ) p4:disconnect() local j = require "cjson.safe" local JSON = "[" local convErrs = "" for i, m in ipairs( results ) do local js, r = j.encode( m ) if js == nil then convErrs = convErrs .. r else JSON = JSON .. js .. "," end end JSON = string.gsub( JSON, ",$", "]" ) if string.len( convErrs ) > 0 then return false, convErrs end return JSON end function preCommand() local func = Helix.Core.Client.GetVar( "func" ) if func ~= "JSON_output" then return Helix.Core.Client.Action.PASS end local argc = Helix.Core.Client.GetVar( "argc" ) local argv = Helix.Core.Client.GetVar( "argv" ) local msg = "" local r, out = pcall( function() return mkJSON( argc, argv ) end ) if r == false then Helix.Core.Client.ClientError( out ) return Helix.Core.Client.Action.FAIL end print( out ) return Helix.Core.Client.Action.REPLACE end
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#1 | 30809 | C. Thomas Tyler |
Code drop with newer versions of Extensions. Thanks to @jason_gibon. #review-30810 @jabson_gibson |