Friday, September 9, 2022

Re: Editing config files for docker containers with vim

Den fre 9 sep. 2022 10:50Jirka Novak <j.novak@netsystem.cz> skrev:
Hi,

   I'm maintaining set of docker containers. We use convention that
every config file for a container is in /srv/<container>/ subdirectory.
E.g. /srv/<container>/etc/cron.daily/logrotate.
   We use vim to maintain config files and we like feature/shortcut 'gf'
which opens file on cursor. E.g. logrotate file contains:

#!/bin/sh

/usr/sbin/logrotate /etc/logrotate.conf
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
     /usr/bin/logger -t logrotate "ALERT exited abnormally with
[$EXITVALUE]"
fi
exit $EXITVALUE

   therefore when I use 'gf' on /etc/logrotate.conf, file should be opened.

   The issue is that path is correct when container is running, but when
I'm editing it from the host, it should be
/srv/<container>/etc/logrotate.conf.
   Therefore I'm looking for vim setting or extension of vim which will
add requested prefix to file name when I'm opening it.

   Any advice is welcome.

Assuming that the current file is somewhere under /srv/<container>/

    fun! DockerGf ()
      " save the current register contents
      let old_reg = @"
      " get the path under the cursor
      norm! ByE
      " remove the leading slash if any
      let path = substitute(@",'^/\?','','')
      " restore the previous register contents
      let @" = old_reg
      " get the container directory path
      let parent = substitute(expand('%:p'),'^/srv/[^/]*/\zs.*','','')
      " go to the file
      exe 'edit ' . parent . path
    endfun

    " define custom mapping to open path under cursor in container
    nnor ,dgf :call DockerGf()<cr>

If the assumption that the current file is under the container directory may be
wrong you can use a global variable with the path to the container instead:

    :let g:container_path = '/srv/<container>/'
    
and replace the two lines in the function above containing "parent" with
just

      exe 'edit ' . g:container_path . path

There may be more elegant ways...



                                        Best regards,

                                                Jirka Novak

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/83a3be50-3ac6-abfb-9491-8c9e6524aca0%40netsystem.cz.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/CADAJKhBZDWG%2BPChdb%2BjO%2BSFdSSJUhoUDt9H_4TS2pAuVdhAffA%40mail.gmail.com.

No comments: