by shigemk2

当面は技術的なことしか書かない

ディレクトリをgisty postしたけどできなかった

gistコマンドよりちょっと便利なgisty - SWDYH

まあまあこんな感じでございます。 ディレクトリごとgisty postするとError: invalid fileってなるな。

$ gisty post gisty-test
Error: invalid file

$ gisty post gisty-test/*
https://gist.github.com/4a4b092267d927bfed52
Cloning into '4a4b092267d927bfed52'...
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 5 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (5/5), done.
Checking connectivity... done.

ソースコード

パラメータがファイルじゃないなら(ディレクトリなどだったら)例外を飛ばすようになっている。

gisty.rb

  def build_params paths
    list = (Array === paths ? paths : [paths]).map { |i| Pathname.new i }
    raise InvalidFileException if list.any?{ |i| !i.file? }

    params = {}
    params['files'] = {}
    list.each_with_index do |i, index|
      params['files'][i.basename.to_s] = { 'content' => IO.read(i) }
    end
    params
  end

post.rb

cmd :post, 'file1 file2 ...', 'post new gist' do |fs|
  if fs.size > 0
    begin
      url = @g.create fs
    rescue Gisty::InvalidFileException => e
      puts "Error: invalid file"
    rescue Exception => e
      puts "Error: #{e}"
    else
      id = url.split('/').last
      html_url = "https://gist.github.com/#{id}"
      puts html_url
      system "open #{html_url}" if /darwin/ === RUBY_PLATFORM
      @g.clone id
    end
  end
end