-4

I created a bash script that automatically populates a lua config file with ssh-mux settings according to my input. The populated config file looks like this:

ssh_domains.lua

local M = {}

M.ssh_domains = {
-- AUTO GENERATED BELOW THIS LINE
    {
        name = 'SSHMUX:server01',
        remote_address = 'server01.brand.loc.p.company.com',
        username = 'process',
        remote_wezterm_path = "/home/process/test/user/repo-b/.local/bin/wezterm",
    },
    {
        name = 'SSHMUX:server02',
        remote_address = 'server02.company.com',
        username = 'process',
        remote_wezterm_path = "/home/process/test/user/repo-b/.local/bin/wezterm"
    },
    {
        name = 'SSHMUX:server15',
        remote_address = 'server15.company.com',
        username = 'process',
        remote_wezterm_path = "/home/process/test/user/repo-b/.local/bin/wezterm-bootstrap.bash"
    },
    {
        name = 'SSHMUX:server16',
        remote_address = 'server16.par.cz.t.company.com',
        username = 'process',
        remote_wezterm_path = "/home/user/test/user/repo-b/.local/bin/bootstrap-wezterm.bash"
    },
}

return M

The settings are automatically inserted as a substituted heredoc with sed and not checked for syntactical correctness. It's brittle but it works.

However, now I wanted to implement the inverse logic that removes the respective configuration from the file. This is a lot more involved, since I would have to make even more assumptions and make it even more brittle.

That's why I was wondering if I could remove a whole config block (array element) in a more robust way by removing it from the abstract syntax tree directly. Could this be done with treesitter from a bash script?

1
  • 4
    Please edit your question and add your desired output (no description) for that sample input. Commented May 3 at 15:00

1 Answer 1

2

You could pick a string and calculate the offsets to remove an entire block like below. Don't forget to change the redirect for sed's -i option.

$ rem="name = 'SSHMUX:server15'"
$ nr=$(grep -n "$rem" ssh_domains.lua | grep -Po '^[\d]+')
$ sed "$((nr-1)),$((nr+4))d" ssh_domains.lua > ssh_domains.lua.tst

The code above will result in:

local M = {}

M.ssh_domains = {
-- AUTO GENERATED BELOW THIS LINE
    {
        name = 'SSHMUX:server01',
        remote_address = 'server01.brand.loc.p.company.com',
        username = 'process',
        remote_wezterm_path = "/home/process/test/user/repo-b/.local/bin/wezterm",
    },
    {
        name = 'SSHMUX:server02',
        remote_address = 'server02.company.com',
        username = 'process',
        remote_wezterm_path = "/home/process/test/user/repo-b/.local/bin/wezterm"
    },
    {
        name = 'SSHMUX:server16',
        remote_address = 'server16.par.cz.t.company.com',
        username = 'process',
        remote_wezterm_path = "/home/user/test/user/repo-b/.local/bin/bootstrap-wezterm.bash"
    },
}

return M
Sign up to request clarification or add additional context in comments.

2 Comments

That would have been my approach but it's brittle... because the deleting code depends on the creating code, i could decide to add one more line and the deletion code would break. I thought there could be a syntax-aware way to delete the code block :)
It should be possible to detect it with a regex, but that's a bit much to do yesterday evening. You can check what (?:(?!:name = 'whatever')[.\n])*) does. :-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.