class Gigawatt::Commands::Init

Attributes

options[RW]

Public Class Methods

new(settings, options, directory) click to toggle source
# File lib/gigawatt/commands/init.rb, line 30
def initialize(settings, options, directory)
  @settings = settings
  @options = options
  @directory = directory
  @access_key = OAuth.token(settings.access_key)
  @cache = Cache.new(settings, @access_key)
end
run!(settings) click to toggle source
# File lib/gigawatt/commands/init.rb, line 6
def self.run!(settings)
  directory = nil
  p = Trollop::Parser.new
  options = p.parse
  directory = p.leftovers.first

  Trollop::die "Please supply a directory" unless directory
  Trollop::die "Directory does not exist" unless File.exists?(directory)
  Trollop::die "#{directory} is not a directory" unless File.directory?(directory)

  instance = self.new(settings, options, directory)
  begin
    instance.list_projects
  rescue OAuth2::Error => e
    say "Access to your 88 Miles may have been revoked. Please run <%= color('88miles setup', BOLD) %> again."
    return INVALID_OAUTH_TOKEN_EXIT_CODE
  rescue Faraday::Error::ConnectionFailed => e
    say "Couldn't connect to the 88 Miles server. Please try again later."
    return CONNECTION_ERROR_EXIT_CODE
  end

  return OK_EXIT_CODE
end

Public Instance Methods

list_projects() click to toggle source
# File lib/gigawatt/commands/init.rb, line 38
def list_projects
  companies = @cache.companies(true)

  selected = nil
  choose do |menu|
    menu.prompt = "Pick a project"
    @cache.projects.each do |project|
      menu.choice("#{companies[project["company_uuid"]]["name"]}: #{project["name"]}") { selected = project }
    end
  end

  ProjectFile.write(selected)
  say("<%= color('#{companies[selected["company_uuid"]]["name"]}: #{selected["name"]}', GREEN) %> selected. Run <%= color('88miles start', BOLD) %> to punch in")
end