Class: Chef::Knife::GithubDiff

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

Instance Method Summary (collapse)

Instance Method Details

- (Object) do_diff(name, version)



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/chef/knife/github_diff.rb', line 110

def do_diff(name, version)
  # Check to see if there is a tag matching the version
  Dir.chdir("#{@github_tmp}/git/#{name}")
  # Only checkout in github mode
  if config[:github]
      if `git tag`.split("\n").include?(version)
        ui.info("Tag version #{version} found, checking that out for diff")
        # Tag found so checkout that tag
        `git checkout -b #{version}`
        if !$?.exitstatus == 0
          ui.error("Failed to checkout branch #{version}")
          exit 1
        end
      else
        ui.info("Version #{version} of #{name} has no tag, using latest for diff")
      end
  end

  FileUtils.remove_entry("#{@github_tmp}/git/#{name}/.git")
  output = `git diff --color #{@github_tmp}/git/#{name} #{@github_tmp}/cb/#{name}-#{version} 2>&1`
  if output.length == 0
    ui.info("No differences found")
  else
    ui.msg(output)
  end
end

- (Object) get_cookbook_copy(name, version)



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/chef/knife/github_diff.rb', line 137

def get_cookbook_copy(name, version)
  Dir.mkdir("#{@github_tmp}/cb")
  args = ['cookbook', 'download',  name ]
  args.push version if version
  Dir.chdir("#{@github_tmp}/cb")
  download = Chef::Knife::CookbookDownload.new(args)
  download.config[:download_directory] = "#{@github_tmp}/cb"
  download.run

  Dir.entries("#{@github_tmp}/cb").each do |d|
    if d =~ /#{name}-(.*)/
      version = $1
    end
  end
  return version
end

- (Object) run



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

def run
# The run method.  The entry point into the class

  # 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.
  get_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 name from the command line
  @cookbook_name = name_args.first unless name_args.empty?
  cookbook_version = name_args[1] unless name_args[1].nil?
  if @cookbook_name
    repo = get_all_repos.select { |k,v| v["name"] == @cookbook_name }
  else
    #repos = all_repos 
    Chef::Log.error("Please specify a cookbook name")
    exit 1
  end
  
  if repo.empty?
    Chef::Log.error("Cannot find the repository: #{} within github")
    exit 1
  end

  github_link = (repo[@cookbook_name][(get_repo_clone_link)])
  if github_link.nil? || github_link.empty?
    Chef::Log.error("Cannot find the link for the repository with the name: #{@cookbook_name}")
    exit 1
  end

  if config[:github]
   get_clone(github_link, @cookbook_name)
  else # Copy downloaded version to #{@github_tmp}/git
      cpath = cookbook_path_valid?(@cookbook_name, false)
      if cpath.nil?
        Chef::Log.error("Cannot find any local repository with the name: #{@cookbook_name}")
        Chef::Log.error("Please use the option -g if you want to diff the github repository")
        exit 1
      end
      tpath = "#{@github_tmp}/git"
      if ! File.exists?(tpath)
          FileUtils.makedirs(tpath)
      end
      FileUtils.cp_r cpath, tpath
  end

	    version = get_cookbook_copy(@cookbook_name, cookbook_version)

	    do_diff(@cookbook_name, version)
  FileUtils.remove_entry(@github_tmp)
end