Sha256: d313f7f796b5d0d346d94654d60620392cba9dd53c8471cf1fa085d76984bdb2

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

Contents

module SousChef
  module Resource
    class File < Directory
      def content(content=nil)
        set_or_return(:content, content)
      end

      protected
        def escaped_content
          content#.gsub('$', '\$')
        end

        def create
          not_if file_exist_command
          command create_file_command
        end

        def delete
          only_if file_exist_command
          command "rm #{escape_path(path)}"
        end

        def file_exist_command
          %{test -e #{escape_path(path)}}
        end

        def heredoc
          @heredoc ||= begin
            candidate = "SousChefHeredoc"
            candidate += "1" while content.include?(candidate)
            candidate
          end
        end

        def create_file_command
          if content
            lines = []
            lines << "cat <<'#{heredoc}' > #{escape_path(path)}"
            lines << escaped_content
            lines << heredoc
            lines.join("\n")
          else
            %{touch #{escape_path(path)}}
          end
        end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sous_chef-0.0.2 lib/sous_chef/resource/file.rb