Sha256: cf24809cff7d9e93156087bf153c0e2c7cdfaa2d89ccab42323bef3e48417fb5

Contents?: true

Size: 1.91 KB

Versions: 7

Compression:

Stored size: 1.91 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

    unless user_name
      begin
        print "User name: "
        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: "
        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

7 entries across 7 versions & 1 rubygems

Version Path
td-0.10.18 lib/td/command/account.rb
td-0.10.17 lib/td/command/account.rb
td-0.10.16 lib/td/command/account.rb
td-0.10.15 lib/td/command/account.rb
td-0.10.13 lib/td/command/account.rb
td-0.10.12 lib/td/command/account.rb
td-0.10.11 lib/td/command/account.rb