From 3710bbfda1800c124582ec5a13cef76a79d45d12 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Sun, 6 May 2012 21:16:54 +0000 Subject: [PATCH] Use optparse instead rdoc/usage in reposman (#10837). git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@9648 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- extra/svn/reposman.rb | 193 +++++++++++++++++------------------------- 1 file changed, 79 insertions(+), 114 deletions(-) diff --git a/extra/svn/reposman.rb b/extra/svn/reposman.rb index 3eae905a8..9ae746afe 100755 --- a/extra/svn/reposman.rb +++ b/extra/svn/reposman.rb @@ -1,96 +1,12 @@ #!/usr/bin/env ruby -# == Synopsis -# -# reposman: manages your repositories with Redmine -# -# == Usage -# -# reposman [OPTIONS...] -s [DIR] -r [HOST] -# -# Examples: -# reposman --svn-dir=/var/svn --redmine-host=redmine.example.net --scm subversion -# reposman -s /var/git -r redmine.example.net -u http://svn.example.net --scm git -# -# == Arguments (mandatory) -# -# -s, --svn-dir=DIR use DIR as base directory for svn repositories -# -r, --redmine-host=HOST assume Redmine is hosted on HOST. Examples: -# -r redmine.example.net -# -r http://redmine.example.net -# -r https://example.net/redmine -# -k, --key=KEY use KEY as the Redmine API key (you can use the -# --key-file option as an alternative) -# -# == Options -# -# -o, --owner=OWNER owner of the repository. using the rails login -# allow user to browse the repository within -# Redmine even for private project. If you want to -# share repositories through Redmine.pm, you need -# to use the apache owner. -# -g, --group=GROUP group of the repository. (default: root) -# --scm=SCM the kind of SCM repository you want to create (and -# register) in Redmine (default: Subversion). -# reposman is able to create Git and Subversion -# repositories. For all other kind, you must specify -# a --command option -# -u, --url=URL the base url Redmine will use to access your -# repositories. This option is used to automatically -# register the repositories in Redmine. The project -# identifier will be appended to this url. Examples: -# -u https://example.net/svn -# -u file:///var/svn/ -# if this option isn't set, reposman won't register -# the repositories in Redmine -# -c, --command=COMMAND use this command instead of "svnadmin create" to -# create a repository. This option can be used to -# create repositories other than subversion and git -# kind. -# This command override the default creation for git -# and subversion. -# -f, --force force repository creation even if the project -# repository is already declared in Redmine -# --key-file=PATH path to a file that contains the Redmine API key -# (use this option instead of --key if you don't -# the key to appear in the command line) -# -t, --test only show what should be done -# -h, --help show help and exit -# -v, --verbose verbose -# -V, --version print version and exit -# -q, --quiet no log -# -# == References -# -# You can find more information on the redmine's wiki : http://www.redmine.org/wiki/redmine/HowTos - - -require 'getoptlong' -require 'rdoc/usage' +require 'optparse' require 'find' require 'etc' -Version = "1.3" +Version = "1.4" SUPPORTED_SCM = %w( Subversion Darcs Mercurial Bazaar Git Filesystem ) -opts = GetoptLong.new( - ['--svn-dir', '-s', GetoptLong::REQUIRED_ARGUMENT], - ['--redmine-host', '-r', GetoptLong::REQUIRED_ARGUMENT], - ['--key', '-k', GetoptLong::REQUIRED_ARGUMENT], - ['--key-file', GetoptLong::REQUIRED_ARGUMENT], - ['--owner', '-o', GetoptLong::REQUIRED_ARGUMENT], - ['--group', '-g', GetoptLong::REQUIRED_ARGUMENT], - ['--url', '-u', GetoptLong::REQUIRED_ARGUMENT], - ['--command' , '-c', GetoptLong::REQUIRED_ARGUMENT], - ['--scm', GetoptLong::REQUIRED_ARGUMENT], - ['--test', '-t', GetoptLong::NO_ARGUMENT], - ['--force', '-f', GetoptLong::NO_ARGUMENT], - ['--verbose', '-v', GetoptLong::NO_ARGUMENT], - ['--version', '-V', GetoptLong::NO_ARGUMENT], - ['--help' , '-h', GetoptLong::NO_ARGUMENT], - ['--quiet' , '-q', GetoptLong::NO_ARGUMENT] - ) - $verbose = 0 $quiet = false $redmine_host = '' @@ -133,36 +49,84 @@ module SCM end -begin - opts.each do |opt, arg| - case opt - when '--svn-dir'; $repos_base = arg.dup - when '--redmine-host'; $redmine_host = arg.dup - when '--key'; $api_key = arg.dup - when '--key-file' - begin - $api_key = File.read(arg).strip - rescue Exception => e - $stderr.puts "Unable to read the key from #{arg}: #{e.message}" - exit 1 - end - when '--owner'; $svn_owner = arg.dup; $use_groupid = false; - when '--group'; $svn_group = arg.dup; $use_groupid = false; - when '--url'; $svn_url = arg.dup - when '--scm'; $scm = arg.dup.capitalize; log("Invalid SCM: #{$scm}", :exit => true) unless SUPPORTED_SCM.include?($scm) - when '--command'; $command = arg.dup - when '--verbose'; $verbose += 1 - when '--test'; $test = true - when '--force'; $force = true - when '--version'; puts Version; exit - when '--help'; RDoc::usage - when '--quiet'; $quiet = true - end +def read_key_from_file(filename) + begin + $api_key = File.read(filename).strip + rescue Exception => e + $stderr.puts "Unable to read the key from #{filename}: #{e.message}" + exit 1 end -rescue - exit 1 end +def set_scm(scm) + $scm = v.capitalize + unless SUPPORTED_SCM.include?($scm) + log("Invalid SCM: #{$scm}\nValid SCM are: #{SUPPORTED_SCM.join(', ')}", :exit => true) + end +end + +optparse = OptionParser.new do |opts| + opts.banner = "Usage: reposman.rb [OPTIONS...] -s [DIR] -r [HOST] -k [KEY]" + opts.separator("") + + opts.separator("Required arguments:") + opts.on("-s", "--svn-dir", "use DIR as base directory for svn repositories") {|v| $repos_base = v} + opts.on("-r", "--redmine-host", "assume Redmine is hosted on HOST. Examples:", " -r redmine.example.net", " -r http://redmine.example.net", " -r https://redmine.example.net") {|v| $redmine_host = v} + opts.on("-k", "--key KEY", "use KEY as the Redmine API key", "(you can use --key-file option as an alternative)") {|v| $api_key = v} + + opts.separator("") + opts.separator("Options:") + opts.on("-o", "--owner OWNER", "owner of the repository. using the rails login", + "allows users to browse the repository within", + "Redmine even for private projects. If you want to", + "share repositories through Redmine.pm, you need", + "to use the apache owner.") {|v| $svn_owner = v; $use_groupid = false} + opts.on("-g", "--group GROUP", "group of the repository (default: root)") {|v| $svn_group = v; $use_groupid = false} + opts.on("-u", "--url URL", "the base url Redmine will use to access your", + "repositories. This option is used to register", + "the repositories in Redmine automatically. The", + "project identifier will be appended to this url.", + "Examples:", + " -u https://example.net/svn", + " -u file:///var/svn/", + "if this option isn't set, reposman won't register", + "the repositories in Redmine") {|v| $svn_url = v} + opts.on( "--scm SCM", "the kind of SCM repository you want to create", + "(and register) in Redmine (default: Subversion).", + "reposman is able to create Git and Subversion", + "repositories.", + "For all other kind, you must specify a --command", + "option") {|v| set_scm(v)} + opts.on("-c", "--command COMMAND", "use this command instead of `svnadmin create` to", + "create a repository. This option can be used to", + "create repositories other than subversion and git", + "kind.", + "This command override the default creation for", + "git and subversion.") {|v| $command = v} + opts.on( "--key-file FILE", "path to a file that contains the Redmine API key", + "(use this option instead of --key if you don't", + "want the key to appear in the command line)") {|v| read_key_from_file(v)} + opts.on("-t", "--test", "only show what should be done") {$test = true} + opts.on("-f", "--force", "force repository creation even if the project", "repository is already declared in Redmine") {$force = true} + opts.on("-v", "--verbose", "verbose") {$verbose += 1} + opts.on("-V", "--version", "show version and exit") {puts Version; exit} + opts.on("-h", "--help", "show help and exit") do + puts opts + exit 1 + end + opts.on("-q", "--quiet", "no log") {$quiet = true} + opts.separator("") + + opts.separator("Examples:") + opts.separator(" reposman.rb --svn-dir=/var/svn --redmine-host=redmine.host") + opts.separator(" reposman.rb -s /var/git -r redmine.host -u http://git.host --scm git") + opts.separator("") + opts.separator("You can find more information on the redmine's wiki:\nhttp://www.redmine.org/projects/redmine/wiki/HowTos") + + opts.summary_width = 25 +end +optparse.parse! + if $test log("running in test mode") end @@ -179,7 +143,8 @@ end $svn_url += "/" if $svn_url and not $svn_url.match(/\/$/) if ($redmine_host.empty? or $repos_base.empty?) - RDoc::usage + puts "Some arguments are missing. Use reposman.rb --help for getting help." + exit 1 end unless File.directory?($repos_base) -- 2.39.5