Sha256: 427ad0e464e0c04b4eb7a84ec25df168a6072cc7e23c84c55b8b9c8171f4f57a

Contents?: true

Size: 1.54 KB

Versions: 1

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true

command :build do |c|
  c.syntax = 'pk build [PASSNAME]'
  c.summary = 'Creates a .pkpass archive'
  c.description = ''

  c.example 'description', 'pk archive mypass -o mypass.pkpass'
  c.option '-c', '--certificate /path/to/cert.pem', 'Pass certificate'
  c.option '-p', '--[no]-password', 'Prompt for certificate password'
  c.option '-o', '--output /path/to/out.pkpass', '.pkpass output filepath'

  c.action do |args, options|
    determine_directory! unless @directory = args.first
    validate_directory!

    @filepath = options.output || "#{@directory}.pkpass"
    validate_output_filepath!

    @certificate = options.certificate
    validate_certificate!

    @password = ask('Enter certificate password:') { |q| q.echo = false } if options.password

    Dubai::Passbook.certificate = @certificate
    Dubai::Passbook.password = @password

    begin
      File.open(@filepath, 'w') do |f|
        f.write Dubai::Passbook::Pass.new(@directory).pkpass.string
      end
    rescue OpenSSL::PKCS12::PKCS12Error => e
      say_error "Error: #{e.message}"
      say_warning 'You may be getting this error because the certificate password is either incorrect or missing'
      abort
    rescue StandardError => e
      say_error("Error: #{e.message}") && abort
    end
  end
end

alias_command :archive, :build
alias_command :b, :build

private

def validate_output_filepath!
  say_error('Filepath required') && abort if @filepath.nil? || @filepath.empty?
  say_error("#{@filepath} already exists") && abort if File.exist?(@filepath)
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dubai-0.2.0 ./lib/dubai/commands/build.rb