Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add --skip-empty to skip gisting empty files #270

Merged
merged 2 commits into from
Oct 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions bin/gist
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ Instead of creating a new gist, you can update an existing one by passing its ID
or URL with "-u". For this to work, you must be logged in, and have created the
original gist with the same GitHub account.

Usage: #{executable_name} [-o|-c|-e] [-p] [-s] [-R] [-d DESC] [-a] [-u URL] [-P] [-f NAME|-t EXT]* FILE*
If you want to skip empty files, use the --skip-empty flag. If all files are
empty no gist will be created.

Usage: #{executable_name} [-o|-c|-e] [-p] [-s] [-R] [-d DESC] [-a] [-u URL]
[--skip-empty] [-P] [-f NAME|-t EXT]* FILE*
#{executable_name} --login
#{executable_name} [-l|-r]

Expand Down Expand Up @@ -107,6 +111,10 @@ Usage: #{executable_name} [-o|-c|-e] [-p] [-s] [-R] [-d DESC] [-a] [-u URL] [-P]

opts.on("--no-open")

opts.on("--skip-empty", "Skip gisting empty files") do
options[:skip_empty] = true
end

opts.on("-P", "--paste", "Paste from the clipboard to gist") do
options[:paste] = true
end
Expand Down Expand Up @@ -195,7 +203,8 @@ begin
end
end

puts Gist.multi_gist(files, options)
output = Gist.multi_gist(files, options)
puts output if output
end

rescue Gist::Error => e
Expand Down
10 changes: 8 additions & 2 deletions lib/gist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ def gist(content, options = {})
# @option options [String] :update the URL or id of a gist to update
# @option options [Boolean] :copy (false) Copy resulting URL to clipboard, if successful.
# @option options [Boolean] :open (false) Open the resulting URL in a browser.
# @option options [Boolean] :skip_empty (false) Skip gisting empty files.
# @option options [Symbol] :output (:all) The type of return value you'd like:
# :html_url gives a String containing the url to the gist in a browser
# :short_url gives a String contianing a git.io url that redirects to html_url
Expand All @@ -110,10 +111,15 @@ def multi_gist(files, options={})
json[:files] = {}

files.each_pair do |(name, content)|
raise "Cannot gist empty files" if content.to_s.strip == ""
json[:files][File.basename(name)] = {:content => content}
if content.to_s.strip == ""
raise "Cannot gist empty files" unless options[:skip_empty]
else
json[:files][File.basename(name)] = {:content => content}
end
end

return if json[:files].empty? && options[:skip_empty]

existing_gist = options[:update].to_s.split("/").last
if options[:anonymous]
access_token = nil
Expand Down