bin/mdtranslator in adiwg-mdtranslator-1.4.0 vs bin/mdtranslator in adiwg-mdtranslator-2.0.0rc2
- old
+ new
@@ -1,211 +1,9 @@
#!/usr/bin/env ruby
# ADIwg mdTranslator - Thor CLI for mdtranslator
# History:
-# Stan Smith 2014-07-15 original script
-# Stan Smith 2014-09-02 changed name to mdtranslator
-# Stan Smith 2014-09-21 coded cli to 0.8.0 api
-# Stan Smith 2014-10-09 added version command to the CLI
-# Stan Smith 2014-12-29 changed default reader from adiwgJson to mdJson
-# Stan Smith 2014-01-16 changed ADIWG::Mdtranslator.translate() to keyword parameter list
-# Stan Smith 2015-07-17 added method_options to provide custom CSS
-
-require 'thor'
+# Stan Smith 2017-04-22 original script
require 'adiwg-mdtranslator'
+require_relative '../lib/adiwg/mdtranslator_cli'
-class Mdtranslator < Thor
-
- # exit_on_failure added to exit with code 1 if thor cannot complete task
- # such as if required parameters are missing
- def self.exit_on_failure?
- true
- end
-
- # basic cli description
- desc 'translate [FILE]', %q{Pass JSON string or filename plus parameters to mdtranslator translate}
- long_desc <<-LONGDESC
- 'mdtranslator translate' provides command line access to the ADIwg metadata translator, mdTranslator. The 'translate'
-method converts input metadata to supported established metadata metadata formats. The CLI accepts an input metadata
-file with options to select the input reader format, writer output format, display empty tags
-in XML outputs, and choose level of validation for mdJson input files.
- LONGDESC
- # define cli options
- method_option :reader, :aliases => '-r', :desc => 'Reader to read your input metadata file', :enum => %w{mdJson sbJson}, :required => true
- method_option :writer, :aliases => '-w', :desc => 'Writer to create your output metadata file, leave blank to validate input only', :enum => %w{iso19115_2 iso19110 html mdJson sbJson}
- method_option :validate, :aliases => '-v', :desc => 'Specify level of validation to be performed', :enum => %w{none normal strict}, :default => 'normal'
- method_option :showAllTags, :aliases => '-s', :desc => 'Include tags for unused attributes', :type => :boolean, :default => false
- method_option :messages, :aliases => '-m', :desc => 'On error return messages as formatted text or json object', :enum => %w{json text}, :default => 'text'
- method_option :returnObject, :aliases => '-o', :desc => 'Return full JSON object generated by translator', :type => :boolean, :default => false
- method_option :cssLink, :desc => 'Fully qualified link to a CSS file to customize HTML writer output', :type => :string
- method_option :css, :desc => 'Local CSS file or inline CSS to customize HTML writer output', :type => :string
-
- # accept command and options
- def translate(file)
-
- # test to see if file parameter is a local file name
- # if not ... assumed it is a JSON string
- # note: this will need to be modified if/when a reader is added that is not in JSON format
- if File.exist?(file)
- # read file
- my_file = File.open(file, 'r')
- fileObj = my_file.read
- my_file.close
- else
- fileObj = file
- end
-
- # test to see if css parameter is a local file name
- # if not ... assumed it is a css string
- css = options[:css]
- if css
- if File.exist?(css)
- # read file
- my_file = File.open(css, 'r')
- cssObj = my_file.read
- my_file.close
- else
- cssObj = css
- end
- end
-
- # for testing parameters
- # puts 'reader: ' + options[:reader]
- # puts 'writer: ' + options[:writer]
- # puts 'validation level: ' + options[:validate]
- # puts 'showAllTags: ' + options[:showAllTags].to_s
- # puts 'message format: ' + options[:messages]
- # puts 'return object: ' + options[:returnObject].to_s
- # puts 'css link: ' + options[:cssLink]
- # puts 'css: ' + options[:css]
-
- # call mdtranslator
- mdReturn = ADIWG::Mdtranslator.translate(
- file: fileObj,
- reader: options[:reader],
- writer: options[:writer],
- validate: options[:validate],
- showAllTags: options[:showAllTags],
- css: cssObj,
- cssLink: options[:cssLink])
-
- # determine return content and type of return ...
- if mdReturn[:readerStructurePass] && mdReturn[:readerValidationPass]
-
- # no problem was found with the input file
- if options[:writer].nil?
- # if no writer was specified the input was being validated only,
- # ...no writer output will have been generated,
- # ...and the return will be a string unless json was requested
- if options[:messages] == 'json'
- $stdout.write mdReturn.to_json
- return
- else
- $stdout.write 'Success'
- return
- end
- else
- # a writer was specified,
- # output is expected from the translator's writer
- if mdReturn[:writerPass]
- # writer output was generated
- # ...return the writer output in its native format unless json was requested
- if options[:returnObject]
- $stdout.write mdReturn.to_json
- return
- else
- $stdout.write mdReturn[:writerOutput].to_s
- return
- end
- else
- # the writer failed or generated warnings to be reported
- # ...return the messages as a string unless json was requested
- if options[:messages] == 'json'
- $stdout.write mdReturn.to_json
- return
- else
- # build a string with messages issues from parser and validator
- s = ''
- s += "Failed\n"
- s += "Writer failed to generate output or issued significant warnings\n"
- s += "See following messages for further information\n"
-
- # post structure messages
- i = 0
- mdReturn[:writerMessages].each do |message|
- i += 1
- s += "\nMessage: #{i}\n"
- s += message + "\n"
- end
-
- $stdout.write s
- return
-
- end
- end
- end
-
- else
-
- # problems were found with the input file
-
- # if no writer was specified the input was being validated only,
- # ...no writer output will have been generated,
- # ...and return is always expected to be a string
- if options[:messages] == 'json'
- $stdout.write mdReturn.to_json
- return
- else
- # build a string with messages issues from parser and validator
- s = ''
- s += "Failed\n"
- s += "Input failed to pass either file structure validation or content does not match schema\n"
- s += "See following messages for further information\n"
-
- # post structure messages
- if mdReturn[:readerStructurePass]
- s += "Success - Input structure is valid\n"
- else
- s += "Fail - Structure of input file is invalid - see following message(s):\n"
- i = 0
- mdReturn[:readerStructureMessages].each do |message|
- i += 1
- s += "\nMessage: #{i}\n"
- s += message.to_s + "\n"
- end
- end
-
- # post validator messages
- unless mdReturn[:readerValidationPass].nil?
- if mdReturn[:readerValidationPass]
- s += "Success - Input content passes schema definition\n"
- else
- s += "Fail - Input content did not pass schema validation - see following message(s):\n"
- i = 0
- mdReturn[:readerValidationMessages].each do |message|
- i += 1
- s += "\nMessage: #{i}\n"
- s += message.to_s + "\n"
- end
- end
- end
-
- $stdout.write s
- return
-
- end
- end
-
- end
-
- desc 'version', %q{Returns the version of mdTranslator}
- long_desc <<-LONGDESC
- 'mdtranslator version' returns the version number for mdTranslator
- LONGDESC
-
- def version
- $stdout.write ADIWG::Mdtranslator::VERSION
- end
-
- Mdtranslator.start(ARGV)
-
-end
+MdtranslatorCLI.start(ARGV)