Class: Difgist

Inherits:
Object
  • Object
show all
Defined in:
lib/difgist/difgist.rb

Instance Method Summary collapse

Constructor Details

#initialize(arguments) ⇒ Difgist

Returns a new instance of Difgist.



11
12
13
14
15
16
17
18
19
20
# File 'lib/difgist/difgist.rb', line 11

def initialize(arguments)
  @config_yml = File.expand_path("~/.difgist.yml")
  @diff_file = File.expand_path("~/.difgist.diff")
  @params = ["hostname", "username", "password"]
  @options = {}
  @settings = {}
  load_config
  set_options(arguments)
  check_options
end

Instance Method Details

#check_optionsObject



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/difgist/difgist.rb', line 70

def check_options
  if @options[:description] == nil
    then description = "posted by difgist"
  end
  if @options[:target_file] == nil
    then target_file = ""
  else
    if File.exist?("#{@options[:target_file]}") == false
      puts "No such file. Dump diff."
      @options[:target_file] = ""
    end
  end
end

#init_configObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/difgist/difgist.rb', line 37

def init_config
  print "Initializing...\n"
  # hostname
  print "= hit enter if you want yo use github, or enter endpoint url if you use github enterprise =\n"
  print "[hostname]: "
  hostname = gets.strip
  if hostname == "" then @settings[:hostname] = "api.github.com" else @settings[:hostname] = hostname end
  # username
  print "[username]: "
  @settings[:username] = gets.strip
  # password
  print "[password]: "
  @settings[:password] = gets.strip
  # finish
  print "Init done. [username] = #{@settings[:username]}, [hostname] = #{@settings[:hostname]}\n"
  # write to yml
  File.open(@config_yml, "w") {|f| f.write(YAML.dump(@settings)) }
  system("chmod 600 #{@config_yml}")
end

#load_configObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/difgist/difgist.rb', line 22

def load_config
  config = YAML.load_file(@config_yml)
  is_initialized = true
  @params.each{|key|
    if not config or not config.has_key?(:"#{key}")
      is_initialized = false
      break
    else @settings[:"#{key}"] = config[:"#{key}"]
    end
  }
  if not is_initialized
    init_config
  end
end

#runObject



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
# File 'lib/difgist/difgist.rb', line 84

def run
  begin
    if @options[:target_file] != nil
      @diff_file = @options[:target_file]
      @filename = Pathname.new("#{@options[:target_file]}").basename
    else
      if File.exist?(".svn")
        then
          system("LANG=C svn diff #{@options[:target_file]} > #{@diff_file}")
      elsif system('git rev-parse --is-inside-work-tree >/dev/null 2>&1') == true
        then
          system("git diff #{@options[:target_file]} > #{@diff_file}")
        else
      end
      @filename = 'difgist.diff'
    end

    payload = nil
    File.open(@diff_file, "r") do |f|
      payload = {
        :description => @options[:description],
        :public => true,
        :files => {
          @filename => {
            :content => f.read
          }
        }
      }
      response = JSON.parse(RestClient.post("https://#{@settings[:username]}:#{@settings[:password]}@#{@settings[:hostname]}/gists", payload.to_json, :content_type => "text/json"))
      puts "Done. " + response['html_url']
    end
  rescue
    puts "Error."
  end
end

#set_options(arguments) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/difgist/difgist.rb', line 57

def set_options(arguments)
  @optparse = OptionParser.new
  @optparse.banner = "Usage: -d --description, -f -file"
  @optparse.on('-d value','--description value') { |opt| @options[:description] = opt }
  @optparse.on('-f value','--file value') { |opt| @options[:target_file] = opt }
  begin
    @optparse.parse!(arguments)
  rescue
    puts "Bad params."
    exit
  end
end