Sha256: b9c596e3788fdd1c83b475ab1205eec029c75b76e9e49be8ad7bd1a82d6b1276

Contents?: true

Size: 1.73 KB

Versions: 16

Compression:

Stored size: 1.73 KB

Contents

module GoodData::Command
  class Auth < Base
    def connect
      unless defined? @connected
        GoodData.connect user, password, url, :auth_token => auth_token
        @connected = true
      end
      @connected
    end

    def user
      ensure_credentials
      @credentials[:username]
    end

    def password
      ensure_credentials
      @credentials[:password]
    end

    def url
      ensure_credentials
      @credentials[:url]
    end

    def auth_token
      ensure_credentials
      @credentials[:auth_token]
    end

    def credentials_file
      "#{home_directory}/.gooddata"
    end

    def ensure_credentials
      return if defined? @credentials
      unless @credentials = read_credentials
        @credentials = ask_for_credentials
      end
      @credentials
    end

    def read_credentials
      if File.exists?(credentials_file) then
        config = File.read(credentials_file)
        JSON.parser.new(config, :symbolize_names => true).parse
      end
    end

    def ask_for_credentials
      puts "Enter your GoodData credentials."
      user = ask("Email")
      password = ask("Password", :secret => true)
      auth_token = ask("Authorization Token")
      { :username => user, :password => password, :auth_token => auth_token }
    end

    def store
      credentials = ask_for_credentials

      ovewrite = if File.exist?(credentials_file)
        ask "Overwrite existing stored credentials", :answers => %w(y n)
      else
        'y'
      end

      if ovewrite == 'y'
        File.open(credentials_file, 'w', 0600) do |f|
          f.puts JSON.pretty_generate(credentials)
        end
      else
        puts 'Aborting...'
      end
    end

    def unstore
      FileUtils.rm_f(credentials_file)
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
gooddata-0.6.0.pre6 lib/gooddata/commands/auth.rb
gooddata-0.6.0.pre5 lib/gooddata/commands/auth.rb
gooddata-0.6.0.pre4 lib/gooddata/commands/auth.rb
gooddata-0.6.0.pre3 lib/gooddata/commands/auth.rb
gooddata-0.6.0.pre2 lib/gooddata/commands/auth.rb
gooddata-0.5.16 lib/gooddata/commands/auth.rb
gooddata-0.5.15 lib/gooddata/commands/auth.rb
gooddata-0.5.14 lib/gooddata/commands/auth.rb
gooddata-0.5.13 lib/gooddata/commands/auth.rb
gooddata-0.5.12 lib/gooddata/commands/auth.rb
gooddata-0.5.11 lib/gooddata/commands/auth.rb
gooddata-0.5.10 lib/gooddata/commands/auth.rb
gooddata-0.5.9 lib/gooddata/commands/auth.rb
gooddata-0.5.8 lib/gooddata/commands/auth.rb
gooddata-0.5.7 lib/gooddata/commands/auth.rb
gooddata-0.5.6 lib/gooddata/commands/auth.rb