Class: FhirPackagesManager::CLI

Inherits:
Object
  • Object
show all
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.

Returns:

  • (Array<String>)

    the supported subcommands

%w[fetch check].freeze
<<~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

USAGE

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ CLI

Returns a new instance of CLI.

Parameters:

  • argv (Array<String>)

    see run



31
32
33
34
# File 'lib/fhir_packages_manager/cli.rb', line 31

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).

Parameters:

  • argv (Array<String>)

    arguments as passed to the executable, e.g. ARGV



26
27
28
# File 'lib/fhir_packages_manager/cli.rb', line 26

def self.run(argv)
  new(argv).run
end

Instance Method Details

#check(package_specs) ⇒ void

This method returns an undefined value.

Parameters:

  • package_specs (Array[String])


121
122
123
124
125
126
127
# File 'lib/fhir_packages_manager/cli.rb', line 121

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

Parameters:

Returns:

  • (String)


129
130
131
132
133
134
# File 'lib/fhir_packages_manager/cli.rb', line 129

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.

Parameters:

  • opts (OptionParser)


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/fhir_packages_manager/cli.rb', line 66

def define_options(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.

Parameters:

  • command (String, nil)
  • package_specs (Array[String])


50
51
52
53
54
55
56
57
# File 'lib/fhir_packages_manager/cli.rb', line 50

def dispatch(command, package_specs)
  case command
  when 'fetch'
    fetch(package_specs)
  when 'check'
    check(package_specs)
  end
end

#fetch(package_specs) ⇒ void

This method returns an undefined value.

Parameters:

  • package_specs (Array[String])


105
106
107
108
109
# File 'lib/fhir_packages_manager/cli.rb', line 105

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?

Parameters:

Returns:

  • (String, nil)


111
112
113
114
115
116
117
118
119
# File 'lib/fhir_packages_manager/cli.rb', line 111

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

#load_ignore_listIgnoreList?

Returns:



100
101
102
103
# File 'lib/fhir_packages_manager/cli.rb', line 100

def load_ignore_list
  ignore_file = @options[:ignore_file]
  IgnoreList.load(ignore_file) if ignore_file
end

#managerManager

Returns:



92
93
94
95
96
97
98
# File 'lib/fhir_packages_manager/cli.rb', line 92

def manager
  @manager ||= Manager.new(
    registries: @options[:registries],
    destination: @options[:destination],
    ignore_list: load_ignore_list
  )
end

#parserOptionParser

Returns:

  • (OptionParser)


59
60
61
62
63
64
# File 'lib/fhir_packages_manager/cli.rb', line 59

def parser
  @parser ||= OptionParser.new do |opts|
    opts.banner = BANNER
    define_options(opts)
  end
end

#runvoid

This method returns an undefined value.



37
38
39
40
41
42
43
44
45
46
# File 'lib/fhir_packages_manager/cli.rb', line 37

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

Parameters:

  • message_or_status (String, Integer)

Returns:

  • (bot)


83
84
85
86
87
88
89
90
# File 'lib/fhir_packages_manager/cli.rb', line 83

def usage_error(message_or_status)
  if message_or_status.is_a?(String)
    warn message_or_status
  else
    warn parser
  end
  exit 1
end