#!/usr/bin/env ruby require 'gli' require 'gitlink' include GLI::App program_desc 'Allows you to quickly open your GitHub project in a web browser from the command line' version Gitlink::VERSION subcommand_option_handling :normal arguments :strict desc 'Name of remote' arg_name 'remote_name' default_value 'origin' flag [:r, :remote] desc 'Opens project\'s GitHub page' command [:project, :p] do |c| c.action do |global_options,options,args| %x(open "#{@base_project_url}") end end desc 'Opens project\'s GitHub page to a specific branch' arg_name 'Branch Name (leave blank for current)' command [:branch, :b] do |c| c.action do |global_options,options,args| @current_branch = args.first if args.any? %x(open "#{@base_project_url}/tree/#{@current_branch}") end end desc 'Opens project\'s pull requests page' command [:pulls, :pr] do |c| c.action do |global_options,options,args| %x(open "#{@base_project_url}/pulls") end end desc 'Open project\'s wiki page' command [:wiki, :w] do |c| c.action do |global_options,options,args| %x(open "#{@base_project_url}/wiki") end end default_command :project pre do |global,command,options,args| no_repo_error = "gitlink only works from within git repos" raise no_repo_error unless %x(git rev-parse --is-inside-work-tree) @remote = global[:remote] available_urls = %x(git remote -v) remotes = available_urls.split("\n").select do |remote| remote = remote.split("\t") remote[0] == @remote end raise "this project does not have any remotes" if remotes.empty? project_params = remotes.first.match(/\.*(.com|.net|.org|.biz|.edu)(:|\/)(.*)(.git|\s)/) unsupported_url_message = "Your remote has an unsupported URL. Feel free to make an issue at github.com/cacqw7/gitlink" raise unsupported_url_message if project_params.size == 0 project_params = project_params[3] @base_project_url = "http://github.com/#{project_params}" @current_branch = %x(git symbolic-ref -q HEAD | sed -e 's|^refs/heads/||') @current_branch.chomp! true end post do |global,command,options,args| # Post logic here # Use skips_post before a command to skip this # block on that command only end on_error do |exception| # Error logic here # return false to skip default error handling true end exit run(ARGV)