Class: GetToWork::Command::Bootstrap

Inherits:
GetToWork::Command show all
Defined in:
lib/get_to_work/command/bootstrap.rb

Instance Method Summary collapse

Methods inherited from GetToWork::Command

#config_file, #harvest_service, #initialize, #last_story, #last_timer, run

Methods included from Menu

#menu_ask

Constructor Details

This class inherits a constructor from GetToWork::Command

Instance Method Details

#auth_with_service(service:, username:, password:, subdomain: nil) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/get_to_work/command/bootstrap.rb', line 71

def auth_with_service(service:, username:, password:, subdomain: nil)
  shell.say "\n\nAuthenticating with #{service.display_name}...", :magenta

  begin
    service.authenticate(username: username, password: password, subdomain: subdomain)
  rescue Service::UnauthorizedError
    shell.say "Could not authenticate with #{service.display_name}", :red
    exit(1)
  end

  service.update_keychain(account: username)
end

#check_for_config_fileObject



49
50
51
52
53
54
55
# File 'lib/get_to_work/command/bootstrap.rb', line 49

def check_for_config_file
  if config_file
    unless shell.yes?("Would you like to overwrite your existing #{GetToWork::ConfigFile.filename} file? [y/N]", :green)
      exit(0)
    end
  end
end

#prompt_for_login(service) ⇒ Object



57
58
59
60
61
62
# File 'lib/get_to_work/command/bootstrap.rb', line 57

def (service)
  username = shell.ask "#{service.display_name} Username:", :green
  password = shell.ask "#{service.display_name} Password:", :green, echo: false

  [username, password]
end

#prompt_for_subdomain_and_login(service) ⇒ Object



64
65
66
67
68
69
# File 'lib/get_to_work/command/bootstrap.rb', line 64

def (service)
  subdomain = shell.ask "#{service.display_name} Subdomain:", :green
  username, password = (service)

  [subdomain, username, password]
end

#prompt_select_project(service) ⇒ Object



84
85
86
87
88
89
90
91
92
93
# File 'lib/get_to_work/command/bootstrap.rb', line 84

def prompt_select_project(service)
  project_options = GetToWork::MenuPresenter.with_collection(service.projects)

  menu_ask(
    "\nSelect a #{service.display_name} project:",
    project_options,
    :green,
    limited_to: project_options.menu_limit
  )
end

#prompt_select_tasks(service, project) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/get_to_work/command/bootstrap.rb', line 95

def prompt_select_tasks(service, project)
  tasks = project["tasks"].sort do |x, y|
    x[:name] <=> y[:name]
  end

  project_options = GetToWork::MenuPresenter.with_collection(tasks)

  menu_ask(
    "\nSelect a #{service.display_name} Task:",
    project_options,
    :green,
    limited_to: project_options.menu_limit
  )
end

#runObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/get_to_work/command/bootstrap.rb', line 4

def run
  check_for_config_file

  pt = GetToWork::Service::PivotalTracker.new

  shell.say "\n\nStep #1 #{pt.display_name} Setup", :magenta
  shell.say "-----------------------------", :magenta

  if pt.keychain
    pt.authenticate_with_keychain
  else
    username, password = (pt)
    auth_with_service(service: pt, username: username, password: password)
  end

  project = prompt_select_project(pt)
  pt.save_config("project_id" => project.id)

  GetToWork::ConfigFile.save

  shell.say "\n\nStep #2 #{harvest_service.display_name} Setup", :magenta
  shell.say "-----------------------------", :magenta

  unless harvest_service.authenticate_with_keychain
    subdomain, username, password = (harvest_service)
    auth_with_service(
      service: harvest_service,
      username: username,
      password: password,
      subdomain: subdomain
    )
  end

  harvest_project = prompt_select_project(harvest_service)
  harvest_task = prompt_select_tasks(harvest_service, harvest_project)

  harvest_service.save_config(
    "project_id" => harvest_project.id,
    "task_id" => harvest_task["id"],
    "subdomain" => harvest_service.subdomain
  )

  GetToWork::ConfigFile.save
end