Class: SkullIsland::CLI

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

Overview

Base CLI for SkullIsland

Instance Method Summary collapse

Methods included from Helpers::CliErb

#erb_preprocess, #lookup

Methods included from Helpers::Migration

#migrate_0_14_to_1_1, #migrate_1_1_to_1_4, #migrate_1_4_to_1_5, #migrate_1_5_to_2_0, #migrate_config

Instance Method Details

#export(output_file = '-') ⇒ Object



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
47
48
49
50
# File 'lib/skull_island/cli.rb', line 20

def export(output_file = '-')
  if output_file == '-'
    warn '[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' => '2.3' }
  output['project'] = options['project'] if options['project']

  [
    Resources::CACertificate,
    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



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/skull_island/cli.rb', line 55

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

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

  validate_config_version input['version']

  import_time = Time.now.utc.to_i
  input['project'] = options['project'] if options['project']

  [
    Resources::CACertificate,
    Resources::Certificate,
    Resources::Consumer,
    Resources::Upstream,
    Resources::Service,
    Resources::Plugin
  ].each do |clname|
    input[clname.route_key] = [] unless input[clname.route_key] # enforce all top-level keys
    import_class(clname, input, import_time)
  end
end

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



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/skull_island/cli.rb', line 85

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)
  output['project'] = options['project'] if options['project']

  if output_file == '-'
    warn '[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

#render(input_file = '-') ⇒ Object



136
137
138
139
# File 'lib/skull_island/cli.rb', line 136

def render(input_file = '-')
  raw ||= acquire_input(input_file, options['verbose'])
  puts erb_preprocess(raw)
end

#resetObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/skull_island/cli.rb', line 114

def reset
  unless options['force']
    puts '[ERR] Missing --force flag.'
    exit 2
  end

  if options['project'] && options['verbose']
    warn "[WARN] ! Resetting gateway for project '#{options['project']}'"
  elsif options['verbose']
    warn '[WARN] ! FULLY Resetting gateway'
  end
  [
    Resources::CACertificate,
    Resources::Certificate,
    Resources::Consumer,
    Resources::Upstream,
    Resources::Service,
    Resources::Plugin
  ].each { |clname| reset_class(clname, options['project']) }
end

#versionObject



142
143
144
145
# File 'lib/skull_island/cli.rb', line 142

def version
  puts "SkullIsland Version: #{SkullIsland::VERSION}"
  exit 1
end