]> git.proxmox.com Git - pve-zsync.git/commitdiff
refactor synopsis handling
authorThomas Lamprecht <t.lamprecht@proxmox.com>
Fri, 9 Mar 2018 15:55:43 +0000 (16:55 +0100)
committerWolfgang Bumiller <w.bumiller@proxmox.com>
Tue, 15 May 2018 11:33:05 +0000 (13:33 +0200)
move command synopsis help into the "known command" hash
use that to generalise parameter checking, printing help and
generating POD synopsis

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
pve-zsync

index 8b41912d0a1a888ee8fce15d1f9ff4fe794172d3..dd3dee4db27c31dbd5a0c89520e4ecf8d0b7f350 100755 (executable)
--- a/pve-zsync
+++ b/pve-zsync
@@ -1018,54 +1018,21 @@ sub disable_job {
 
 my $command = $ARGV[0];
 
-my $commands = {'destroy' => 1,
-               'create' => 1,
-               'sync' => 1,
-               'list' => 1,
-               'status' => 1,
-               'help' => 1,
-               'enable' => 1,
-               'disable' => 1,
-               'printpod' => 1,
-};
-
-if (!$command || !$commands->{$command}) {
-    usage();
-    die "\n";
-}
-
-my $help_sync = <<EOF;
-$PROGNAME sync -dest <string> -source <string> [OPTIONS]\n
-
-       will sync one time
-
-        -dest      string
-
-               the destination target is like [IP:]<Pool>[/Path]
-
-       -limit     integer
-
-               max sync speed in kBytes/s, default unlimited
-
-        -maxsnap   integer
+my $cmd_help = {
+    destroy => qq{
+$PROGNAME destroy -source <string> [OPTIONS]
 
-               how much snapshots will be kept before get erased, default 1
+        remove a sync Job from the scheduler
 
         -name      string
 
-               name of the sync job, if not set it is default.
-               It is only necessary if scheduler allready contains this source.
+               name of the sync job, if not set it is default
 
         -source    string
 
-               the source can be an <VMID> or [IP:]<ZFSPool>[/Path]
-
-       -verbose   boolean
-
-               print out the sync progress.
-EOF
-
-my $help_create = <<EOF;
+                the source can be an  <VMID> or [IP:]<ZFSPool>[/Path]
+    },
+    create => qq{
 $PROGNAME create -dest <string> -source <string> [OPTIONS]
 
         Create a sync Job
@@ -1093,49 +1060,61 @@ $PROGNAME create -dest <string> -source <string> [OPTIONS]
         -source    string
 
                the source can be an <VMID> or [IP:]<ZFSPool>[/Path]
-EOF
+    },
+    sync => qq{
+$PROGNAME sync -dest <string> -source <string> [OPTIONS]\n
 
-my $help_destroy = <<EOF;
-$PROGNAME destroy -source <string> [OPTIONS]
+       will sync one time
 
-        remove a sync Job from the scheduler
+        -dest      string
 
-        -name      string
+               the destination target is like [IP:]<Pool>[/Path]
 
-               name of the sync job, if not set it is default
+       -limit     integer
 
-        -source    string
+               max sync speed in kBytes/s, default unlimited
 
-                the source can be an  <VMID> or [IP:]<ZFSPool>[/Path]
-EOF
+        -maxsnap   integer
 
-my $help_help = <<EOF;
-$PROGNAME help <cmd> [OPTIONS]
+               how much snapshots will be kept before get erased, default 1
 
-       Get help about specified command.
+        -name      string
 
-        <cmd>      string
+               name of the sync job, if not set it is default.
+               It is only necessary if scheduler allready contains this source.
 
-               Command name
+        -source    string
 
-       -verbose   boolean
+               the source can be an <VMID> or [IP:]<ZFSPool>[/Path]
 
-               Verbose output format.
-EOF
+       -verbose   boolean
 
-my $help_list = <<EOF;
+               print out the sync progress.
+    },
+    list => qq{
 $PROGNAME list
 
        Get a List of all scheduled Sync Jobs
-EOF
-
-my $help_status = <<EOF;
+    },
+    status => qq{
 $PROGNAME status
 
        Get the status of all scheduled Sync Jobs
-EOF
+    },
+    help => qq{
+$PROGNAME help <cmd> [OPTIONS]
+
+       Get help about specified command.
+
+        <cmd>      string
 
-my $help_enable = <<EOF;
+               Command name
+
+       -verbose   boolean
+
+               Verbose output format.
+    },
+    enable => qq{
 $PROGNAME enable -source <string> [OPTIONS]
 
         enable a syncjob and reset error
@@ -1147,9 +1126,8 @@ $PROGNAME enable -source <string> [OPTIONS]
         -source    string
 
                 the source can be an  <VMID> or [IP:]<ZFSPool>[/Path]
-EOF
-
-my $help_disable = <<EOF;
+    },
+    disable => qq{
 $PROGNAME disable -source <string> [OPTIONS]
 
         pause a sync job
@@ -1161,57 +1139,42 @@ $PROGNAME disable -source <string> [OPTIONS]
         -source    string
 
                 the source can be an  <VMID> or [IP:]<ZFSPool>[/Path] 
-EOF
-
-sub help {
-    my ($command) = @_;
-
-    if ($command eq 'help') {
-       die "$help_help\n";
-
-    } elsif ($command eq 'sync') {
-       die "$help_sync\n";
-
-    } elsif ($command eq 'destroy') {
-       die "$help_destroy\n";
-
-    } elsif ($command eq 'create') {
-       die "$help_create\n";
+    },
+    printpod => 'internal command',
 
-    } elsif ($command eq 'list') {
-       die "$help_list\n";
-
-    } elsif ($command eq 'status') {
-       die "$help_status\n";
-
-    } elsif ($command eq 'enable') {
-       die "$help_enable\n";
-
-    } elsif ($command eq 'disable') {
-       die "$help_disable\n";
-
-    }
+};
 
+if (!$command) {
+    usage(); die "\n";
+} elsif (!$cmd_help->{$command}) {
+    print "ERROR: unknown command '$command'";
+    usage(1); die "\n";
 }
 
 my @arg = @ARGV;
 my $param = parse_argv(@arg);
 
+sub check_params {
+    for (@_) {
+       die "$cmd_help->{$command}\n" if !$param->{$_};
+    }
+}
+
 if ($command eq 'destroy') {
-    die "$help_destroy\n" if !$param->{source};
+    check_params(qw(source));
 
     check_target($param->{source});
     destroy_job($param);
 
 } elsif ($command eq 'sync') {
-    die "$help_sync\n" if !$param->{source} || !$param->{dest};
+    check_params(qw(source dest));
 
     check_target($param->{source});
     check_target($param->{dest});
     sync($param);
 
 } elsif ($command eq 'create') {
-    die "$help_create\n" if !$param->{source} || !$param->{dest};
+    check_params(qw(source dest));
 
     check_target($param->{source});
     check_target($param->{dest});
@@ -1226,8 +1189,8 @@ if ($command eq 'destroy') {
 } elsif ($command eq 'help') {
     my $help_command = $ARGV[1];
 
-    if ($help_command && $commands->{$help_command}) {
-       print help($help_command);
+    if ($help_command && $cmd_help->{$help_command}) {
+       die "$cmd_help->{$command}\n";
 
     }
     if ($param->{verbose}) {
@@ -1239,13 +1202,13 @@ if ($command eq 'destroy') {
     }
 
 } elsif ($command eq 'enable') {
-    die "$help_enable\n" if !$param->{source};
+    check_params(qw(source));
 
     check_target($param->{source});
     enable_job($param);
 
 } elsif ($command eq 'disable') {
-    die "$help_disable\n" if !$param->{source};
+    check_params(qw(source));
 
     check_target($param->{source});
     disable_job($param);
@@ -1275,6 +1238,9 @@ sub check_target {
 }
 
 sub print_pod {
+
+    my $synopsis = join("\n", sort values %$cmd_help);
+
     print <<EOF;
 =head1 NAME
 
@@ -1284,21 +1250,7 @@ pve-zsync - PVE ZFS Replication Manager
 
 pve-zsync <COMMAND> [ARGS] [OPTIONS]
 
-$help_help
-
-$help_create
-
-$help_destroy
-
-$help_disable
-
-$help_enable
-
-$help_list
-
-$help_status
-
-$help_sync
+$synopsis
 
 =head1 DESCRIPTION