From 03348fa2713f477caf76bdda40b042c20a634d2a Mon Sep 17 00:00:00 2001 From: Vadim Smal Date: Sat, 7 Oct 2017 17:16:22 +0300 Subject: [PATCH] Implement command line --- Gemfile.lock | 4 +- bin/fabricio | 4 + fabricio.gemspec | 9 ++- lib/fabricio/cli.rb | 105 ++++++++++++++++++++++++++ lib/fabricio/models/abstract_model.rb | 4 + lib/fabricio/models/credential.rb | 18 +++++ lib/fabricio/models/organization.rb | 2 +- 7 files changed, 140 insertions(+), 6 deletions(-) create mode 100755 bin/fabricio create mode 100644 lib/fabricio/cli.rb create mode 100644 lib/fabricio/models/credential.rb diff --git a/Gemfile.lock b/Gemfile.lock index beb15fa..6e9466d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -3,6 +3,7 @@ PATH specs: fabricio (1.3.0) faraday + thor GEM remote: https://rubygems.org/ @@ -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) @@ -58,4 +60,4 @@ DEPENDENCIES webmock BUNDLED WITH - 1.14.5 + 1.14.6 diff --git a/bin/fabricio b/bin/fabricio new file mode 100755 index 0000000..724c30f --- /dev/null +++ b/bin/fabricio @@ -0,0 +1,4 @@ +#!/usr/bin/env ruby +require "fabricio/cli" + +Fabricio::CLI.start(ARGV) diff --git a/fabricio.gemspec b/fabricio.gemspec index 44fa18d..17ff51b 100644 --- a/fabricio.gemspec +++ b/fabricio.gemspec @@ -6,8 +6,8 @@ require 'fabricio/version' Gem::Specification.new do |spec| spec.name = "fabricio" spec.version = Fabricio::VERSION - spec.authors = ["Egor Tolstoy"] - spec.email = ["e.tolstoy@rambler-co.ru"] + spec.authors = ["Egor Tolstoy", "Vadim Smal"] + spec.emails = ["igrekde@gmail.com", "cognitivedisson@gmail.com"] spec.summary = "A simple gem that fetches mobile application statistics from Fabric.io API." spec.license = "MIT" @@ -15,11 +15,12 @@ Gem::Specification.new do |spec| 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' diff --git a/lib/fabricio/cli.rb b/lib/fabricio/cli.rb new file mode 100644 index 0000000..141d0e0 --- /dev/null +++ b/lib/fabricio/cli.rb @@ -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 diff --git a/lib/fabricio/models/abstract_model.rb b/lib/fabricio/models/abstract_model.rb index d00c7b8..cc5025c 100644 --- a/lib/fabricio/models/abstract_model.rb +++ b/lib/fabricio/models/abstract_model.rb @@ -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 diff --git a/lib/fabricio/models/credential.rb b/lib/fabricio/models/credential.rb new file mode 100644 index 0000000..b52c500 --- /dev/null +++ b/lib/fabricio/models/credential.rb @@ -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 diff --git a/lib/fabricio/models/organization.rb b/lib/fabricio/models/organization.rb index 45dd4de..bbda77f 100644 --- a/lib/fabricio/models/organization.rb +++ b/lib/fabricio/models/organization.rb @@ -19,4 +19,4 @@ def initialize(attributes) end end end -end \ No newline at end of file +end