Class: GhBackup::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/gh_backup.rb

Constant Summary collapse

REPO_LIMIT =
100

Instance Method Summary collapse

Constructor Details

#initialize(color = true) ⇒ Base

Returns a new instance of Base.



12
13
14
# File 'lib/gh_backup.rb', line 12

def initialize(color=true)
  @color = color
end

Instance Method Details

#backup(f, account, user, pass) ⇒ Object

Run all backups



62
63
64
65
66
67
68
69
# File 'lib/gh_backup.rb', line 62

def backup(f, , user, pass)
  repos = YAML.load_file(f).map(&:strip)
  repo_count = repos.length
  repos.each_with_index do |repo, idx|
    puts colorize("\n#{idx+1} of #{repo_count}: Cloning #{repo}", 32)
    clone_repo(user, pass, , repo)  
  end
end

#clone_org_repos(org, user, password, limit = REPO_LIMIT) ⇒ Object

You can download the repos directly without having to save to YAML



89
90
91
92
93
94
95
96
97
# File 'lib/gh_backup.rb', line 89

def clone_org_repos(org, user, password, limit=REPO_LIMIT)
  all_repos = list_org_repos(org, user, password, limit)
  repo_count = all_repos.length
  all_repos.each_with_index.map do |repo, i| 
    repository = split_repo_name(repo)
    puts colorize("\n#{i+1} of #{repo_count}: Cloning #{repo}", 32)
    clone_repo(user, password, org, repository)
  end
end

#clone_repo(username, password, account, repo) ⇒ Object

Clone a single repository and put it in the backups dir repos are saved under a folder with the current date i.e 02012014 etc



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/gh_backup.rb', line 49

def clone_repo(username, password, , repo)
  now = format_current_date
  make_dir("./backups")
  make_dir("./backups/#{now}")
  path = "./backups/#{now}/#{repo}"
  if Dir.exists?(path)
    puts colorize("Dir already exists. Skipping clone", 31)
  else
    system "git clone #{repo_url(username, password, , repo)} #{path}"
  end
end

#colorize(str, code) ⇒ Object

Colorize a string for the terminal



28
29
30
31
32
33
34
# File 'lib/gh_backup.rb', line 28

def colorize(str, code)
  if @color
    "\e[#{code}m#{str}\e[0m"
  else
    str
  end
end

#format_current_dateObject

Returns current date as a string



43
44
45
# File 'lib/gh_backup.rb', line 43

def format_current_date
  Time.now.strftime("%m%d%Y")
end

#list_org_repos(org, user, password, limit = REPO_LIMIT) ⇒ Object

Returns an array of all the repos for an organization



79
80
81
82
83
84
85
86
# File 'lib/gh_backup.rb', line 79

def list_org_repos(org, user, password, limit=REPO_LIMIT)
  auth = "#{user}:#{password}"
  path = "https://api.github.com/orgs/#{org}/repos?per_page=#{limit}"
  repos = `curl -u #{auth} #{path}`
  JSON.parse(repos).map do |repo|
    repo["clone_url"]
  end
end

#make_dir(dir) ⇒ Object



36
37
38
39
40
# File 'lib/gh_backup.rb', line 36

def make_dir(dir)
  unless Dir.exists?(dir)
    Dir.mkdir(dir) 
  end
end

#repo_url(username, password, account, repo) ⇒ Object

Create a string path to a github repo with auth info



18
19
20
# File 'lib/gh_backup.rb', line 18

def repo_url(username, password, , repo)
  "https://#{username}:#{password}@github.com/#{}/#{repo}.git"
end

#split_repo_name(repo_path) ⇒ Object

Extracts the repo name from a github URL i.e github.com/boxuk/wedge.js.git



73
74
75
76
# File 'lib/gh_backup.rb', line 73

def split_repo_name(repo_path)
  parts = repo_path.split(/\//)
  parts.last.split(/.git/).first
end

#zipf(name, dir) ⇒ Object

Zip up a directory in the backups folder



23
24
25
# File 'lib/gh_backup.rb', line 23

def zipf(name, dir)
  system "tar zcvf #{name}.tar.gz backups/#{dir}"
end