Class: FhirPackagesManager::CLI
- Inherits:
-
Object
- Object
- FhirPackagesManager::CLI
- Defined in:
- lib/fhir_packages_manager/cli.rb,
sig/fhir_packages_manager/cli.rbs
Overview
Command-line entry point backing the fhir_packages_manager executable.
See the "CLI" section of the README for usage examples.
Constant Summary collapse
- COMMANDS =
Returns the supported subcommands.
%w[fetch check list].freeze
- BANNER =
Returns usage text shown on --help and on invalid invocations.
<<~USAGE Usage: fhir_packages_manager COMMAND package[@version] [package[@version] ...] [options] Commands: fetch Download packages into the destination folder check Report which registry (if any) has each package/version list List every version of a package available across registries USAGE
Class Method Summary collapse
-
.run(argv) ⇒ void
Parses argv and runs the requested command (
fetchorcheck).
Instance Method Summary collapse
- #check(package_specs) ⇒ void
- #check_line(package, found) ⇒ String
- #define_options(opts) ⇒ void
- #dispatch(command, package_specs) ⇒ void
- #fetch(package_specs) ⇒ void
- #fetch_line(result) ⇒ String?
-
#initialize(argv) ⇒ CLI
constructor
A new instance of CLI.
- #list(package_specs) ⇒ void
- #list_lines(name, versions_by_registry) ⇒ Array[String]
- #load_ignore_list ⇒ IgnoreList?
- #manager ⇒ Manager
- #parser ⇒ OptionParser
- #run ⇒ void
- #usage_error(message_or_status) ⇒ bot
Constructor Details
#initialize(argv) ⇒ CLI
Returns a new instance of CLI.
32 33 34 35 |
# File 'lib/fhir_packages_manager/cli.rb', line 32 def initialize(argv) @argv = argv.dup @options = { destination: './fhir_packages', registries: [] } # : Hash[Symbol, untyped] end |
Class Method Details
.run(argv) ⇒ void
This method returns an undefined value.
Parses argv and runs the requested command (fetch or check).
27 28 29 |
# File 'lib/fhir_packages_manager/cli.rb', line 27 def self.run(argv) new(argv).run end |
Instance Method Details
#check(package_specs) ⇒ void
This method returns an undefined value.
124 125 126 127 128 129 130 |
# File 'lib/fhir_packages_manager/cli.rb', line 124 def check(package_specs) package_specs.each do |spec| package = Package.parse(spec) found = manager.find_registry(package.name, package.version) puts check_line(package, found) end end |
#check_line(package, found) ⇒ String
132 133 134 135 136 137 |
# File 'lib/fhir_packages_manager/cli.rb', line 132 def check_line(package, found) return "UNAVAILABLE #{package}" unless found registry, version = found "AVAILABLE #{package.name}@#{version} (#{registry.base_url})" end |
#define_options(opts) ⇒ void
This method returns an undefined value.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/fhir_packages_manager/cli.rb', line 69 def (opts) opts.on('-r URL', '--registry URL', 'Registry base URL (repeatable, checked in order given)') do |value| @options[:registries] << value end opts.on('-d DIR', '--destination DIR', 'Destination folder for downloaded packages (default: ./fhir_packages)') do |value| @options[:destination] = value end opts.on('-i PATH', '--ignore-file PATH', 'YAML/JSON file listing packages/versions to ignore') do |value| @options[:ignore_file] = value end opts.on('-h', '--help', 'Show this help') do puts opts exit end end |
#dispatch(command, package_specs) ⇒ void
This method returns an undefined value.
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/fhir_packages_manager/cli.rb', line 51 def dispatch(command, package_specs) case command when 'fetch' fetch(package_specs) when 'check' check(package_specs) when 'list' list(package_specs) end end |
#fetch(package_specs) ⇒ void
This method returns an undefined value.
108 109 110 111 112 |
# File 'lib/fhir_packages_manager/cli.rb', line 108 def fetch(package_specs) results = manager.fetch_all(package_specs) results.each { |result| puts fetch_line(result) } exit 1 if results.any? { |result| result.not_found? || result.error? } end |
#fetch_line(result) ⇒ String?
114 115 116 117 118 119 120 121 122 |
# File 'lib/fhir_packages_manager/cli.rb', line 114 def fetch_line(result) package = result.package case result.status when :downloaded then "OK #{package} -> #{result.path} (#{result.registry})" when :ignored then "SKIP #{package} (ignored)" when :not_found then "MISS #{package} (not found in any registry)" when :error then "ERR #{package}: #{result.error}" end end |
#list(package_specs) ⇒ void
This method returns an undefined value.
139 140 141 142 143 144 |
# File 'lib/fhir_packages_manager/cli.rb', line 139 def list(package_specs) package_specs.each do |spec| name = Package.parse(spec).name list_lines(name, manager.list_versions(name)).each { |line| puts line } end end |
#list_lines(name, versions_by_registry) ⇒ Array[String]
146 147 148 149 150 151 152 |
# File 'lib/fhir_packages_manager/cli.rb', line 146 def list_lines(name, versions_by_registry) return ["NONE #{name} (not found in any registry)"] if versions_by_registry.empty? versions_by_registry.map do |base_url, versions| "FOUND #{name} @ #{base_url}: #{versions.sort.join(', ')}" end end |
#load_ignore_list ⇒ IgnoreList?
103 104 105 106 |
# File 'lib/fhir_packages_manager/cli.rb', line 103 def load_ignore_list ignore_file = @options[:ignore_file] IgnoreList.load(ignore_file) if ignore_file end |
#manager ⇒ Manager
95 96 97 98 99 100 101 |
# File 'lib/fhir_packages_manager/cli.rb', line 95 def manager @manager ||= Manager.new( registries: @options[:registries], destination: @options[:destination], ignore_list: load_ignore_list ) end |
#parser ⇒ OptionParser
62 63 64 65 66 67 |
# File 'lib/fhir_packages_manager/cli.rb', line 62 def parser @parser ||= OptionParser.new do |opts| opts. = BANNER (opts) end end |
#run ⇒ void
This method returns an undefined value.
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/fhir_packages_manager/cli.rb', line 38 def run parser.parse!(@argv) command = @argv.shift package_specs = @argv return usage_error(1) if !COMMANDS.include?(command) || package_specs.empty? return usage_error('Error: at least one --registry URL is required') if @options[:registries].empty? dispatch(command, package_specs) end |
#usage_error(message_or_status) ⇒ bot
86 87 88 89 90 91 92 93 |
# File 'lib/fhir_packages_manager/cli.rb', line 86 def usage_error() if .is_a?(String) warn else warn parser end exit 1 end |