Sha256: b4f1e38948990e849d61a01460ca7277fe10e823ebc6b795125961c187e08cfc

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 KB

Contents

module Choria
  class Colt
    module DataStructurer
      def self.structure(res) # rubocop:disable Metrics/AbcSize
        return res unless [0, 1].include? res[:statuscode]

        # If data is empty, status message is an RPC error
        if res[:data].empty?
          res[:result] = {
            _error: {
              kind: 'choria/rpc',
              msg: res[:statusmsg],
            },
          }
          return res
        end

        # data.stdout seems to always be JSON, so parse it once.
        res[:result] = JSON.parse res[:data][:stdout] unless res.dig(:data, :stdout).nil?
        res[:data].delete :stdout

        # On one side, data.stderr is filled by the remote execution stderr.
        # On the other side, error description is in JSON (ie. '_error')
        # So merge data.stderr in '_error'.'details'
        unless res.dig(:data, :stderr).nil? || res[:data][:stderr].empty?
          raise NotImplementedError, 'What to do when res[:data][:stderr] contains something?' if res[:result]['_error'].empty?

          res[:result]['_error']['details'].merge!({ 'stderr' => res[:data][:stderr].split("\n") })
        end
        res[:data].delete :stderr

        # Convert '_output' (ie. stdout) lines into array
        res[:result]['_output'] = res[:result]['_output'].split("\n") unless res.dig(:result, '_output').nil?

        res
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
choria-colt-0.7.0 lib/choria/colt/data_structurer.rb