Skip to content

Commit

Permalink
Implement command line
Browse files Browse the repository at this point in the history
  • Loading branch information
CognitiveDisson committed Oct 7, 2017
1 parent 0b8c592 commit 03348fa
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 6 deletions.
4 changes: 3 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ PATH
specs:
fabricio (1.3.0)
faraday
thor

GEM
remote: https://rubygems.org/
Expand Down Expand Up @@ -41,6 +42,7 @@ GEM
json (>= 1.8, < 3)
simplecov-html (~> 0.10.0)
simplecov-html (0.10.1)
thor (0.20.0)
webmock (3.0.1)
addressable (>= 2.3.6)
crack (>= 0.3.2)
Expand All @@ -58,4 +60,4 @@ DEPENDENCIES
webmock

BUNDLED WITH
1.14.5
1.14.6
4 changes: 4 additions & 0 deletions bin/fabricio
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env ruby
require "fabricio/cli"

Fabricio::CLI.start(ARGV)
9 changes: 5 additions & 4 deletions fabricio.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@ require 'fabricio/version'
Gem::Specification.new do |spec|
spec.name = "fabricio"
spec.version = Fabricio::VERSION
spec.authors = ["Egor Tolstoy"]
spec.email = ["[email protected]"]
spec.authors = ["Egor Tolstoy", "Vadim Smal"]
spec.emails = ["[email protected]", "[email protected]"]

spec.summary = "A simple gem that fetches mobile application statistics from Fabric.io API."
spec.license = "MIT"

spec.files = `git ls-files -z`.split("\x0").reject do |f|
f.match(%r{^(test|spec|features)/})
end
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }

spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.add_dependency 'faraday'
spec.add_dependency "thor"

spec.add_development_dependency 'rake', '~> 10.0'
spec.add_development_dependency 'rspec', '~> 3.0'
Expand Down
105 changes: 105 additions & 0 deletions lib/fabricio/cli.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
require 'thor'
require 'fabricio'
require 'fileutils'
require 'yaml'

module Fabricio
class CLI < Thor

# Constants
CREDENTIAL_DIRECTORY_PATH = "#{Dir.home}/.fabricio"
CREDENTIAL_FILE_PATH = "#{CREDENTIAL_DIRECTORY_PATH}/.credential"
FABRIC_GRAPHQL_API_URL = 'https://api-dash.fabric.io/graphql'

desc "credential", "Setup credential"
def credential
say("Setup credential")
credential = ask_credential

tmp_client = Fabricio::Client.new do |config|
config.username = credential.email
config.password = credential.password
end

organization = tmp_client.organization.get
unless organization.nil?
say("Successful login to #{organization.name}")
create_credential_file(email, password)
else
say("Login failed")
end

say("Complete!")
end

desc "organization", "Obtain organization"
def organization
say("#{client.organization.get.to_s}")
end

desc "apps", "Obtain all app"
def apps
say("#{client.app.all}")
end

desc "app", "Obtain single app"
option :app_id => :required, :type => :string
def app(app_id)
say("#{client.app.get(app_id).to_s}")
end

desc "builds", "Obtain all builds"
option :app_id => :required, :type => :string
def builds(app_id)
say("#{client.build.all(app_id).to_s}")
end

desc "build", "Obtain single build"
option :app_id => :required, :type => :string
option :version => :required, :type => :string
option :build_number => :required, :type => :string
def build(app_id, version, build_number)
say("#{client.build.get(app_id, version, build_number).to_s}")
end

private
def client
email = ""
password = ""
if File.file?(CREDENTIAL_FILE_PATH)
credential = YAML.load_file(CREDENTIAL_FILE_PATH)
email = credential['email']
password = credential['password']
else
ask_credential
end

client = Fabricio::Client.new do |config|
config.username = email
config.password = password
end
end

def create_credential_file(credential)
FileUtils.mkdir(CREDENTIAL_DIRECTORY_PATH)
credential_hash = {
"email" => credential.email,
"password" => credential.password
}
File.open(CREDENTIAL_FILE_PATH,'w') do |f|
f.write credential_hash.to_yaml
end
say("Your credential in #{CREDENTIAL_FILE_PATH}")
end

def ask_credential
say("We have to know you're email from fabric account")
email = ask("email: ")
say("Now we want your password. Do not be afraid, it is stored locally")
password = ask("password: ", :echo => false)
say("")
Fabricio::Model::Credential(email, password)
end

end
end
4 changes: 4 additions & 0 deletions lib/fabricio/models/abstract_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ def method_missing(*args)
return json_value if json_value
raise NoMethodError.new("There's no method called #{args.first} here -- please try again.", args.first)
end

def to_s
@json
end
end
end
end
18 changes: 18 additions & 0 deletions lib/fabricio/models/credential.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module Fabricio
module Model
# This model represents a credential
class Credential
attr_reader :email, :password

# Returns a Credential model object
#
# @param email [String]
# @param password [String]
# @return [Fabricio::Model::Build]
def initialize(email, password)
@id = email
@version = password
end
end
end
end
2 changes: 1 addition & 1 deletion lib/fabricio/models/organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ def initialize(attributes)
end
end
end
end
end

0 comments on commit 03348fa

Please sign in to comment.