Module: SdrClient::CLI

Defined in:
lib/sdr_client/cli.rb

Overview

The command line interface

Constant Summary collapse

HELP =
<<~HELP
  DESCRIPTION:
    The SDR Command Line Interface is a tool to interact with the Stanford Digital Repository.

  SYNOPSIS:
    sdr [options] <command>

    To see help text for each command you can run:

    sdr [options] <command> help

  OPTIONS:
    --service-url (string)
    Override the command's default URL with the given URL.

    -h, --help
    Displays this screen


  COMMANDS:
    get
      Retrieve an object from the SDR

    deposit
      Accession an object into the SDR

    register
      Create a draft object in SDR and retrieve a Druid identifier.

    login
      Will prompt for email & password and exchange it for an login token, which it saves in ~/.sdr/token

    version
      Display the sdr-client version

HELP

Class Method Summary collapse

Class Method Details

.deposit(command, options, arguments) ⇒ Object



62
63
64
65
66
67
# File 'lib/sdr_client/cli.rb', line 62

def self.deposit(command, options, arguments)
  options[:files] = arguments if arguments.present?
  display_errors(validate_deposit_options(options))
  job_id = SdrClient::Deposit.run(accession: command == 'deposit', **options)
  poll_for_job_complete(job_id: job_id, url: options[:url]) # TODO: add an option that skips this
end

.display_errors(errors) ⇒ Object



69
70
71
72
73
# File 'lib/sdr_client/cli.rb', line 69

def self.display_errors(errors)
  return if errors.empty?

  raise errors.map { |k, v| "#{k} #{v}" }.join("\n")
end

.helpObject



82
83
84
85
# File 'lib/sdr_client/cli.rb', line 82

def self.help
  puts HELP
  exit
end

.poll_for_job_complete(job_id:, url:) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/sdr_client/cli.rb', line 87

def self.poll_for_job_complete(job_id:, url:)
  result = nil
  1.upto(5) do |_n|
    result = SdrClient::BackgroundJobResults.show(url: url, job_id: job_id)
    break unless %w[pending processing].include? result['status']

    sleep 5
  end
  if result['status'] == 'complete'
    puts result.dig('output', 'druid')
  else
    warn "Job #{job_id} did not complete\n#{result.inspect}"
  end
end

.start(command, options, arguments = []) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/sdr_client/cli.rb', line 43

def self.start(command, options, arguments = [])
  case command
  when 'get'
    puts SdrClient::Find.run(arguments.first, **options)
  when 'deposit', 'register'
    deposit(command, options, arguments)
  when 'login'
    status = SdrClient::Login.run(options)
    puts status.failure if status.failure?
  when 'version'
    puts SdrClient::VERSION
  else
    raise "Unknown command #{command}"
  end
rescue SdrClient::Credentials::NoCredentialsError
  puts 'Log in first'
  exit(1)
end

.validate_deposit_options(options) ⇒ Object



75
76
77
78
79
80
# File 'lib/sdr_client/cli.rb', line 75

def self.validate_deposit_options(options)
  {}.tap do |errors|
    errors['admin-policy'] = 'is a required argument' unless options[:apo]
    errors['source-id'] = 'is a required argument' unless options[:source_id]
  end
end