Class: SkullIsland::CLI

Inherits:
Thor
  • Object
show all
Includes:
Helpers::Migration
Defined in:
lib/skull_island/cli.rb

Overview

Base CLI for SkullIsland

Instance Method Summary collapse

Methods included from Helpers::Migration

#migrate_0_14_to_1_1, #migrate_config

Instance Method Details

#export(output_file = '-') ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/skull_island/cli.rb', line 18

def export(output_file = '-')
  if output_file == '-'
    STDERR.puts '[INFO] Outputting to STDOUT' if options['verbose']
  else
    full_filename = File.expand_path(output_file)
    dirname = File.dirname(full_filename)
    unless File.exist?(dirname) && File.ftype(dirname) == 'directory'
      raise Exceptions::InvalidArguments, "#{full_filename} is invalid"
    end
  end

  validate_server_version

  output = { 'version' => '1.1' }

  [
    Resources::Certificate,
    Resources::Consumer,
    Resources::Upstream,
    Resources::Service,
    Resources::Plugin
  ].each { |clname| export_class(clname, output) }

  if output_file == '-'
    STDOUT.puts output.to_yaml
  else
    File.write(full_filename, output.to_yaml)
  end
end

#import(input_file = '-') ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/skull_island/cli.rb', line 50

def import(input_file = '-')
  raw ||= acquire_input(input_file, options['verbose'])

  # rubocop:disable Security/YAMLLoad
  input = YAML.load(raw)
  # rubocop:enable Security/YAMLLoad

  validate_config_version input['version']

  [
    Resources::Certificate,
    Resources::Consumer,
    Resources::Upstream,
    Resources::Service,
    Resources::Plugin
  ].each { |clname| import_class(clname, input) }
end

#migrate(input_file = '-', output_file = '-') ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/skull_island/cli.rb', line 72

def migrate(input_file = '-', output_file = '-')
  raw ||= acquire_input(input_file, options['verbose'])

  # rubocop:disable Security/YAMLLoad
  input = YAML.load(raw)
  # rubocop:enable Security/YAMLLoad

  validate_migrate_version input['version']

  output = migrate_config(input)

  if output_file == '-'
    STDERR.puts '[INFO] Outputting to STDOUT' if options['verbose']
    STDOUT.puts output.to_yaml
  else
    full_filename = File.expand_path(output_file)
    dirname = File.dirname(full_filename)
    unless File.exist?(dirname) && File.ftype(dirname) == 'directory'
      raise Exceptions::InvalidArguments, "#{full_filename} is invalid"
    end

    File.write(full_filename, output.to_yaml)
  end
end