Caddy: Suggestion: Automatic Caddyfile indentation

Created on 9 Jun 2016  路  12Comments  路  Source: caddyserver/caddy

Hey,

Is there any tool to indent Caddyfile? Perhaps Caddy could automatically indent on load (if loading from a file).

What are your thoughts?

feature request

Most helpful comment

I dunno about Sublime, but for Vim users out there, I just recently made a syntax plugin for Vim.

All 12 comments

Just got this same request on Twitter this morning, actually: https://twitter.com/dlsniper/status/740853956461596672

Changing the file on load is definitely a no-no (loading should be a read-only operation; changing the file would be unexpected) but I am willing to consider maybe a caddy -fmt to just format the file and exit. What do you think about that?

That sounds nice as well. I had the idea of automatic indentation because I was doing some edits on Caddyfile and it quickly went a mess, so I wanted to automate the formatting upon Caddy reload. But the caddy -fmt flag is fine, I can automate the "format & reload" operation :)

I mean, hitting tab isn't that hard either... and is much easier than a -fmt flag. :wink:

I'm using nano and got bored of typing 4 spaces every time I pressed Enter. Thanks for remembering me about the tab key, I completely forgot about it :O

This would only be really worth developing if it would be used by site owners. I don't see the incentive for them to do so, and thus I don't really think we need to add that feature to this code base.

Go fmt, on the other hand, is built into so many editor plugins and directly into the Go toolchain that it's even part of the CI process of many applications (including this one).

Basically, this tool would be best implemented as an editor plugin. That, and Caddyfile syntax highlighting. Any takers? Feel free to do it! I'll show it off. Heck, I'll even use it (if you make one for Sublime Text)! :smile: But I'm going to close the issue since I'm not sure it belongs in Caddy core.

Thanks for the suggestion!

I dunno about Sublime, but for Vim users out there, I just recently made a syntax plugin for Vim.

@joshglendenning Cool. If you can do it without enumerating each possible directive, we'd be interested in having this a part of the official caddyserver org on GitHub (like caddyserver/sublimetext)!

Ah, didn't realize there was actually a sublimetext plugin! I might steal some of these regexen ;)

Go for it!

I'm working on a structured configuration module for NixOS, which inevitably generates very broken indentation. Right now I'm fixing that with an awk script, but it'd be awesome (and more reliable) if caddy could do it for me.

@Baughn

(and more reliable)

I wouldn't count on it. Writing to your configuration file without the user looking?

I much rather would see this as a plugin for editors.

What's your awk script? Just curious. :)

NixOS configuration files aren't supposed to be written by humans at all. They can't be, in fact.

Here's my awk script:

/^ *$/ { if (indent != "") next; }
/^.*}/ { sub("  ", "", indent) }
{ 
  sub("^ *", "", $0)
  print indent, $0
}
/^.*{/ { indent  = indent"  " }

And for comparison purposes, my (experimental) structured Caddy config and the resulting Caddyfile. Importantly, the structured config has almost precisely the same format as the nginx variant.

  services.caddy = {
    enable = true;
    agree = true;
    email = "[email protected]";
    virtualHosts = {
      "brage.info" = {
        forceSSL = true;
        locations."/" = {
          root = "/home/svein/web";
          extraConfig = "browse";
        };
      };
      "ar-innna.brage.info" = {
        forceSSL = true;
        locations."/" = {
          root = "/home/aquagon/web";
          extraConfig = "browse";
        };
      };
    };

Caddyfile
```
http://ar-innna.brage.info {
redir / https://ar-innna.brage.info
log /var/log/caddy/ar-innna.brage.info.INFO {
size 100
age 365
keep 20
}
errors {
log /var/log/caddy/ar-innna.brage.info.ERROR {
size 100
age 365
keep 20
}
}
}

https://ar-innna.brage.info:443/ {
root /home/aquagon/web
log /var/log/caddy/ar-innna.brage.info.INFO {
size 100
age 365
keep 20
}
errors {
log /var/log/caddy/ar-innna.brage.info.ERROR {
size 100
age 365
keep 20
}
}
browse
}

http://brage.info {
redir / https://brage.info
log /var/log/caddy/brage.info.INFO {
size 100
age 365
keep 20
}
errors {
log /var/log/caddy/brage.info.ERROR {
size 100
age 365
keep 20
}
}
}

https://brage.info:443/ {
root /home/svein/web
log /var/log/caddy/brage.info.INFO {
size 100
age 365
keep 20
}
errors {
log /var/log/caddy/brage.info.ERROR {
size 100
age 365
keep 20
}
}
browse
}
```

Was this page helpful?
0 / 5 - 0 ratings

Related issues

wayneashleyberry picture wayneashleyberry  路  3Comments

kilpatty picture kilpatty  路  3Comments

la0wei picture la0wei  路  3Comments

billop picture billop  路  3Comments

dafanasiev picture dafanasiev  路  3Comments