symfony-console-completion: Unable to autocomplete with sudo

Hi there,

we from Nextcloud started using the completion and it works quite fine in general.

The only problem is, that it is impossible to run the stuff with e.g. sudo -u www-data. This is the recommanded easy-default for Nextcloud, to simply have all code and files owned by the www-data user. But I am unable to --generate-hook in this case.

I’m not sure if this is fixable, but would be very cool if so. I tried adding a copy of a generated hook script manually in /etc/bash_completion.d/ but when trying to modify the script to add sudo -u www-data on the calls, it seems to not execute properly anymore.

Any idea/workaround?

Cheers and keep it up

About this issue

  • Original URL
  • State: open
  • Created 8 years ago
  • Comments: 35 (15 by maintainers)

Most upvoted comments

I got it working like this:

$ . <(sudo -u www-data --preserve-env=SHELL php /var/www/nextcloud/occ _completion -g | sed 's|/var/www/nextcloud/occ|sudo -u www-data --preserve-env=CMDLINE_CONTENTS,CMDLINE_CURSOR_INDEX,CMDLINE_WORDBREAKS php /var/www/nextcloud/occ|')
$ alias occ='sudo -u www-data php /var/www/nextcloud/occ'
$ occ
Display all 124 possibilities? (y or n)
activity:send-mails                    config:app:get                         dav:sync-system-addressbook            encryption:status                      integrity:sign-core                    notification:test-push                 tag:edit                               user:delete
app:disable                            config:app:set                         db:add-missing-columns                 files:cleanup                          l10n:createjs                          notify_push:log                        tag:list                               user:disable
app:enable                             config:import                          db:add-missing-indices                 files:repair-tree                      list                                   notify_push:metrics                    theming:config                         user:enable
app:getpath                            config:list                            db:add-missing-primary-keys            files:scan                             log:file                               notify_push:reset                      trashbin:cleanup                       user:info
app:install                            config:system:delete                   db:convert-filecache-bigint            files:scan-app-data                    log:manage                             notify_push:self-test                  trashbin:expire                        user:lastseen
app:list                               config:system:get                      db:convert-mysql-charset               files:transfer-ownership               log:tail                               notify_push:setup                      trashbin:restore                       user:list
app:remove                             config:system:set                      db:convert-type                        group:add                              log:watch                              preview:repair                         trashbin:size                          user:report
app:update                             dav:create-addressbook                 encryption:change-key-storage-root     group:adduser                          maintenance:data-fingerprint           preview:reset-rendered-texts           twofactorauth:cleanup                  user:resetpassword
background-job:execute                 dav:create-calendar                    encryption:decrypt-all                 group:delete                           maintenance:mimetype:update-db         ransomware_protection:block            twofactorauth:disable                  user:setting
background-job:list                    dav:delete-calendar                    encryption:disable                     group:info                             maintenance:mimetype:update-js         security:bruteforce:reset              twofactorauth:enable                   versions:cleanup
background:ajax                        dav:list-calendars                     encryption:enable                      group:list                             maintenance:mode                       security:certificates                  twofactorauth:enforce                  versions:expire
background:cron                        dav:move-calendar                      encryption:encrypt-all                 group:removeuser                       maintenance:repair                     security:certificates:import           twofactorauth:state                    workflows:list
background:webcron                     dav:remove-invalid-shares              encryption:list-modules                help                                   maintenance:repair-share-owner         security:certificates:remove           update:check
broadcast:test                         dav:retention:clean-up                 encryption:migrate-key-storage-format  integrity:check-app                    maintenance:theme:update               status                                 upgrade
check                                  dav:send-event-reminders               encryption:set-default-module          integrity:check-core                   maintenance:update:htaccess            tag:add                                user:add
config:app:delete                      dav:sync-birthday-calendar             encryption:show-key-storage-root       integrity:sign-app                     notification:generate                  tag:delete                             user:add-app-password
  • Only the SHELL variable is needed to generate the hook script. Alternatively --shell-type bash or --shell-type zsh can be used, in which cases the SHELL variable is not required.
  • Only the three completion-related variables, exported within the hook script, need to be passed through sudo.

What indeed would help is:

  • --sudo-user=USER and/or reading it from SUDO_USER variable, to add sudo -u USER --preserve-env=CMDLINE_CONTENTS,CMDLINE_CURSOR_INDEX,CMDLINE_WORDBREAKS to the hook script automatically.
  • However, the php command is required as well in case of Nextcloud, since the occ script is not executable OOTB. More logic could solve this: [[ -x $completionCommand ]] || completionCommand="php $completionCommand" (respectively doing this in PHP hook factory already.