Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions bin/gitlab-clone
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ class GitlabClone
opts = Slop.parse do |o|
o.bool '-h', '--help', 'help'
o.bool '-c', '--clone', 'clone'
o.bool '-ca', '--clone_all', 'clone_all'
o.bool '-w', '--web', 'web'
o.bool '-wa', '--web_all', 'web_all'
o.bool '-l', '--list', 'list'
o.bool '-la', '--list_all', 'list_all'
o.bool '-lg', '--list_groups', 'list_groups'
o.bool '-v', '--version', 'version'
o.string '-g', '--group', 'group'
o.bool '-o', '--github', 'github'
Expand Down Expand Up @@ -65,23 +69,32 @@ class GitlabClone
end

begin
if opts[:help]
Gitlab.printhelp
if opts[:help]
Gitlab.printhelp
elsif opts[:reconfigure]
Setup.configure
elsif opts[:clone]
class_to_use.clone(0, clone_group)
elsif opts[:web]
class_to_use.clone(1, clone_group)
elsif opts[:clone_all]
class_to_use.clone_all(0)
elsif opts[:web_all]
class_to_use.clone_all(1)
elsif opts[:version]
Version::version
elsif opts[:config]
Gitlab.config
elsif opts[:list]
class_to_use.list_repos(clone_group)
elsif opts[:list_all]
class_to_use.list_all_repos
elsif opts[:list_groups]
puts class_to_use.get_groups
end
rescue
rescue => e
puts Rainbow("Looks like there was an issue. Please verify host and api version with \"#{File.basename($0)} -n\" to show configuration.").red.italic
puts Rainbow("Error message: #{e} \nBacktrace: #{e.backtrace}").red.italic
end
end
end
Expand Down
38 changes: 28 additions & 10 deletions lib/gitlab.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@ class Gitlab
require "setup"
require "rainbow"

user_home = ENV['HOME']
user_defined_dir = "code" ### need to add the ability to set this dynamically
HOME = "#{user_home}/#{user_defined_dir}"

def self.printhelp
puts Rainbow("---------------------------------------------------------------------------------------\n").green
puts Rainbow("\t\t###### Options for #{File.basename($0)} ######\n\n").green
puts Rainbow("\t-h, --help: shows this help message").green
puts Rainbow("\t-c, --clone: clones all repos from https://gitlab_server/groups/Cookbooks
into a ~/code/Cookbooks directory by default").green
puts Rainbow("\t-ca, --clone_all: will clone all repositories ssh").green
puts Rainbow("\t-w, --web: will clone using web protocol instead of ssh").green
puts Rainbow("\t-l, --list: will give you a list of repos in git").green
puts Rainbow("\t-wa, --web_all: will clone all repositories using web protocol instead of ssh").green
puts Rainbow("\t-l, --list: will give you a list of repos in git (need param group)").green
puts Rainbow("\t-la, --list_all: will give you a list of all repos in git").green
puts Rainbow("\t-g, --group: will let you choose which gitlab group to look for repos in").green
puts Rainbow("\t-o, --github: will allow you to clone from Github").green
puts Rainbow("\t-n, --config: will print your current configuration settings").green
Expand All @@ -23,18 +22,37 @@ def self.printhelp
end

def self.list_repos(group_name)
repos_list = get_repos(group_name)
if (repos_list = get_repos(group_name)).empty?
puts Rainbow("\n---\nNot repositories were found in group '#{group_name}'\n---\n").red
return
end
puts Rainbow("-------------------------------------------------------------------\n").green
puts Rainbow("\tThe following #{repos_list["projects"].length} repo(s) were found in the group #{group_name}.").green
puts Rainbow("\tThe following #{repos_list["projects"].length} repo(s) were found in the group '#{Rainbow(group_name).red}'.").green
repos_list["projects"].length.times do |get|
puts Rainbow("\t\t#{repos_list["projects"][get]["name"]}").blue
end
puts Rainbow("\n-------------------------------------------------------------------").green
end

def self.list_all_repos
get_groups.each{|group_name,group_id|
list_repos(group_name)
}
end

def self.clone_all(web)
get_groups.each{|group_name,group_id|
clone(web, group_name)
}
end

def self.clone(web, group_name)
repos_list = get_repos(group_name)
repos_dir = "#{HOME}/#{group_name}"
if (repos_list = get_repos(group_name)).empty?
puts Rainbow("\n---\nNot repositories were found in group '#{group_name}'\n---\n").red
return
end

repos_dir = "#{Setup.get_backup_dir}/#{group_name}"

if File.directory?("#{repos_dir}")
FileUtils::mkdir_p repos_dir
Expand Down Expand Up @@ -94,7 +112,7 @@ def self.config
if Setup.github_precheck
puts Rainbow("\tCurrent Github token:\t\t #{Setup.get_github_token}").green
end
puts Rainbow("\tCurrent home directory:\t\t #{HOME}").green
puts Rainbow("\tCurrent backup directory:\t\t #{Setup.get_backup_dir}").green
puts
puts Rainbow("#######################################################################").green
end
Expand Down
22 changes: 19 additions & 3 deletions lib/setup.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
class Setup
require "rainbow"

user_home=ENV['HOME']
CONFIG = "#{user_home}/.gitlab_config"
USER_HOME=ENV['HOME']
CONFIG = "#{USER_HOME}/.gitlab_config"

### This checks to see if the json we are getting from the file is valid or not.
def self.valid_json
Expand Down Expand Up @@ -81,7 +81,7 @@ def self.configure

gitlab_host = "#{host}/api/#{ver}"
params["gitlab_server"] = gitlab_host
params["gitlab_server"]

puts Rainbow("What is your token?\nExample: 3pe14gZfap:\nToken: ").purple
params["gitlab_token"] = STDIN.gets.chomp
puts Rainbow("Do you wish to do the Github token now too? ").purple
Expand All @@ -92,6 +92,10 @@ def self.configure
params["github_token"] = STDIN.gets.chomp
end

puts Rainbow("Backup directory? (default #{USER_HOME}/code).").purple
dir = STDIN.gets.chomp
params['backup_dir'] = dir.empty? ? "#{USER_HOME}/code" : dir

save = JSON.generate(params)
puts Rainbow("\n\nUpdating config file with the following:").purple
puts Rainbow("Gitlab server: #{params["gitlab_server"]}").purple
Expand Down Expand Up @@ -176,4 +180,16 @@ def self.get_gitlabserver
return params["gitlab_server"]
end
end

def self.get_backup_dir
unless Setup.precheck
puts Rainbow("\n\nWhoops! Looks like we have not setup a config before...\n").yellow
Setup.configure
else
JSON.parse(File.read(CONFIG))
params = JSON.parse(File.read(CONFIG))
return params["backup_dir"]
end
end

end