Class: Capsium::Cli::Package

Inherits:
Thor
  • Object
show all
Extended by:
ThorExt::Start
Includes:
Formatting
Defined in:
lib/capsium/cli/package.rb

Instance Method Summary collapse

Methods included from ThorExt::Start

extended, start

Instance Method Details

#decrypt(path_to_package) ⇒ Object

Raises:

  • (Thor::Error)


157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/capsium/cli/package.rb', line 157

def decrypt(path_to_package)
  key = options[:private_key] || options[:key]
  raise Thor::Error, "decrypt requires --private-key (RSA) or --key (OpenPGP)" if key.nil?

  output = options[:output] || "#{File.basename(path_to_package, '.cap')}-decrypted.cap"
  cipher = if options[:openpgp]
             Capsium::Package::OpenPgpCipher.new
           else
             Capsium::Package::Cipher.for_encrypted(path_to_package)
           end
  cipher.decrypt(path_to_package, key, output)
  puts "Package decrypted: #{output}"
end

#encrypt(path_to_package) ⇒ Object

Raises:

  • (Thor::Error)


140
141
142
143
144
145
146
147
# File 'lib/capsium/cli/package.rb', line 140

def encrypt(path_to_package)
  key = options[:public_key] || options[:recipient]
  raise Thor::Error, "encrypt requires --public-key or --openpgp --recipient" if key.nil?

  cipher = options[:openpgp] ? Capsium::Package::OpenPgpCipher.new : Capsium::Package::Cipher.new
  cipher.encrypt(path_to_package, key, options[:output])
  puts "Package encrypted: #{options[:output]}"
end

#info(path_to_package) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/capsium/cli/package.rb', line 14

def info(path_to_package)
  package = Capsium::Package.new(path_to_package, store: options[:store])
  puts "Package Path: #{package.path}"
  puts "Routes: #{package.routes.to_hash}"
  puts "Manifest: #{package.manifest.to_hash}"
  print_dependency_tree(package)
end

#manifest(path_to_package) ⇒ Object



25
26
27
28
# File 'lib/capsium/cli/package.rb', line 25

def manifest(path_to_package)
  package = Capsium::Package.new(path_to_package)
  puts JSON.pretty_generate(package.manifest.to_hash)
end

#metadata(path_to_package) ⇒ Object



46
47
48
49
# File 'lib/capsium/cli/package.rb', line 46

def (path_to_package)
  package = Capsium::Package.new(path_to_package)
  puts JSON.pretty_generate(package..to_hash)
end

#pack(path_to_package) ⇒ Object



59
60
61
62
63
# File 'lib/capsium/cli/package.rb', line 59

def pack(path_to_package)
  package = Capsium::Package.new(path_to_package, store: options[:store])
  packager = Capsium::Packager.new
  packager.pack(package, options)
end

#push(path_to_package) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/capsium/cli/package.rb', line 78

def push(path_to_package)
  registry = Capsium::Registry.fetch(options[:registry] || ENV.fetch("CAPSIUM_REGISTRY", nil))
  entry = registry.push(path_to_package)
  puts "Pushed #{entry.name} #{entry.version} to #{registry.location}"
rescue Capsium::Registry::RegistryError => e
  raise Thor::Error, e.message
end

#routes(path_to_package) ⇒ Object



39
40
41
42
# File 'lib/capsium/cli/package.rb', line 39

def routes(path_to_package)
  package = Capsium::Package.new(path_to_package)
  puts JSON.pretty_generate(package.routes.to_hash)
end

#sign(path_to_package) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/capsium/cli/package.rb', line 103

def sign(path_to_package)
  if options[:openpgp] && options[:cert]
    raise Thor::Error, "--cert is only used with X.509 signing"
  end

  if options[:openpgp]
    Capsium::Package::OpenPgpSigner.sign_package(path_to_package, options[:key])
  else
    Capsium::Package::Signer.sign_package(path_to_package, options[:key], options[:cert])
  end
  puts "Package signed: #{path_to_package}"
end

#storage(path_to_package) ⇒ Object



32
33
34
35
# File 'lib/capsium/cli/package.rb', line 32

def storage(path_to_package)
  package = Capsium::Package.new(path_to_package)
  puts JSON.pretty_generate(package.storage.to_hash)
end

#test(path_to_package) ⇒ Object

Raises:

  • (Thor::Error)


173
174
175
176
177
178
179
180
181
182
# File 'lib/capsium/cli/package.rb', line 173

def test(path_to_package)
  package = Capsium::Package.new(path_to_package)
  report = Capsium::Package::Testing::TestSuite.new(package).run
  report.results.each { |result| puts format_result(result) }
  if report.results.empty?
    puts "No tests found (#{Capsium::Package::Testing::TestSuite::TESTS_DIR}/*.yaml)"
  end
  puts report.summary
  raise Thor::Error, "Package tests failed" unless report.ok?
end

#unpack(path_to_package) ⇒ Object



68
69
70
71
72
# File 'lib/capsium/cli/package.rb', line 68

def unpack(path_to_package)
  destination = options[:output] || File.basename(path_to_package, ".cap")
  Capsium::Packager.new.unpack(path_to_package, destination)
  puts "Package unpacked to: #{destination}"
end

#validate(path_to_package) ⇒ Object

Raises:

  • (Thor::Error)


88
89
90
91
92
93
94
# File 'lib/capsium/cli/package.rb', line 88

def validate(path_to_package)
  results = Capsium::Package::Validator.new(path_to_package).run
  results.each { |result| puts format_result(result) }
  return if results.all?(&:ok?)

  raise Thor::Error, "Package validation failed"
end

#verify_signature(path_to_package) ⇒ Object



121
122
123
124
125
126
127
128
129
130
# File 'lib/capsium/cli/package.rb', line 121

def verify_signature(path_to_package)
  verifier = options[:openpgp] ? Capsium::Package::OpenPgpSigner : Capsium::Package::Signer
  unless verifier.verify_package(path_to_package, options[:cert])
    raise Thor::Error, "Signature verification failed: #{path_to_package}"
  end

  puts "Signature valid: #{path_to_package}"
rescue Capsium::Package::Signer::SignatureError => e
  raise Thor::Error, e.message
end