<commit_before>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 = <PASSWORD>.password
      end

      organization = tmp_client.organization.get
      unless organization.nil?
        say("Successful login to #{organization.name}")
        create_credential_file(credential)
      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.new(email, password)
    end

  end
end
<commit_msg>Fixed a bug that crashed fabricio at credentials overwrite<commit_after>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 = <PASSWORD>
      end

      organization = tmp_client.organization.get
      unless organization.nil?
        say("Successful login to #{organization.name}")
        create_credential_file(credential)
      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_p(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.new(email, password)
    end

  end
end

{'fraction_non_alphanumeric': 0.11514414727335881, 'fraction_numerical': 0.0, 'mean_word_length': 2.7542372881355934, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 31, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5068370', 'n_tokens_mistral': 1818, 'n_tokens_neox': 1786, 'n_words': 424}