Skip to content

Registry related commands #85

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

Closed
wants to merge 3 commits into from
Closed
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
16 changes: 8 additions & 8 deletions src/cli.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ module Shards
puts
puts "Commands:"
puts " check"
#puts " info <package>"
puts " info <package>"
puts " init"
puts " install"
puts " list"
puts " prune"
#puts " search <query>"
puts " search <query>"
puts " update"
#puts " update [package package ...]"
puts
Expand All @@ -36,9 +36,9 @@ module Shards
case args[0]? || DEFAULT_COMMAND
when "check"
Commands::Check.run(path)
#when "info"
# display_help_and_exit(opts) unless args[1]?
# Commands::Info.run(args[1])
when "info"
display_help_and_exit(opts) unless args[1]?
Commands::Info.run(path, args[1])
when "init"
Commands::Init.run(path)
when "install"
Expand All @@ -47,9 +47,9 @@ module Shards
Commands::List.run(path)
when "prune"
Commands::Prune.run(path)
#when "search"
# display_help_and_exit(opts) unless args[1]?
# Commands::Search.run(args[1])
when "search"
display_help_and_exit(opts) unless args[1]?
Commands::Search.run(path, args[1])
when "update"
Commands::Update.run(path)
#Commands.update(*args[1 .. -1])
Expand Down
6 changes: 3 additions & 3 deletions src/commands/command.cr
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ module Shards
@lockfile_path = File.join(@path, LOCK_FILENAME)
end

abstract def run
abstract def run(*args)

def self.run(path)
new(path).run
def self.run(path, *args)
new(path).run(*args)
end

def spec
Expand Down
23 changes: 23 additions & 0 deletions src/commands/info.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require "./command"
require "../registry/client"

module Shards
module Commands
class Info < Command
def run(name)
client = Registry::Client.new

shard = client.shard(name)
latest = client.latest_version(name)

puts " #{ "name".colorize(:green) }: #{ shard["name"] }"
puts " #{ "url".colorize(:green) }: #{ shard["url"] }"
puts " #{ "version".colorize(:green) }: #{ latest["version"] }"
puts "#{ "released".colorize(:green) }: #{ latest["released_at"] }"

rescue Registry::Client::NotFound
puts "#{ "Not found:".colorize(:red) } shard #{ name } not found"
end
end
end
end
24 changes: 24 additions & 0 deletions src/commands/search.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require "colorize"
require "./command"
require "../registry/client"

module Shards
module Commands
class Search < Command
def run(query)
client = Registry::Client.new
shards = client.search(query)

if shards.as_a.any?
puts "Search results:\n\n"

shards.each do |shard|
puts " #{ shard["name"].colorize(:cyan) } #{ shard["url"] }"
end
else
puts "No results."
end
end
end
end
end
6 changes: 4 additions & 2 deletions src/config.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ module Shards
SPEC_FILENAME = "shard.yml"
LOCK_FILENAME = "shard.lock"

CACHE_DIRECTORY = ENV["SHARDS_CACHE_PATH"]? || File.join(Dir.current, ".shards")
INSTALL_PATH = ENV["SHARDS_INSTALL_PATH"]? || File.join(Dir.current, "libs")
CACHE_DIRECTORY = ENV.fetch("SHARDS_CACHE_PATH", File.join(Dir.current, ".shards"))
INSTALL_PATH = ENV.fetch("SHARDS_INSTALL_PATH", File.join(Dir.current, "libs"))

DEFAULT_COMMAND = "install"
DEFAULT_VERSION = "0"

REGISTRY_URL = ENV.fetch("SHARDS_REGISTRY_URL", "https://crystal-shards-registry.herokuapp.com")

@@production = false

def self.production?
Expand Down
65 changes: 65 additions & 0 deletions src/registry/client.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
require "http/client"
require "json"
require "uri"

module Shards
module Registry
class Client
class Error < Exception
end

class NotFound < Exception
end

def initialize(base_url = REGISTRY_URL)
@base_url = URI.parse(REGISTRY_URL)
end

def search(query)
get("/api/v1/shards/search", { query: query })
end

def shard(name)
get("/api/v1/shards/#{ URI.escape(name) }")
end

def versions(name)
get("/api/v1/shards/#{ URI.escape(name) }/versions")
end

def latest_version(name)
get("/api/v1/shards/#{ URI.escape(name) }/versions/latest")
end

private def get(path, query = nil)
response = HTTP::Client.get(url(path, query))

if response.status_code < 400
return JSON.parse(response.body)
end

if response.status_code == 404
raise NotFound.new
end

raise Error.new("GET #{ path }: got #{ response.status_code }\n#{ response.body }")
end

private def url(path, query = nil)
uri = @base_url.dup
uri.path = path

case query
when String
uri.query = query
when Hash
uri.query = query
.map { |k, v| "#{ URI.escape(k.to_s) }=#{ URI.escape(v.to_s) }" }
.join("&")
end

uri.to_s
end
end
end
end