]> git.ipfire.org Git - thirdparty/snapper.git/commitdiff
Add zsh completion 780/head
authorWu Zhenyu <wuzhenyu@ustc.edu>
Tue, 7 Feb 2023 12:01:07 +0000 (20:01 +0800)
committerWu Zhenyu <wuzhenyu@ustc.edu>
Wed, 8 Feb 2023 09:17:06 +0000 (17:17 +0800)
Fix #623, similar with #680

dists/debian/snapper.install
scripts/Makefile.am
scripts/zsh-completion.zsh [new file with mode: 0644]
snapper.spec.in

index 668f7e3684268f49e90fa491e09216bd62f2ef1c..77bb37e8ca35ca1fc9d8d5537457dc5beb2195a2 100644 (file)
@@ -7,4 +7,5 @@ usr/share/dbus-1/system.d/org.opensuse.Snapper.conf
 usr/share/dbus-1/system-services/org.opensuse.Snapper.service
 usr/share/locale/*/LC_MESSAGES/snapper.mo
 usr/share/bash-completion/completions/snapper
+usr/share/zsh/site-functions/_snapper
 usr/lib/systemd/system/snapperd.service
index 506f74a10d34584eba28f15b4510acc8c5d54083..0839c706c12b60ff2273d96bd5e119b51d8bf7cc 100644 (file)
@@ -14,10 +14,10 @@ pam_snapper_SCRIPTS =                       \
 
 endif
 
-EXTRA_DIST = snapper-hourly snapper-daily bash-completion.bash $(pam_snapper_SCRIPTS)
+EXTRA_DIST = snapper-hourly snapper-daily bash-completion.bash zsh-completion.zsh $(pam_snapper_SCRIPTS)
 
 install-data-local:
        install -D snapper-hourly $(DESTDIR)/etc/cron.hourly/suse.de-snapper
        install -D snapper-daily $(DESTDIR)/etc/cron.daily/suse.de-snapper
        install -D --mode a+r,u+w bash-completion.bash $(DESTDIR)/usr/share/bash-completion/completions/snapper
-
+       install -D --mode a+r,u+w zsh-completion.zsh $(DESTDIR)/usr/share/zsh/site-functions/_snapper
diff --git a/scripts/zsh-completion.zsh b/scripts/zsh-completion.zsh
new file mode 100644 (file)
index 0000000..40175ed
--- /dev/null
@@ -0,0 +1,139 @@
+#compdef snapper
+
+local options=(
+       {--quiet,-q}'[Suppress normal output]'
+       {--verbose,-v}'[Increase verbosity]'
+       --utc'[Display dates and times in UTC]'
+       --iso'[Display dates and times in ISO format]'
+       {--table-style,-t}'[Table style (integer)]:style:({0..11})'
+       --abbreviate'[Allow to abbreviate table columns]'
+       --machine-readable'[Set a machine-readable output format]:format:(csv json)'
+       --csvout --no-headers'[Set CSV output format]'
+       --jsonout'[Set JSON output format]'
+       --separator'[Character separator for CSV output format]:separator'
+  {--config,-c}'[Set name of config to use]:config:('${(f)$(command snapper --csvout --no-headers list-configs --columns config)}')'
+       --no-dbus'[Operate without DBus]'
+       {--root,-r}'[Operate on target root (works only without DBus)]: :_dirs'
+       {--ambit,-a}'[Operate in the specified ambit]:ambit:(auto classic transactional)'
+       "(- : *)"{--help,-h}'[Prints help information]'
+       "(- : *)"--version'[Print version and exit]'
+)
+
+local subcommands=(
+  'list-configs:List\ configs'
+  'create-config:Create\ config'
+  'delete-config:Delete\ config'
+  'get-config:Get\ config'
+  'set-config:Set\ config'
+  'list:List\ snapshots'
+  'create:Create\ snapshot'
+  'modify:Modify\ snapshot'
+  'delete:Delete\ snapshot'
+  'mount:Mount\ snapshot'
+  'umount:Umount\ snapshot'
+  'status:Comparing\ snapshots'
+  'diff:Comparing\ snapshots'
+  'xadiff:Comparing\ snapshots\ extended\ attributes'
+  'undochange:Undo\ changes'
+  'rollback:Rollback'
+  'setup-quota:Setup\ quota'
+  'cleanup:Cleanup\ snapshots'
+)
+
+_arguments -s -S $options \
+  ": :(($subcommands))" \
+       "*:: :->option-or-argument"
+
+if [[ $state != option-or-argument ]]; then
+  return
+fi
+local curcontext=${curcontext%:*:*}:snapper-$words[1]:
+local algorithms=(
+  'number:Deletes\ old\ snapshots\ when\ a\ certain\ number\ of\ snapshots\ is\ reached'
+  'timeline:Deletes\ old\ snapshots\ but\ keeps\ a\ number\ of\ hourly,\ daily,\ weekly,\ monthly\ and\ yearly\ snapshots'
+  'empty-pre-post:Deletes\ pre/post\ snapshot\ pairs\ with\ empty\ diffs'
+)
+local type=(single pre post)
+case $words[1] in
+list-configs)
+  _arguments -s -S \
+    '--columns[Columns to show separated by comma]:columns:(config subvolume)'
+  ;;
+create-config)
+  # nixOS doesn't have /usr/share while it has /run/current-system/sw
+  # other distributions (except Android) have /usr/share or /usr/local/share
+  _arguments -s -S \
+    {--fstype,-f}'[Manually set filesystem type]:filesystem:(btrfs ext4 lvm)' \
+    {--template,-t}"[Name of config template to use]:template:(${$(echo /{usr{/local,},run/current-system/sw}/share/snapper/config-templates/*(N))##*/})"
+  ;;
+get-config)
+  _arguments -s -S \
+    --columns'[Columns to show separated by comma]:columns:(key value)'
+  ;;
+set-config)
+  _arguments -s -S \
+    ":configdata"
+  ;;
+list)
+  _arguments -s -S \
+  {--type,-t}'[Type of snapshots to list]:type:(($type))' \
+  --disable-used-space'[Disable showing used space]' \
+  {--all,-a-configs}'[List snapshots from all accessible configs]' \
+  --columns'[Columns to show separated by comma]:columns:(config subvolume number default active type date user used-space cleanup description userdata pre-number post-number post-date)'
+  ;;
+create)
+  local number=$(command snapper --csvout --no-headers --separator : list --columns number,description)
+  _arguments -s -S \
+  {--type,-t}'[Type for snapshot]:type:(($type))' \
+  --pre-number'[Number of corresponding pre snapshot]:' \
+  {--print-number,-p}'[Print number of created snapshot]' \
+  {--description,-d}'[Description for snapshot]:description' \
+  {--cleanup-algorithm,-c}'[Cleanup algorithm for snapshot]:algorithms:(($algorithms))' \
+  {--userdata,-u}'[Userdata for snapshot]:userdata' \
+  --command'[Run command and create pre and post snapshots]: :{_command_names -e}' \
+  --read-only'[Create read-only snapshot]' \
+  --read-write'[Create read-write snapshot]' \
+  --from"[Create a snapshot from the specified snapshot]:number:((${(f)number}))"
+  ;;
+modify)
+  _arguments -s -S \
+    {--description,-d}'[Description for snapshot]:description' \
+    {--cleanup-algorithm,-c}'[Cleanup algorithm for snapshot]:algorithms:(($algorithms))' \
+    {--userdata,-u}'[Userdata for snapshot]:userdata'
+  ;;
+delete)
+  _arguments -s -S \
+    {--sync,-s}'[Sync after deletion]'
+  ;;
+(u|)mount|xadiff)
+  local number=$(command snapper --csvout --no-headers --separator : list --columns number,description)
+  _arguments -s -S \
+    ":number:((${(f)number}))"
+    ;;
+status)
+  _arguments -s -S \
+    {--output,-o}'[Save status to file]: :_files'
+  ;;
+diff)
+  _arguments -s -S \
+    {--input,-i}'[Read files to diff from file]: :_files' \
+    --diff-cmd'[Command used for comparing files]: :{_command_names -e}' \
+    {--extensions,-x}'[Extra options passed to the diff command]:diff options:_diff'
+  ;;
+undochange)
+  _arguments -s -S \
+    {--input,-i}'[Read files for which to undo changes from file]: :_files'
+  ;;
+rollback)
+  _arguments -s -S \
+    {--print-number,-p}'[Print number of second created snapshot]' \
+    {--description,-d}'[Description for snapshot]:description' \
+    {--cleanup-algorithm,-c}'[Cleanup algorithm for snapshot]:algorithms:(($algorithms))' \
+    {--userdata,-u}'[Userdata for snapshot]:userdata'
+  ;;
+cleanup)
+  _arguments -s -S \
+    --path'[Cleanup all configs affecting path.]: :_files' \
+    --free-space'[Try to make space available.]:space'
+  ;;
+esac
index caa1f0c47b1abace289e01f44f2eea13161c1a5b..f9d45dd04dcb6bca93e74f575d2f2661577ea6cb 100644 (file)
@@ -235,6 +235,7 @@ test -f /etc/logrotate.d/snapper.rpmsave && mv -v /etc/logrotate.d/snapper.rpmsa
 %{_datadir}/dbus-1/system.d/org.opensuse.Snapper.conf
 %{_datadir}/dbus-1/system-services/org.opensuse.Snapper.service
 %{_datadir}/bash-completion/completions/snapper
+%{_datadir}/zsh/site-functions/_snapper
 
 %package -n libsnapper@LIBVERSION_MAJOR@
 Summary:        Library for filesystem snapshot management