Sha256: f24cb35ea6c5ba26963f27670cdfd0e686f5df2a93fee22877b0c94df32ad8c5

Contents?: true

Size: 1.98 KB

Versions: 11

Compression:

Stored size: 1.98 KB

Contents

module TreasureData
module Command

  def account(op)
    op.banner << "\noptions:\n"

    force = false
    op.on('-f', '--force', 'overwrite current account setting', TrueClass) {|b|
      force = true
    }

    user_name = op.cmd_parse

    conf = nil
    begin
      conf = Config.read
    rescue ConfigError
    end
    if conf && conf['account.apikey']
      unless force
        if conf['account.user']
          $stderr.puts "Account is already configured with '#{conf['account.user']}' account."
        else
          $stderr.puts "Account is already configured."
        end
        $stderr.puts "Add '-f' option to overwrite."
        exit 0
      end
    end

    puts "Enter your Treasure Data credentials."
    unless user_name
      begin
        print "Email: "
        line = STDIN.gets || ""
        user_name = line.strip
      rescue Interrupt
        $stderr.puts "\ncanceled."
        exit 1
      end
    end

    if user_name.empty?
      $stderr.puts "canceled."
      exit 0
    end

    client = nil

    3.times do
      begin
        system "stty -echo"  # TODO termios
        print "Password (typing will be hidden): "
        password = STDIN.gets || ""
        password = password[0..-2]  # strip \n
      rescue Interrupt
        $stderr.print "\ncanceled."
        exit 1
      ensure
        system "stty echo"   # TODO termios
        print "\n"
      end

      if password.empty?
        $stderr.puts "canceled."
        exit 0
      end

      begin
        # enalbe SSL for the authentication
        client = Client.authenticate(user_name, password, :ssl=>true)
      rescue TreasureData::AuthError
        $stderr.puts "User name or password mismatched."
      end

      break if client
    end
    return unless client

    $stderr.puts "Authenticated successfully."

    conf ||= Config.new
    conf["account.user"] = user_name
    conf["account.apikey"] = client.apikey
    conf.save

    $stderr.puts "Use '#{$prog} db:create <db_name>' to create a database."
  end

end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
td-0.10.46 lib/td/command/account.rb
td-0.10.45 lib/td/command/account.rb
td-0.10.44 lib/td/command/account.rb
td-0.10.43 lib/td/command/account.rb
td-0.10.42 lib/td/command/account.rb
td-0.10.41 lib/td/command/account.rb
td-0.10.40 lib/td/command/account.rb
td-0.10.39 lib/td/command/account.rb
td-0.10.38 lib/td/command/account.rb
td-0.10.37 lib/td/command/account.rb
td-0.10.36 lib/td/command/account.rb