change whonix source indentation

Something is bugging me for quite a while, it may sound silly but it breaks my flow because I have to correct whonix indentation, and change my editor indentation to 3 only for whonix file just doesn’t work if you are not always working with whonix only files.

Currently whonix is using 3 spaces for indentation, it is not standard to use an odd number. I also don’t know of anyone who manually indents to 3 spaces, they either indent to 2 or 4 or 8, so my guess is that past and present developers use(d) their editor configuration to set 3 spaces for indentation.

Can this be corrected by the great search and replace to all whonix files?

Pattern tor search for is ^ .* for the first degree of indentation and adding 3 spaces for other levels of indentation.

grep --color=auto "^ .*" /usr/bin/whonix-gateway-firewall, of literally any whonix file and you will see it is using 3 spaces for indentation.

I made this for 5 levels of indentation:

sed \
        -e "s/^               /          /g" \
        -e "s/^            /        /g" \
        -e "s/^         /      /g" \ 
        -e "s/^      /    /g" \
        -e "s/^   /  /g" \
        FILE

15 spaces becomes 10, 12 become 8, 9 become 6, 6 becomes 4, 3 becomes 2.

I prefer 2 spaces because then it has less chances or hitting the 79 char mark, but 4 spaces would also be fine, but not 3.

1 Like

Ok, I compared and clearly nobody (in my quick review) is using 3.

So 2 it is.

Above sed script doesn’t work.

It fails for example on whonix-firewall debian/whonix-firewall.postinst. Hard to explain. But the indent style is messed up. Easy to reproduce and see.

Do we need to invent a sed script here? Isn’t there some existing tool for code reformatting?

Probably there is, but will have to search, I don’t know.

1 Like

If there’s a reliable re-formatting method (specialized tool, sed, script, …), I am happy to do it project wide.

Until then, for anyone who modifies any scripts, feel free to change indentation to 2 spaces. However, change of indentation level (except for trivial scripts) needs to be in a separate commit to make it easier to review. (Not good to mix complex code changes with script wide indentation level changes.)

Found a good one available on testing/bookworm.

https://tracker.debian.org/pkg/golang-mvdan-sh

on a debian testing enabled dev machine:

sudo apt install golang-mvdan-sh-dev shfmt

I recommend reading the man page for shfmt.

Good options:

shfmt --indent 2 --space-redirects --switch-case-indent FILE

use -w|--write to write to file.

The above is for formatting from the command line.

For the future, during development, editing files, I recommend https://editorconfig.org/ and use an editor that support editorconfig (many does), with this, when saving the file, editorconfig will correct the format.

1 Like

shfmt seems to be an excellent tool! Great suggestion!

I’ve changed all scripts in whonix-firewall repository. Let me know if you like it.

As for other scripts being modified by anyone, feel free to give it a pass with that shfmt command first and put it into the first commit.

As for automating it project wide… Not sure… It seems to change some things too much. So it needs to be manually reviewed. For example, it changed…

  bash_n_output="$(bash -n "$i" 2>&1)" || { bash_n_exit_code="$?" ; true; };

To:

bash_n_output="$(bash -n "$i" 2>&1)" || {
  bash_n_exit_code="$?"
  true
}

Both styles aren’t great. I probably would not write it that way nowadays.

That functionality can and should probably be implemented in some better way anyhow. Can and should probably be simplified to:

  if ! bash -n "$i" ; then
    output_cmd "ERROR: Invalid config file: '$i'" >&2
    exit 1
  fi