| 1 | #!/usr/local/bin/perl -w | ||
| 2 | # | ||
| 3 | # Prepare the stage... | ||
| 4 | # | ||
| 5 | # $Id: moses-user-registries.pl,v 1.3 2008/02/21 00:12:55 kawas Exp $ | ||
| 6 | # Contact: Edward Kawas <edward.kawas@gmail.com> | ||
| 7 | # ----------------------------------------------------------- | ||
| 8 | |||
| 9 | BEGIN { | ||
| 10 | use Getopt::Std; | ||
| 11 | use vars qw/ $opt_h $opt_F /; | ||
| 12 | getopt; | ||
| 13 | # usage | ||
| 14 | if ($opt_h) { | ||
| 15 | print STDOUT <<'END_OF_USAGE'; | ||
| 16 | |||
| 17 | Add/Remove user defined persistent registries for use with MoSeS. | ||
| 18 | This utility can remove only those registries contained in your | ||
| 19 | USER_REGISTRIES file specified in your config file. | ||
| 20 | |||
| 21 | Usage: [-F] | ||
| 22 | |||
| 23 | Provided that you provide the following information, a | ||
| 24 | registry is added to USER_REGISTRIES so that you can | ||
| 25 | access it like all of the other 'known registries': | ||
| 26 | |||
| 27 | registry_name - a human readable name for the registry | ||
| 28 | synonym - a nickname to access the registry | ||
| 29 | endpoint - the endpoint for the registry | ||
| 30 | uri - the registry namespace | ||
| 31 | contact - registry contact information | ||
| 32 | description - a human readable textual description of the registry | ||
| 33 | |||
| 34 | An existing registry is not overwritten - unless an option -F | ||
| 35 | is specified. | ||
| 36 | |||
| 37 | END_OF_USAGE | ||
| 38 | exit (0); | ||
| 39 | } | ||
| 40 | |||
| 41 | sub say { print @_, "\n"; } | ||
| 42 | sub check_module { | ||
| 43 | eval "require $_[0]"; | ||
| 44 | if ($@) { | ||
| 45 | say "Module $_[0] not installed."; | ||
| 46 | } else { | ||
| 47 | say "OK. Module $_[0] is installed."; | ||
| 48 | } | ||
| 49 | } | ||
| 50 | use constant MSWIN => $^O =~ /MSWin32|Windows_NT/i ? 1 : 0; | ||
| 51 | if (MSWIN) { | ||
| 52 | check_module ('Term::ReadLine'); | ||
| 53 | { | ||
| 54 | local $^W = 0; | ||
| 55 | $SimplePrompt::Terminal = Term::ReadLine->new ('Installation'); | ||
| 56 | } | ||
| 57 | } else { | ||
| 58 | check_module ('IO::Prompt'); | ||
| 59 | require IO::Prompt; import IO::Prompt; | ||
| 60 | } | ||
| 61 | say 'Modify Your Persistant Registries'; | ||
| 62 | say '------------------------------------------------------'; | ||
| 63 | |||
| 64 | say; | ||
| 65 | } | ||
| 66 | |||
| 67 | use MOSES::MOBY::Cache::Central; | ||
| 68 | use MOSES::MOBY::Cache::Registries; | ||
| 69 | use English qw( -no_match_vars ) ; | ||
| 70 | use strict; | ||
| 71 | |||
| 72 | # different prompt modules used for different OSs | ||
| 73 | # ('pprompt' as 'proxy_prompt') | ||
| 74 | sub pprompt { | ||
| 75 | return prompt (@_) unless MSWIN; | ||
| 76 | return SimplePrompt::prompt (@_); | ||
| 77 | } | ||
| 78 | |||
| 79 | |||
| 80 | # what registry to use | ||
| 81 | sub prompt_for_action { | ||
| 82 | my $action = pprompt ("Would you like to add or remove a registry? [a] ", | ||
| 83 | -m => [(' Add a new persistent user registry', ' Remove a persistent user registry', ' Quit')]); | ||
| 84 | return $action ||= 'a'; | ||
| 85 | } | ||
| 86 | |||
| 87 | |||
| 88 | |||
| 89 | # --- main --- | ||
| 90 | no warnings 'once'; | ||
| 91 | $opt_F ? say ("In over-write mode\n") : say ("Not in over-write mode ... \n") ; | ||
| 92 | |||
| 93 | my $cache = new MOSES::MOBY::Cache::Central; | ||
| 94 | while(1) { | ||
| 95 | my $action = prompt_for_action; | ||
| 96 | say($action); | ||
| 97 | if ($action =~ m"^ Add a new") { | ||
| 98 | my $force = $opt_F; | ||
| 99 | my $reg = $cache->registries->all(); | ||
| 100 | my $name = pprompt("What is the name of your registry: "); | ||
| 101 | |||
| 102 | my $syn = pprompt("Please provide a short nickname: "); | ||
| 103 | while ($syn =~ m"^http://" or (not $syn =~ m"\w+") | ||
| 104 | or (not ( (not defined $reg->{$syn}) or $force ) ) | ||
| 105 | ) { | ||
| 106 | $syn = pprompt("Invalid nickname entered. Please provide a different nickname: "); | ||
| 107 | } | ||
| 108 | |||
| 109 | my $text = pprompt("Please provide a description for the registry: "); | ||
| 110 | my $endpoint = pprompt("Please enter the endpoint for the registry: "); | ||
| 111 | my $namespace = pprompt("Please enter the registry URI: "); | ||
| 112 | my $contact = pprompt("Please enter registry contact information: "); | ||
| 113 | |||
| 114 | say ("ref is: " . scalar ($endpoint)); | ||
| 115 | |||
| 116 | |||
| 117 | my %args = ( | ||
| 118 | endpoint => ref($endpoint) eq 'IO::Prompt::ReturnVal' ? $endpoint->{value} : $endpoint, | ||
| 119 | namespace => ref($namespace) eq 'IO::Prompt::ReturnVal' ? $namespace->{value} : $namespace, | ||
| 120 | name => ref($name) eq 'IO::Prompt::ReturnVal' ? $name->{value} : $name, | ||
| 121 | contact => ref($contact) eq 'IO::Prompt::ReturnVal' ? $contact->{value} : $contact, | ||
| 122 | public => 'yes', | ||
| 123 | text => ref($text) eq 'IO::Prompt::ReturnVal' ? $text->{value} : $text, | ||
| 124 | synonym => ref($syn) eq 'IO::Prompt::ReturnVal' ? $syn->{value} : $syn | ||
| 125 | ); | ||
| 126 | |||
| 127 | $args{force} = 1 if $opt_F; | ||
| 128 | |||
| 129 | require Data::Dumper; | ||
| 130 | say ( "The following will be added to your list of registries:\n" | ||
| 131 | . Data::Dumper->Dump ( [\%args], ['Details'] )); | ||
| 132 | |||
| 133 | my $suc = $cache->registries->add(%args); | ||
| 134 | say("Successfully added the registry to your user registry file!") if $suc == 1; | ||
| 135 | say("There was an unknown error. Please consult your log file to\nget the exact nature of the problem") | ||
| 136 | if $suc == 0; | ||
| 137 | say("Some of the parameters were not acceptable. Please try again.") if $suc == -1; | ||
| 138 | say("The synonym that you chose already existed. Please try again.") if $suc == -2; | ||
| 139 | |||
| 140 | } elsif ($action =~ m"^ Remove a persistent") { | ||
| 141 | |||
| 142 | my @regs = $cache->registries->list; | ||
| 143 | my $registry = pprompt ("What registry would you like to remove? ", | ||
| 144 | -m => [@regs]); | ||
| 145 | if ('y' eq pprompt ('Are you sure that you would like to remove $registry [n]? ', -ynd=>'n')) { | ||
| 146 | my $suc = $cache->registries->remove($registry); | ||
| 147 | say("Successfully removed the registry.") if $suc == 1; | ||
| 148 | say("There was some problem removing your registry.") if $suc == 0; | ||
| 149 | } else { | ||
| 150 | say ("Nothing was removed."); | ||
| 151 | } | ||
| 152 | } else { | ||
| 153 | say 'Done.'; | ||
| 154 | exit(1); | ||
| 155 | } | ||
| 156 | } | ||
| 157 | # --- End Main --- | ||
| 158 | |||
| 159 | |||
| 160 | package SimplePrompt; | ||
| 161 | |||
| 162 | use vars qw/ $Terminal /; | ||
| 163 | |||
| 164 | sub prompt { | ||
| 165 | my ($msg, $flags, $others) = @_; | ||
| 166 | |||
| 167 | # simple prompt | ||
| 168 | return get_input ($msg) | ||
| 169 | unless $flags; | ||
| 170 | |||
| 171 | $flags =~ s/^-//o; # ignore leading dash | ||
| 172 | |||
| 173 | # 'waiting for yes/no' prompt, possibly with a default value | ||
| 174 | if ($flags =~ /^yn(d)?/i) { | ||
| 175 | return yes_no ($msg, $others); | ||
| 176 | } | ||
| 177 | |||
| 178 | # prompt with a menu of possible answers | ||
| 179 | if ($flags =~ /^m/i) { | ||
| 180 | return menu ($msg, $others); | ||
| 181 | } | ||
| 182 | |||
| 183 | # default: again a simple prompt | ||
| 184 | return get_input ($msg); | ||
| 185 | } | ||
| 186 | |||
| 187 | sub yes_no { | ||
| 188 | my ($msg, $default_answer) = @_; | ||
| 189 | while (1) { | ||
| 190 | my $answer = get_input ($msg); | ||
| 191 | return $default_answer if $default_answer and $answer =~ /^\s*$/o; | ||
| 192 | return 'y' if $answer =~ /^(1|y|yes|ano)$/; | ||
| 193 | return 'n' if $answer =~ /^(0|n|no|ne)$/; | ||
| 194 | } | ||
| 195 | } | ||
| 196 | |||
| 197 | sub get_input { | ||
| 198 | my ($msg) = @_; | ||
| 199 | local $^W = 0; | ||
| 200 | my $line = $Terminal->readline ($msg); | ||
| 201 | chomp $line; # remove newline | ||
| 202 | $line =~ s/^\s*//; $line =~ s/\s*$//; # trim whitespaces | ||
| 203 | $Terminal->addhistory ($line) if $line; | ||
| 204 | return $line; | ||
| 205 | } | ||
| 206 | |||
| 207 | sub menu { | ||
| 208 | my ($msg, $ra_menu) = @_; | ||
| 209 | my @data = @$ra_menu; | ||
| 210 | |||
| 211 | my $count = @data; | ||
| 212 | # die "Too many -menu items" if $count > 26; | ||
| 213 | # die "Too few -menu items" if $count < 1; | ||
| 214 | |||
| 215 | my $max_char = chr(ord('a') + $count - 1); | ||
| 216 | my $menu = ''; | ||
| 217 | |||
| 218 | my $next = 'a'; | ||
| 219 | foreach my $item (@data) { | ||
| 220 | $menu .= ' ' . $next++ . '.' . $item . "\n"; | ||
| 221 | } | ||
| 222 | while (1) { | ||
| 223 | print STDOUT $msg . "\n$menu"; | ||
| 224 | my $answer = get_input (">"); | ||
| 225 | |||
| 226 | # blank and escape answer accepted as undef | ||
| 227 | return undef if $answer =~ /^\s*$/o; | ||
| 228 | return undef | ||
| 229 | if length $answer == 1 && $answer eq "\e"; | ||
| 230 | |||
| 231 | # invalid answer not accepted | ||
| 232 | if (length $answer > 1 || ($answer lt 'a' || $answer gt $max_char) ) { | ||
| 233 | print STDOUT "(Please enter a-$max_char)\n"; | ||
| 234 | next; | ||
| 235 | } | ||
| 236 | |||
| 237 | # valid answer | ||
| 238 | return $data[ord($answer)-ord('a')]; | ||
| 239 | } | ||
| 240 | } | ||
| 241 | |||
| 242 | |||
| 243 | __END__ |