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 sync].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 sync Download every non-ignored version not already in the destination folder 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
- #sync(package_specs) ⇒ void
- #usage_error(message_or_status) ⇒ bot
Constructor Details
#initialize(argv) ⇒ CLI
Returns a new instance of CLI.
33 34 35 36 |
# File 'lib/fhir_packages_manager/cli.rb', line 33 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).
28 29 30 |
# File 'lib/fhir_packages_manager/cli.rb', line 28 def self.run(argv) new(argv).run end |
Instance Method Details
#check(package_specs) ⇒ void
This method returns an undefined value.
129 130 131 132 133 134 135 |
# File 'lib/fhir_packages_manager/cli.rb', line 129 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
137 138 139 140 141 142 |
# File 'lib/fhir_packages_manager/cli.rb', line 137 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.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/fhir_packages_manager/cli.rb', line 72 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.
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/fhir_packages_manager/cli.rb', line 52 def dispatch(command, package_specs) case command when 'fetch' fetch(package_specs) when 'check' check(package_specs) when 'list' list(package_specs) when 'sync' sync(package_specs) end end |
#fetch(package_specs) ⇒ void
This method returns an undefined value.
111 112 113 114 115 |
# File 'lib/fhir_packages_manager/cli.rb', line 111 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?
117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/fhir_packages_manager/cli.rb', line 117 def fetch_line(result) package = result.package path = result.path case result.status when :downloaded then "OK #{package} -> #{path} (#{result.registry})" when :ignored then "SKIP #{package} (ignored)" when :skipped then "SKIP #{package} (already exists at #{path})" 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.
144 145 146 147 148 149 |
# File 'lib/fhir_packages_manager/cli.rb', line 144 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]
151 152 153 154 155 156 157 |
# File 'lib/fhir_packages_manager/cli.rb', line 151 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?
106 107 108 109 |
# File 'lib/fhir_packages_manager/cli.rb', line 106 def load_ignore_list ignore_file = @options[:ignore_file] IgnoreList.load(ignore_file) if ignore_file end |
#manager ⇒ Manager
98 99 100 101 102 103 104 |
# File 'lib/fhir_packages_manager/cli.rb', line 98 def manager @manager ||= Manager.new( registries: @options[:registries], destination: @options[:destination], ignore_list: load_ignore_list ) end |
#parser ⇒ OptionParser
65 66 67 68 69 70 |
# File 'lib/fhir_packages_manager/cli.rb', line 65 def parser @parser ||= OptionParser.new do |opts| opts. = BANNER (opts) end end |
#run ⇒ void
This method returns an undefined value.
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/fhir_packages_manager/cli.rb', line 39 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 |
#sync(package_specs) ⇒ void
This method returns an undefined value.
159 160 161 162 163 |
# File 'lib/fhir_packages_manager/cli.rb', line 159 def sync(package_specs) results = package_specs.flat_map { |spec| manager.sync(Package.parse(spec).name) } results.each { |result| puts fetch_line(result) } exit 1 if results.any? { |result| result.not_found? || result.error? } end |
#usage_error(message_or_status) ⇒ bot
89 90 91 92 93 94 95 96 |
# File 'lib/fhir_packages_manager/cli.rb', line 89 def usage_error() if .is_a?(String) warn else warn parser end exit 1 end |