Method: Bolt::PlanCreator.validate_plan_name

Defined in:
lib/bolt/plan_creator.rb

.validate_plan_name(project, plan_name) ⇒ Object



10
11
12
13
14
15
16
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
# File 'lib/bolt/plan_creator.rb', line 10

def self.validate_plan_name(project, plan_name)
  if project.name.nil?
    raise Bolt::Error.new(
      "Project directory '#{project.path}' is not a named project. Unable to create "\
      "a project-level plan. To name a project, set the 'name' key in the 'bolt-project.yaml' "\
      "configuration file.",
      "bolt/unnamed-project-error"
    )
  end

  if plan_name !~ Bolt::Module::CONTENT_NAME_REGEX
    message = <<~MESSAGE.chomp
      Invalid plan name '#{plan_name}'. Plan names are composed of one or more name segments
      separated by double colons '::'.

      Each name segment must begin with a lowercase letter, and can only include lowercase
      letters, digits, and underscores.

      Examples of valid plan names:
          - #{project.name}
          - #{project.name}::my_plan
    MESSAGE

    raise Bolt::ValidationError, message
  end

  prefix, _, basename = segment_plan_name(plan_name)

  unless prefix == project.name
    message = "Incomplete plan name: A plan name must be prefixed with the name of the "\
      "project or module. Did you mean '#{project.name}::#{plan_name}'?"

    raise Bolt::ValidationError, message
  end

  %w[pp yaml].each do |ext|
    next unless (path = project.plans_path + "#{basename}.#{ext}").exist?
    raise Bolt::Error.new(
      "A plan with the name '#{plan_name}' already exists at '#{path}', nothing to do.",
      'bolt/existing-plan-error'
    )
  end
end