Sha256: fdb4d19fad07c4ac2042aba2461fc79f9ef5c6a8277ae5b7fa346142521682d0

Contents?: true

Size: 1.42 KB

Versions: 2

Compression:

Stored size: 1.42 KB

Contents

require "thor"
require "json"

module CodeInventory
  module CLI
    class App < Thor
      desc "combine AGENCY FILENAMES ...", "Combine multiple code.json files"
      option :replace, aliases: "-r", type: :boolean, desc: "Replace all projects' organization field values with the agency name from each existing code.json"
      option :pretty, aliases: "-p", type: :boolean, desc: "Format JSON output with linebreaks and indentation"
      def combine(agency, *filenames)
        if filenames.count == 0
          puts "You must provide at least one code.json file"
          exit 1
        end
        combined = {
          "version" => "2.0.0",
          "agency" => agency,
          "measurementType" => {
            "method" => "modules"
          },
          "releases" => []
        }
        filenames.each do |filename|
          if !File.exist? filename
            puts "File not found: #{filename}"
            exit 1
          end
          inventory = JSON.load(File.new(filename))
          projects = inventory["releases"]
          if options[:replace]
            projects.each do |project|
              project["organization"] = inventory["agency"] if !inventory["agency"].nil?
            end
          end
          combined["releases"].concat(projects)
        end
        if options["pretty"]
          puts JSON.pretty_generate(combined)
        else
          puts combined.to_json
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
codeinventory-0.3.1 lib/codeinventory/cli/combine.rb
codeinventory-0.3.0 lib/codeinventory/cli/combine.rb