Class: Strata::CLI::Generators::Policy

Inherits:
Group
  • Object
show all
Defined in:
lib/strata/cli/generators/policy.rb

Overview

Appends a new policy entry to the project’s security.yml. Receives a fully-resolved attrs hash from the Create subcommand.

Constant Summary

Constants included from Output

Output::THEME

Instance Method Summary collapse

Methods inherited from Group

exit_on_failure?, source_root

Methods included from Output

format, pastel, print_error, #print_error, print_hint, #print_hint, print_info, #print_info, print_status, #print_status, print_success, #print_success, print_warning, #print_warning, shell_for, thor_color

Instance Method Details

#append_policy_to_security_ymlObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/strata/cli/generators/policy.rb', line 14

def append_policy_to_security_yml
  security_file = "security.yml"

  unless File.exist?(security_file)
    raise Strata::CommandError,
      "security.yml not found in the current directory. Run 'strata init' first."
  end

  content = File.read(security_file)
  new_entry = format_policy_yaml(attrs)

  updated = if content.match?(/^policies:\s*\[\]/)
    # First policy — replace inline empty array with block sequence
    content.sub(/^policies:\s*\[\]/, "policies:\n#{new_entry}")
  else
    # Subsequent policy — append after existing entries
    content.rstrip + "\n#{new_entry}"
  end

  File.write(security_file, updated)
  Output.print_status(:updated, security_file, type: :success, context: self)
end