Class: Entitlements::Cli

Inherits:
Object
  • Object
show all
Includes:
Contracts::Core
Defined in:
lib/entitlements/cli.rb

Constant Summary collapse

C =
::Contracts

Class Method Summary collapse

Class Method Details

.initialize_optionsObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/entitlements/cli.rb', line 80

def self.initialize_options
  Optimist.options do
    banner <<-EOS
    Configure authentication providers to look like the configuration declared in the files.

    Usage:

    $ deploy-entitlements --dir /path/to/configurations
    .
  EOS
    opt :"config-file",
        "Configuration file for application",
        type: :string,
        default: File.expand_path("../../config/entitlements/config.yaml", File.dirname(__FILE__))
    opt :noop,
        "no-op mode (do not actually apply configurations)",
        type: :boolean, default: false
    opt :full,
        "full deployment (skip predictive updates, if configured)",
        type: :boolean, default: false
    opt :debug,
        "debug messages enabled",
        type: :boolean, default: false
  end
end

.optionsObject



64
65
66
67
68
69
70
# File 'lib/entitlements/cli.rb', line 64

def self.options
  @options ||= begin
    o = initialize_options
    validate_options!(o)
    o
  end
end

.runObject



17
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
47
48
49
50
51
52
53
54
# File 'lib/entitlements/cli.rb', line 17

def self.run
  # Establish the logger object. Debugging is enabled or disabled based on options.
  logger = Logger.new(STDERR)
  logger.level = options[:debug] ? Logger::DEBUG : Logger::INFO
  Entitlements.set_logger(logger)

  # Set up configuration file.
  Entitlements.config_file = options[:"config-file"]
  Entitlements.validate_configuration_file!

  # Support predictive updates based on the environment, but allow the --full option to
  # suppress setting and using it.
  if ENV["ENTITLEMENTS_PREDICTIVE_STATE_DIR"] && !@options[:full]
    Entitlements::Data::Groups::Cached.load_caches(ENV["ENTITLEMENTS_PREDICTIVE_STATE_DIR"])
  end

  # Calculate differences.
  actions = Entitlements.calculate

  # No-op mode exits here.
  if @options[:noop]
    logger.info "No-op mode is set. Would make #{Entitlements.cache[:change_count]} change(s)."
    return 0
  end

  # No changes?
  if actions.empty?
    logger.info "No changes to be made. You're all set, friend! :sparkles:"
    return 0
  end

  # Execute the changes. This raises if it fails to apply a change or if auditors fail.
  Entitlements.execute(actions: actions)

  # Done.
  logger.info "Successfully applied #{Entitlements.cache[:change_count]} change(s)!"
  0
end

.validate_options!(options) ⇒ Object



114
115
116
117
118
# File 'lib/entitlements/cli.rb', line 114

def self.validate_options!(options)
  unless options[:"config-file"].is_a?(String) && File.file?(options[:"config-file"])
    raise ArgumentError, "Expected --config-file to be a valid file!"
  end
end