lib/bolt/application.rb
# Add a new policy to the project.
#
# @param name [String] The name of the new policy.
# @return [Hash]
#
Add a new policy to the project.
Add a new policy to the project .
def new_policy(name)
      unless name =~ Bolt::Module::CONTENT_NAME_REGEX
        message = <<~MESSAGE.chomp
          Invalid policy name '#{name}'. Policy 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 policy names:
              - #{@config.project.name}
              - #{@config.project.name}::my_policy
        MESSAGE
        raise Bolt::ValidationError, message
      end
      if @config.project.name.nil?
        command = Bolt::Util.powershell? ? 'New-BoltProject -Name <NAME>' : 'bolt project init <NAME>'
        message = <<~MESSAGE.chomp
          Can't create a policy for the default Bolt project because it doesn't
          have a name. Run '#{command}' to create a new project.
        MESSAGE
        raise Bolt::ValidationError, message
      end
      prefix, *name_segments, basename = name.split('::')
      unless prefix == @config.project.name
        raise Bolt::ValidationError,
              "Policy name '#{name}' must begin with project name '#{@config.project.name}'. Did "\
              "you mean '#{@config.project.name}::#{name}'?"
      end
      basename ||= 'init'
      policy_dir = File.expand_path(File.join(name_segments), @config.project.manifests)
      policy     = File.expand_path("#{basename}.pp", policy_dir)
      if File.exist?(policy)
        raise Bolt::Error.new(
          "A policy with the name '#{name}' already exists at '#{policy}', nothing to do.",
          'bolt/existing-policy-error'
        )
      end
      begin
        FileUtils.mkdir_p(policy_dir)
      rescue Errno::EEXIST => e
        raise Bolt::Error.new(
          "#{e.message}; unable to create manifests directory '#{policy_dir}'",
          'bolt/existing-file-error'
        )
      end
      begin
        File.write(policy, <<~POLICY)
          class #{name} {
          }
        POLICY
      rescue Errno::EACCES => e
        raise Bolt::FileError.new("#{e.message}; unable to create policy", policy)
      end
      project_config = Bolt::Util.read_yaml_hash(@config.project.project_file, 'project config')
      project_config['policies'] ||= []
      project_config['policies'] <<  name
      project_config['policies'].uniq!
      begin
        File.write(@config.project.project_file, project_config.to_yaml)
      rescue Errno::EACCES => e
        raise Bolt::FileError.new(
          "#{e.message}; unable to update project configuration",
          @config.project.project_file
        )
      end
      { name: name, path: policy }
    end
def new_policy ( name ) unless name =~ Bolt :: Module :: CONTENT_NAME_REGEX message = <<~MESSAGE . chomp 
          Invalid policy name ' #{ name } '. Policy 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 policy names:
              -  #{ @config . project . name } 
              -  #{ @config . project . name } ::my_policy
         MESSAGE raise Bolt :: ValidationError , message end if @config . project . name . nil? command = Bolt :: Util . powershell? ? 'New-BoltProject -Name <NAME>' : 'bolt project init <NAME>' message = <<~MESSAGE . chomp 
          Can't create a policy for the default Bolt project because it doesn't
          have a name. Run ' #{ command } ' to create a new project.
         MESSAGE raise Bolt :: ValidationError , message end prefix , * name_segments , basename = name . split ( '::' ) unless prefix == @config . project . name raise Bolt :: ValidationError , "Policy name '#{name}' must begin with project name '#{@config.project.name}'. Did " "you mean '#{@config.project.name}::#{name}'?" end basename ||= 'init' policy_dir = File . expand_path ( File . join ( name_segments ) , @config . project . manifests ) policy = File . expand_path ( "#{basename}.pp" , policy_dir ) if File . exist? ( policy ) raise Bolt :: Error . new ( "A policy with the name '#{name}' already exists at '#{policy}', nothing to do." , 'bolt/existing-policy-error' ) end begin FileUtils . mkdir_p ( policy_dir ) rescue Errno :: EEXIST => e raise Bolt :: Error . new ( "#{e.message}; unable to create manifests directory '#{policy_dir}'" , 'bolt/existing-file-error' ) end begin File . write ( policy , <<~POLICY ) 
          class  #{ name }  {

          }
         POLICY rescue Errno :: EACCES => e raise Bolt :: FileError . new ( "#{e.message}; unable to create policy" , policy ) end project_config = Bolt :: Util . read_yaml_hash ( @config . project . project_file , 'project config' ) project_config [ 'policies' ] ||= [ ] project_config [ 'policies' ] << name project_config [ 'policies' ] . uniq! begin File . write ( @config . project . project_file , project_config . to_yaml ) rescue Errno :: EACCES => e raise Bolt :: FileError . new ( "#{e.message}; unable to update project configuration" , @config . project . project_file ) end { name : name , path : policy } end
Add a new policy to the project.
Add a new policy to the project .
# Validate the policy name # Validate that we're not running with the default project # Error if name is not namespaced to project # If the policy name is just the project name, use the special init.pp class # Policies can be saved in subdirectories in the 'manifests/' directory # Ensure the policy does not already exist # Create the policy directory structure in the current project # Create the new policy # Update the project configuration to include the new policy # Add the 'policies' key if it does not exist and de-dupiclate entries
None
Hash
name
The name of the new policy.
The name of the new policy .