#!/usr/bin/env ruby # frozen_string_literal: true $LOAD_PATH.unshift 'lib' require 'optparse' require 'sdr_client' options = {} global = OptionParser.new do |opts| opts.on('--service-url URL', 'Connect to the host at this URL') do |url| options[:url] = url end opts.on('-h', '--help', 'Display this screen') do SdrClient::CLI.help end end global.order! command = ARGV.shift deposit_options = OptionParser.new do |opts| opts.banner = "Usage: sdr #{command} [options]" opts.on('--label LABEL', 'The object label') do |label| options[:label] = label end opts.on('--admin-policy ADMIN_POLICY', 'The druid identifier of the admin policy object') do |apo| options[:apo] = apo end opts.on('--type TYPE', 'The object type to create. ' \ 'One of: "image", "book", "document", "map", "manuscript", "media", ' \ '"three_dimensional", "object", "collection", or "admin_policy"') do |type| if %w[image book document map manuscript media three_dimensional object collection admin_policy].include?(type) options[:type] = "http://cocina.sul.stanford.edu/models/#{type}.jsonld" end end opts.on('--collection COLLECTION', 'The druid identifier of the collection object') do |collection| options[:collection] = collection end opts.on('--catkey CATKEY', 'The catkey for this item') do |catkey| options[:catkey] = catkey end opts.on('--source-id SOURCE_ID', 'The source id for this object') do |source_id| options[:source_id] = source_id end opts.on('--copyright COPYRIGHT', 'The copyright statement') do |copyright| options[:copyright] = copyright end opts.on('--use-statement STATEMENT', 'The use and reproduction statement') do |use_statement| options[:use_statement] = use_statement end opts.on('--viewing-direction DIRECTION', 'The viewing direction (if a book). ' \ 'Either "left-to-right" or "right-to-left"') do |viewing_direction| options[:viewing_direction] = viewing_direction if %w[left-to-right right-to-left].include?(viewing_direction) end opts.on('--access LEVEL', 'The access level for this object. ' \ 'Either "world", "stanford", "location-based", "citation-only" or "dark"') do |level| options[:access] = level if %w[world stanford location-based citation-only dark].include?(level) end opts.on('--files-metadata FILES_METADATA', 'A JSON object representing per-file metadata') do |files_metadata| options[:files_metadata] = JSON.parse(files_metadata) end opts.on('--strategy STRATEGY', 'The strategy to use for distributing files into filesets. Either "default" or "filename"') do |strategy| strategy_class = case strategy when 'filename' SdrClient::Deposit::MatchingFileGroupingStrategy when 'default' SdrClient::Deposit::SingleFileGroupingStrategy else warn "Unknown strategy #{strategy}" exit(1) end options[:grouping_strategy] = strategy_class end opts.on('-h', '--help', 'Display this screen') do puts opts exit end end SdrClient::CLI.help unless command subcommands = { 'get' => OptionParser.new, 'deposit' => deposit_options, 'register' => deposit_options, 'login' => OptionParser.new, 'version' => OptionParser.new } unless subcommands.key?(command) puts "unknown command '#{command}'" SdrClient::CLI.help end subcommands[command].order! options[:url] ||= 'https://sdr-api-prod.stanford.edu' begin SdrClient::CLI.start(command, options, ARGV) rescue StandardError => e warn "There was a problem making your request:\n\n" warn e.message puts puts subcommands[command].help end