qman 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/bin/qman +170 -23
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e31f4466a10fe5aa017b43172923806cf1103de9
4
- data.tar.gz: 5d07bb1083ba8bcaa3525d723f687f3d6597a2c4
3
+ metadata.gz: bf5f32c19e378fc755053b09de5f312d1d5b5f71
4
+ data.tar.gz: 5d5a3d0ad44154199871a5461a5510bf6e24942c
5
5
  SHA512:
6
- metadata.gz: 77d249615211d833abadd0ef258704c44cff781ee57bc9972c2a82d42c98c1387d29814977204606aa62889e3329a2971446f0aa8d6695135541333a3402e43f
7
- data.tar.gz: 862093278f7e660003056342f530784c813d1cf361c9cafbdf6991c4021cc91c6c30360e6cf5324463ddcd1448f9aaed15fb22dfe6cbcc679572e16873b292a8
6
+ metadata.gz: 8ece25b35c86d245223d81ccc3bb878e55bc36bd4a6287518da2c3ee35c6583953ffd769bf449fe0333902ce018cad5d58c2b6ec1a9004251270fe6079c811be
7
+ data.tar.gz: dde64336543a34d9f4146961acf879bd3ec18e54c7c16f340716313c51888bfce83a1bdd9032407445d51e949f623af5d2a7f32084011afe1ecc0888a2af8abe
data/bin/qman CHANGED
@@ -1,33 +1,180 @@
1
1
  #!/usr/bin/env ruby
2
- Dir.chdir(File.expand_path("~/qman"))
3
- git_pull = `git pull`
4
- if git_pull.include? "Already up-to-date."
5
- puts "#######################################################"
6
- puts "################### pull success ######################"
7
- puts "#######################################################"
8
- else
9
- puts "error while pulling files"
2
+ require 'optparse'
3
+ require 'pry'
4
+ require 'git'
5
+ require 'logger'
6
+ require 'open-uri'
7
+ require 'json'
8
+ require_relative 'mod'
9
+ require_relative 'settings'
10
+ require_relative 'custom_helpers'
11
+
12
+ ARGV << '-h' if ARGV.empty?
13
+
14
+ options = {:list => nil, :help => nil, :read_only => nil}
15
+
16
+ parser = OptionParser.new do |opts|
17
+ opts.banner = print_mod + "Usage: qman [options]"
18
+ opts.separator ""
19
+ opts.separator "Specific options: "
20
+ opts.on('-w', '--web', 'Open the github web page for the qman file') do
21
+ options[:web] = true
22
+ end
23
+
24
+ opts.on('-l', '--list', 'Lists all qman files available') do
25
+ options[:list] = true
26
+ end
27
+
28
+ opts.on('-s', '--stackoverflow', 'Searches in stack overflow') do |query|
29
+ options[:stackoverflow] = query
30
+ end
31
+
32
+ opts.on('-f', '--find file', 'Finds qman files') do |file|
33
+ options[:find] = file
34
+ end
35
+
36
+ opts.on_tail('-h', '--help', 'Prints this help') do
37
+ puts opts
38
+ exit
39
+ end
40
+ end
41
+
42
+ parser.parse!
43
+
44
+ def git_status_empty? (repo)
45
+ repo.status.changed.empty? and repo.status.untracked.empty?
10
46
  end
11
47
 
12
- unless ARGV[0].include? ("--")
13
- vi_cmd = "vim #{ARGV[0]}"
14
- puts "vi_cmd = #{vi_cmd}"
15
- system(vi_cmd)
16
- system("git add #{ARGV[0]}")
17
- system("git commit -m 'nit'; git push origin master")
18
- else
19
- cmd_arg = ARGV[0]
20
- cmd_arg = cmd_arg.gsub(/--/, '')
21
-
22
- case cmd_arg
23
- when 'list'
24
- list_files = `ls`
25
- puts list_files
48
+
49
+ vi_bin = "vim"
50
+
51
+ settings = Settings.new
52
+ logger = Logger.new(STDOUT)
53
+ logger.level = :FATAL
54
+ repo = Git.open(settings.repo_path, :log => logger)
55
+
56
+ Dir.chdir(File.expand_path(settings.repo_path))
57
+ print "Updating qman local copy... "
58
+
59
+ begin
60
+ repo.pull
61
+ rescue
62
+ puts "Could not pull changes from database."
63
+ if git_status_empty? (repo)
64
+ puts "This is probably due to a faulty internet connection. Opening in read-only mode."
65
+ gets
66
+ vi_bin += " -R"
26
67
  else
