Class: Carggo::CLI
- Inherits:
-
Thor
- Object
- Thor
- Carggo::CLI
- Defined in:
- lib/carggo.rb
Instance Method Summary collapse
Instance Method Details
#add(*args) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/carggo.rb', line 28 def add(*args) if File.exists?("Cargo.toml") args.each do |lib| crate = self.find(lib) if crate[:err].nil? name = crate[:name] version = crate[:version] File.open("Cargo.toml", "r+") do |out| File.foreach("Cargo.toml") do |line| if line =~ /#{name}/ next end out << line if line =~ /ependen/ out << "\n#{name} = '#{version}'" end end end else puts "Cannot locate #{lib} or having network issues." end end else puts "Not a Cargo project directory." end end |
#find(args) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/carggo.rb', line 12 def find(args) begin escape_uri = URI.escape("https://crates.io/api/v1/crates//#{args}") uri = URI.parse(escape_uri) res = JSON.parse(Net::HTTP.get(uri)) name = res["crate"]["name"] version = res["crate"]["max_version"] printable_response = "#{name} = '#{version}'" puts printable_response return {name: name, version: version, err: nil} rescue return {err: true} end end |
#remove(*args) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/carggo.rb', line 56 def remove(*args) if File.exists?("Cargo.toml") args.each do |lib| open("Cargo.toml", 'r') do |out| open("Cargo.toml.tmp", 'w') do |out2| out.each_line do |line| out2.write(line) unless line.start_with? lib end end end end FileUtils.mv 'Cargo.toml.tmp', 'Cargo.toml' else puts "Not a Cargo project directory." end end |