Class: KnifeGithubCleanup::GithubCleanup

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

Instance Method Summary (collapse)

Instance Method Details

- (Object) repo_cleanup(repo)



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/chef/knife/github_cleanup.rb', line 88

def repo_cleanup(repo)
  cookbook_path = config[:cookbook_path] || Chef::Config[:cookbook_path]
  cookbook = File.join(cookbook_path.first,repo)
  if File.exists?(cookbook)
    if repo_status_clean?(repo, cookbook)
      # delete the repo
      ui.info("Processing [D] #{repo}")
      FileUtils.remove_entry(cookbook) 
    end
  else
    puts "cannot find repo path for: #{repo}" unless config[:all]
  end
end

- (Boolean) repo_status_clean?(repo, cookbook)

Returns:

  • (Boolean)


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/chef/knife/github_cleanup.rb', line 102

def repo_status_clean?(repo, cookbook)
  shell_out!("git fetch", :cwd => cookbook)
  status = shell_out!("git status", :cwd => cookbook)
  unless status.stdout == "# On branch master\nnothing to commit (working directory clean)\n"
    ui.info("Processing [C] #{repo} (Action needed!)")
    status.stdout.lines.each { |l| puts l.sub( /^/, "    ") }
    return false   
  end
  log = shell_out!("git log --branches --not --remotes --simplify-by-decoration --decorate --oneline", :cwd => cookbook)
  unless log.stdout.empty?
    ui.info("Processing [B] #{repo} (Action needed!)")
    ui.info("    Please check your branches, one of them has unsaved changes")
    log.stdout.lines.each { |l| puts l.sub( /^/, "    ") }
    return false   
  end
  return true
end

- (Object) run



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
# File 'lib/chef/knife/github_cleanup.rb', line 57

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_cleanup(@cookbook_name)
  elsif config[:all]
    cookbooks.each do |c,v|
      repo_cleanup(c)
    end
  else
    Chef::Log.error("Please specify a repo name")
  end
end