Class: KnifeGitHubClone::GithubClone

Inherits:
Chef::Knife show all
Defined in:
lib/chef/knife/github_clone.rb

Overview

Clones a cookbook version to your local cookbooks directory

Instance Method Summary collapse

Instance Method Details

#repo_clone(repo, cookbook_name) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/chef/knife/github_clone.rb', line 79

def repo_clone(repo, cookbook_name)
  if repo.nil? || repo.empty?
    ui.info("Processing [ UNKNOWN ] #{cookbook_name}")
    Chef::Log.info("Cannot find the repository: #{cookbook_name} within github")
    return nil
  end

  repo_link = get_repo_clone_link()
  if repo[cookbook_name].nil? || repo[cookbook_name][repo_link].nil? || repo[cookbook_name][repo_link].empty?
    ui.info("Processing [ UNKNOWN ] #{cookbook_name}")
    Chef::Log.info("Cannot find the link for the repository with the name: #{cookbook_name}")
    return nil
  end

 	github_url = repo[cookbook_name][repo_link]
  token = get_github_token
  if token.nil? || token.empty?
    clone_url = github_url
  else
    uri = URI.parse(github_url)
    uri.userinfo = "#{token}:x-oauth-basic"
    clone_url = URI.join(uri)
  end
  cookbook_path = get_cookbook_path(cookbook_name)
  if File.exists?(cookbook_path)
    ui.info("Processing [ SKIP    ] #{cookbook_name}")
    Chef::Log.info("Path to #{cookbook_path} already exists, skipping.")
  else
    ui.info("Processing [ CLONE   ] #{cookbook_name}")
    Chef::Log.info("Cloning repository to: #{cookbook_path}")
    Chef::Log.debug("Using url: #{clone_url}")
    shell_out!("git clone #{clone_url} #{cookbook_path}") 
  end
end

#runObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/chef/knife/github_clone.rb', line 48

def run

  #executing shell commands
	extend Chef::Mixin::ShellOut

  # validate base options from base module.
  validate_base_options      

  # Display information if debug mode is on.
  display_debug_info

  # Gather all repo information from github.
  all_repos = get_all_repos(@github_organizations.reverse)

  # Get all chef cookbooks and versions (hopefully chef does the error handeling).
  cookbooks = rest.get_rest("/cookbooks?num_version=1")

  # Get the cookbook names from the command line
  @cookbook_name = name_args.first unless name_args.empty?
  if @cookbook_name
    repo = all_repos.select { |k,v| v["name"] == @cookbook_name }
    repo_clone(repo, @cookbook_name)
  elsif config[:all]
    cookbooks.each do |c,v|
      repo_clone(all_repos, c)
    end
  else
    Chef::Log.error("Please specify a repository name")
  end
end