Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

caddyfile: Implement heredoc support #5385

Merged
merged 11 commits into from
Feb 26, 2023

Conversation

francislavoie
Copy link
Member

@francislavoie francislavoie commented Feb 20, 2023

I'm reopening my old feature branch from #3802 since I still think this should be implemented. Closes #5357

I've made some improvements to the implementation, some of which were possible because of improvements to the lexer we've made since 2020 when I first tried this.

This branch has a few commits at the end of the branch with some unrelated changes, mainly cleanup to some Caddyfile parsing/dispensing code that I noticed I could improve while reading around.

Copying the text from my previous PR:


Adds support for heredoc syntax to the Caddyfile, inspired by HCL (HashiCorp Config Language), and PHP 7.3's flexible syntax (trimming the front of every line based on the indentation of the ending marker: https://wiki.php.net/rfc/flexible_heredoc_nowdoc_syntaxes)

Here's an example:

example.com {
	respond <<EOF
	<html>
	  <head><title>Foo</title>
	  <body>Foo</body>
	</html>
	EOF 200
}

Adapted JSON:

{
  "apps": {
    "http": {
      "servers": {
        "srv0": {
          "listen": [
            ":443"
          ],
          "routes": [
            {
              "match": [
                {
                  "host": [
                    "example.com"
                  ]
                }
              ],
              "handle": [
                {
                  "handler": "subroute",
                  "routes": [
                    {
                      "handle": [
                        {
                          "body": "<html>\n  <head><title>Foo</title>\n  <body>Foo</body>\n</html>\n",
                          "handler": "static_response",
                          "status_code": 200
                        }
                      ]
                    }
                  ]
                }
              ],
              "terminal": true
            }
          ]
        }
      }
    }
  }
}

Notice that the response body has the leading whitespace on each line stripped away.


This could use some more tests in lexer.go before merging. There is an adapt test which is enough at a minimum for the moment.

@francislavoie francislavoie added feature ⚙️ New feature or request needs tests 💯 Requires automated tests labels Feb 20, 2023
@francislavoie francislavoie added this to the v2.7.0 milestone Feb 20, 2023
@francislavoie francislavoie requested a review from mholt February 20, 2023 16:50
Copy link
Member

@mholt mholt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, despite my personal feelings about heredoc, I hereby accept that it is inevitable. 😜 This looks like a fairly decent change.

What happens if the heredoc isn't terminated?

@francislavoie
Copy link
Member Author

Oh yeah, good point. Forgot to handle the case where we run out of runes to lex but we didn't find the heredoc marker. Adding a condition for that.

I still want to add some more tests to this before merging. I'll do that next chance I get.

@francislavoie francislavoie removed the needs tests 💯 Requires automated tests label Feb 26, 2023
@francislavoie francislavoie enabled auto-merge (squash) February 26, 2023 00:30
@francislavoie francislavoie merged commit 960150b into caddyserver:master Feb 26, 2023
@francislavoie francislavoie deleted the caddyfile-heredoc branch February 26, 2023 00:34
matthewpi added a commit to caddyserver/vscode-caddyfile that referenced this pull request Feb 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature ⚙️ New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow multiline strings syntax
2 participants