Class: KnifeGithubRepoDestroy::GithubRepoDestroy

Inherits:
Chef::Knife
  • Object
show all
Defined in:
lib/chef/knife/github_repo_destroy.rb

Instance Method Summary collapse

Instance Method Details

#create_cookbook(name, tmp) ⇒ Object

Create the cookbook template for upload

Parameters:

  • name (String)

    cookbook name tmp [String] temp location



202
203
204
205
206
207
# File 'lib/chef/knife/github_repo_destroy.rb', line 202

def create_cookbook(name, tmp)
  args = [ name ]
  create = Chef::Knife::CookbookCreate.new(args)
  create.config[:cookbook_path] = tmp
  create.run
end

#get_useremailObject

Get the email from passwd file or git config

Parameters:

  • nil


180
181
182
183
184
185
# File 'lib/chef/knife/github_repo_destroy.rb', line 180

def get_useremail()
  email = nil
  git_user_email = %x(git config user.email).strip
  email = git_user_email if git_user_email
  email
end

#get_userloginObject

Get the email from passwd file or git config

Parameters:

  • nil


189
190
191
192
193
194
195
196
# File 'lib/chef/knife/github_repo_destroy.rb', line 189

def get_userlogin()
  email = get_useremail()
  unless email
    puts "Cannot continue without login information. Please define the git email address."
    exit 1
  end
   = email.split('@').first
end

#get_usernameObject

Get the username from passwd file or git config

Parameters:

  • nil


169
170
171
172
173
174
175
176
# File 'lib/chef/knife/github_repo_destroy.rb', line 169

def get_username()
  username = ENV['USER']
  passwd_user = %x(getent passwd #{username} | cut -d ':' -f 5).chomp
  username = passwd_user if passwd_user
  git_user_name = %x(git config user.name).strip
  username = git_user_name if git_user_name
  username
end

#git_commit_and_push(cookbook_path, github_ssh_url) ⇒ Object

Set the username in README.md

Parameters:

  • cookbook_path (String)

    cookbook path github_ssh_url [String] github ssh url from repo



129
130
131
132
133
134
135
# File 'lib/chef/knife/github_repo_destroy.rb', line 129

def git_commit_and_push(cookbook_path, github_ssh_url)
  shell_out!("git init", :cwd => cookbook_path )
  shell_out!("git add .", :cwd => cookbook_path ) 
  shell_out!("git commit -m 'creating initial cookbook structure from the knife-github plugin' ", :cwd => cookbook_path ) 
  shell_out!("git remote add origin #{github_ssh_url} ", :cwd => cookbook_path ) 
  shell_out!("git push -u origin master", :cwd => cookbook_path ) 
end

#runObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/chef/knife/github_repo_destroy.rb', line 57

def run
  extend Chef::Mixin::ShellOut

  # validate base options from base module.
  validate_base_options      

  # Display information if debug mode is on.
  display_debug_info

  # Get the repo name from the command line
  name = name_args.first

  # Get the organization name from config
  org = locate_config_value('github_organizations').first

  if name.nil? || name.empty? 
    Chef::Log.error("Please specify a repository name")
    exit 1
  end 
   
  user = get_userlogin

  if config[:github_user_repo]
    url = @github_url + "/api/" + @github_api_version + "/repos/#{user}/#{name}"
    Chef::Log.debug("Destroying repository in user environment: #{user}")
  else
    url = @github_url + "/api/" + @github_api_version + "/repos/#{org}/#{name}"
    Chef::Log.debug("Destroying repository in organization: #{org}")
  end

  # @github_tmp = locate_config_value("github_tmp") || '/var/tmp/gitcreate'
  # @github_tmp = "#{@github_tmp}#{Process.pid}"

  # Get token information
  token = get_github_token()

  # Get body data for post
  # body = get_body_json(name, desc)

  # Creating the local repository 
  # Chef::Log.debug("Creating the local repository based on template")
  # create_cookbook(name, @github_tmp)

  # cookbook_path = File.join(@github_tmp, name)

  # Updating README.md if needed.
  # update_readme(cookbook_path)
 
  # Updateing metadata.rb if needed.
  # update_metadata(cookbook_path)

  # Creating the github repository
  params = {}
  params[:url] = url
  params[:token] = token
  params[:action] = "DELETE"
  repo = connection.request(params)
  puts "Repo: #{name} is deleted" if repo.nil?

  # github_ssh_url = repo['ssh_url']

  # Chef::Log.debug("Commit and push local repository")      
  # Initialize the local git repo
  # git_commit_and_push(cookbook_path, github_ssh_url)

  # Chef::Log.debug("Removing temp files")
  # FileUtils.remove_entry(@github_tmp)
end

#update_metadata(cookbook_path) ⇒ Object

Set the username and email in metadata.rb

Parameters:

  • name (String)

    cookbook path



153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/chef/knife/github_repo_destroy.rb', line 153

def (cookbook_path)
  contents = ''
  username = get_username
  email    = get_useremail
   = File.join(cookbook_path, "metadata.rb")
  File.foreach() do |line|
    line.gsub!(/YOUR_COMPANY_NAME/,username) if username
    line.gsub!(/YOUR_EMAIL/,email) if email
    contents = contents << line
  end
  File.open(, 'w') {|f| f.write(contents) }
  return nil
end

#update_readme(cookbook_path) ⇒ Object

Set the username in README.md

Parameters:

  • name (String)

    cookbook path



139
140
141
142
143
144
145
146
147
148
149
# File 'lib/chef/knife/github_repo_destroy.rb', line 139

def update_readme(cookbook_path)
  contents = ''
  username = get_username
  readme = File.join(cookbook_path, "README.md")
  File.foreach(readme) do |line|
    line.gsub!(/TODO: List authors/,"#{username}\n")
    contents = contents << line
  end
  File.open(readme, 'w') {|f| f.write(contents) }
  return nil
end