| 1 | #!/usr/bin/env perl | ||
| 2 | |||
| 3 | # vim: tabstop=4 expandtab | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | cli-driver-sharedir-example.pl - a CLI::Driver example with File::ShareDir | ||
| 8 | |||
| 9 | Quickstart: | ||
| 10 | - cd to your project dir (typically where the Makefile.PL lives) | ||
| 11 | - create bin/ dir | ||
| 12 | - copy this to bin/<yournewcliname.pl> | ||
| 13 | - create 'share/' dir | ||
| 14 | - create 'share/cli-driver.yml' file (you can use the cli-driver.yml example | ||
| 15 | included with this distro) | ||
| 16 | - find the TODO(s) in this file and update those section(s) appropriately | ||
| 17 | |||
| 18 | - Dist::Zilla users: | ||
| 19 | - add [ExecDir] to your dist.ini (assuming you want to bundle your new cli) | ||
| 20 | - add [ShareDir] to your dist.ini | ||
| 21 | |||
| 22 | - ExtUtils::MakeMaker users: | ||
| 23 | - add EXE_FILES => ['bin/yournewcliname.pl'] to your Makefile.PL | ||
| 24 | - refer to File::ShareDir docs for remaining config | ||
| 25 | |||
| 26 | =cut | ||
| 27 | |||
| 28 | ###### PACKAGES ###### | ||
| 29 | |||
| 30 | use Modern::Perl; | ||
| 31 | use Data::Printer alias => 'pdump'; | ||
| 32 | use CLI::Driver; | ||
| 33 | use File::ShareDir 'dist_file'; | ||
| 34 | use File::Basename; | ||
| 35 | use Getopt::Long; | ||
| 36 | Getopt::Long::Configure('no_ignore_case'); | ||
| 37 | Getopt::Long::Configure('pass_through'); | ||
| 38 | Getopt::Long::Configure('no_auto_abbrev'); | ||
| 39 | |||
| 40 | ###### CONSTANTS ###### | ||
| 41 | |||
| 42 | # TODO: change to your distribution name (using hyphens) | ||
| 43 | use constant DIST_NAME => 'CLI-DRIVER'; | ||
| 44 | |||
| 45 | # TODO: change to your cli-driver filename IF it differs | ||
| 46 | use constant CLI_DRIVER_FILE => 'cli-driver.yml'; | ||
| 47 | |||
| 48 | ###### GLOBALS ###### | ||
| 49 | |||
| 50 | use vars qw( | ||
| 51 | $Action | ||
| 52 | $CliDriver | ||
| 53 | ); | ||
| 54 | |||
| 55 | ###### MAIN ###### | ||
| 56 | |||
| 57 | $| = 1; | ||
| 58 | |||
| 59 | $CliDriver = CLI::Driver->new( path => get_cli_driver_path() ); | ||
| 60 | |||
| 61 | parse_cmd_line(); | ||
| 62 | |||
| 63 | my $action = $CliDriver->get_action( name => $Action ); | ||
| 64 | |||
| 65 | if ($action) { | ||
| 66 | $action->do; | ||
| 67 | } | ||
| 68 | else { | ||
| 69 | $CliDriver->fatal("failed to find action in config file"); | ||
| 70 | } | ||
| 71 | |||
| 72 | ###### END MAIN ###### | ||
| 73 | |||
| 74 | sub get_repo_dir { | ||
| 75 | |||
| 76 | my $bindir = dirname $0; | ||
| 77 | my $devdir = dirname $bindir; | ||
| 78 | |||
| 79 | return $devdir; | ||
| 80 | } | ||
| 81 | |||
| 82 | sub get_cli_driver_path { | ||
| 83 | |||
| 84 | # | ||
| 85 | # first attempt to find the driver file in the git repo location | ||
| 86 | # | ||
| 87 | my $sharedir = sprintf "%s/share", get_repo_dir(); | ||
| 88 | if ( -f sprintf "%s/%s", $sharedir, CLI_DRIVER_FILE() ) { | ||
| 89 | # local development location | ||
| 90 | return $sharedir; | ||
| 91 | } | ||
| 92 | |||
| 93 | # | ||
| 94 | # try the File::ShareDir install location | ||
| 95 | # | ||
| 96 | my $file = dist_file( DIST_NAME(), CLI_DRIVER_FILE()); | ||
| 97 | |||
| 98 | return dirname $file; | ||
| 99 | } | ||
| 100 | |||
| 101 | sub check_required { | ||
| 102 | my $opt = shift; | ||
| 103 | my $arg = shift; | ||
| 104 | |||
| 105 | print_usage("missing arg $opt") if !$arg; | ||
| 106 | } | ||
| 107 | |||
| 108 | sub parse_cmd_line { | ||
| 109 | |||
| 110 | my $help; | ||
| 111 | GetOptions( "help|?" => \$help ); | ||
| 112 | |||
| 113 | if ( !@ARGV ) { | ||
| 114 | print_usage(); | ||
| 115 | } | ||
| 116 | elsif (@ARGV) { | ||
| 117 | $Action = shift @ARGV; | ||
| 118 | } | ||
| 119 | |||
| 120 | if ($help) { | ||
| 121 | if ($Action) { | ||
| 122 | help_action(); | ||
| 123 | } | ||
| 124 | else { | ||
| 125 | print_usage(); | ||
| 126 | } | ||
| 127 | } | ||
| 128 | } | ||
| 129 | |||
| 130 | sub help_action { | ||
| 131 | |||
| 132 | my $action = $CliDriver->get_action( name => $Action ); | ||
| 133 | $action->usage; | ||
| 134 | } | ||
| 135 | |||
| 136 | sub print_actions { | ||
| 137 | |||
| 138 | my $actions = $CliDriver->get_actions; | ||
| 139 | my @list; | ||
| 140 | |||
| 141 | foreach my $action (@$actions) { | ||
| 142 | |||
| 143 | next if $action->name =~ /dummy/i; | ||
| 144 | my $display = $action->name; | ||
| 145 | |||
| 146 | if ( $action->is_deprecated ) { | ||
| 147 | $display .= " (deprecated)"; | ||
| 148 | } | ||
| 149 | |||
| 150 | push @list, $display; | ||
| 151 | } | ||
| 152 | |||
| 153 | say "\tACTIONS:"; | ||
| 154 | |||
| 155 | foreach my $action ( sort @list ) { | ||
| 156 | print "\t\t$action\n"; | ||
| 157 | } | ||
| 158 | } | ||
| 159 | |||
| 160 | sub print_usage { | ||
| 161 | print STDERR "@_\n\n" if @_; | ||
| 162 | |||
| 163 | my $basename = basename($0); | ||
| 164 | |||
| 165 | printf "\nusage: %s <action> [opts] [-?]\n\n", basename($0); | ||
| 166 | print_actions(); | ||
| 167 | print "\n"; | ||
| 168 | |||
| 169 | exit 1; | ||
| 170 | } |