27
- puts 'unkown command'
68
+ puts "The repository is in an invalid state. Please go to #{settings.repo_path} and fix manually."
69
+ exit
28
70
  end
29
71
  end
72
+ puts "OK"
30
73
 
74
+ if options.values.all? { |v| v.nil? }
75
+ file_name = ARGV[0]
31
76
 
77
+ vi_cmd = "#{vi_bin} #{file_name}"
78
+
79
+ ok = false
80
+ while not ok do
32
81
 
82
+ system(vi_cmd)
33
83
 
84
+ # Update index to avoid fake changes in git
85
+ Dir.chdir(settings.repo_path){
86
+ update_index_command = "git update-index --refresh"
87
+ %x[#{update_index_command}]
88
+ }
89
+
90
+ # Early exit if there are no changes
91
+ if git_status_empty? (repo)
92
+ puts "No changes. exiting"
93
+ exit
94
+ end
95
+
96
+
97
+ repo.add(:all=>true)
98
+ repo.commit("Qman-automated edits")
99
+
100
+ print "Updating changes in remote database... "
101
+ begin
102
+ repo.push
103
+ ok = true
104
+ rescue
105
+ begin
106
+ repo.pull
107
+ repo.push
108
+ ok = true
109
+ rescue => e
110
+ puts "Error while writing the file. Check #{file_name} to resolve conflicts."
111
+ print "Would you like to change it now? (y/n) "
112
+ ans = STDIN.gets.chomp
113
+ if ans.include?("Y") or ans.include? ("y")
114
+ ok = false
115
+ else
116
+ repo.reset
117
+ ok = true
118
+ end
119
+ end
120
+ end
121
+ puts "OK"
122
+ end
123
+ elsif options[:web]
124
+ url = "\"" + settings.repo_url + "/blob/master/" + ARGV[0] + "\""
125
+ system("open #{url}")
126
+ elsif options[:list]
127
+ puts "\t Available qman files"
128
+ list_files = `ls`
129
+ puts list_files
130
+ elsif options[:find]
131
+ query = options[:find]
132
+
133
+ results = []
134
+
135
+ available_files = (`ls`).split("\n")
136
+ available_files.each do |file|
137
+ distance = levenshtein_distance(file, query)
138
+ results << file if distance <= 2 or file.include?(query)
139
+ end
140
+
141
+ if results.count != 0
142
+ puts "\n\t Results for '#{query}':"
143
+ results.each do |res|
144
+ print "- #{res} "
145
+ end
146
+ puts ""
147
+ else
148
+ puts print_no_results(query)
149
+ end
150
+ elsif options[:stackoverflow]
151
+ query = options[:stackoverflow]
152
+
153
+ url = "https://api.stackexchange.com/2.2/search/advanced?order=desc&sort=relevance&q=#{query}&accepted=True&closed=False&site=stackoverflow"
154
+ content = open(url).read
155
+ json_obj = JSON.parse(content)
156
+
157
+ system("clear")
158
+ puts ">>> Top10 Stackoverflow questions related to your question: #{query}\n\n"
159
+ counter = 0
160
+ json_obj["items"].first(10).each do |item|
161
+ puts "#{counter}) #{item['title']}"
162
+ counter = counter + 1
163
+ end
164
+
165
+ puts "\n\nWhat answer would you like to see? From 0 to 9"
166
+ ans = STDIN.gets.chomp
167
+ ans_i = ans.to_i
168
+ if ans.match(/[0-9]/) and ans.to_i.between?(0, 9)
169
+ ans_i = ans.to_i
170
+ question_id = json_obj["items"][ans_i]["question_id"]
171
+
172
+ url = "https://api.stackexchange.com//2.2/questions/#{question_id}/answers?order=desc&sort=votes&site=stackoverflow"
173
+ content = open(url).read
174
+ question_obj = JSON.parse(content)
175
+
176
+ answer_id = question_obj["items"][0]["answer_id"]
177
+ answer_url = "https://stackoverflow.com/a/#{answer_id}"
178
+ system("open #{answer_url}")
179
+ end
180
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: qman
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Márcio PaivaMatheus Barros