#!/usr/bin/env ruby require 'rubygems' require 'cmdparse' require 'yaml' require 'fileutils' require 'json' require 'highline/import' sauce_dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')) $LOAD_PATH.unshift(sauce_dir) unless $LOAD_PATH.include?(sauce_dir) # special case for sauce connect if ARGV.length > 0 && ARGV[0] == 'connect' require 'sauce/connect' system ([Sauce::Connect.find_sauce_connect] + ARGV[1..100]).join(" ") else require 'sauce' cmd = CmdParse::CommandParser.new(true, true) cmd.program_name = "sauce " cmd.program_version = [0, 1, 0] cmd.add_command(CmdParse::HelpCommand.new) # configure configure = CmdParse::Command.new('configure', false) configure.short_desc = "Configure Sauce OnDemand credentials" configure.set_execution_block do |args| if args.length < 2: puts "Usage: sauce configure USERNAME ACCESS_KEY" exit 1 end username = args[0] access_key = args[1] dir = File.join(File.expand_path("~"), ".sauce") FileUtils.mkdir(dir) unless File.directory?(dir) out = File.new(File.join(dir, "ondemand.yml"), 'w') out.write(YAML.dump({"username" => username, "access_key" => access_key})) out.close() end cmd.add_command(configure) #create create = CmdParse::Command.new('create', false) create.short_desc = "Create a new Sauce OnDemand account" create.set_execution_block do |args| puts "Let's create a new account!" username = ask("Username: ") password = ask("Password: ") { |q| q.echo = "*" } email = ask("Email: ") name = ask("Full name: ") body = { :username => username, :password => password, :email => email, :name => name, :token => "c8eb3e2645005bcbbce7e2c208c6b7a71555d908" }.to_json begin result = RestClient.post("http://saucelabs.com/rest/v1/users", body, :content_type => :json, :accept => :json) rescue => e begin puts "ERROR: #{JSON.load(e.response)['errors']}" rescue puts "ERROR: #{e.response}" end else result = JSON.load(result) puts "Account #{username} created successfully." puts "Your access key is: #{result['access_key']}" end end cmd.add_command(create) cmd.parse end