Module: Comply::CLI::Helpers::Program

Includes:
Token
Included in:
Agent
Defined in:
lib/comply/cli/helpers/program.rb

Constant Summary collapse

PROGRAM_ENV_VAR =
'APTIBLE_PROGRAM_ID'.freeze

Constants included from Token

Token::TOKEN_ENV_VAR

Instance Method Summary collapse

Methods included from Token

#current_token_hash, #current_user_email, #fetch_token, #save_token, #token_file

Instance Method Details

#accessible_programsObject



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/comply/cli/helpers/program.rb', line 65

def accessible_programs
  # If a user is a member of a role in ACCOUNT_MANAGEMENT_ROLE_IDS
  # in Comply, they have read access to ALL programs. As a result,
  # when offering programs for customers to access, we select just
  # those which actually belong to their organization(s).

  programs = Aptible::Comply::Program.all(token: fetch_token)
  orgs = Aptible::Auth::Organization.all(token: fetch_token)

  programs.select do |program|
    orgs.map(&:href).include?(program.organization_url)
  end
end

#current_program_id_hashObject



55
56
57
58
59
# File 'lib/comply/cli/helpers/program.rb', line 55

def current_program_id_hash
  JSON.parse(File.read(program_id_file))
rescue
  {}
end

#default_programObject



14
15
16
17
18
19
# File 'lib/comply/cli/helpers/program.rb', line 14

def default_program
  return nil unless (id = fetch_program_id)
  @default_program ||= Aptible::Comply::Program.find(
    id, token: fetch_token
  )
end

#fetch_program_idObject

Raises:

  • (Thor::Error)


30
31
32
33
34
35
36
37
# File 'lib/comply/cli/helpers/program.rb', line 30

def fetch_program_id
  @program_id ||=
    ENV[PROGRAM_ENV_VAR] ||
    current_program_id_hash[Aptible::Comply.configuration.root_url]
  return @program_id if @program_id
  raise Thor::Error, 'Could not read program: please run comply ' \
                     "programs:select or set #{PROGRAM_ENV_VAR}"
end

#pretty_print_program(program) ⇒ Object



26
27
28
# File 'lib/comply/cli/helpers/program.rb', line 26

def pretty_print_program(program)
  "#{program.organization.name} (#{program.id})"
end

#program_id_fileObject



61
62
63
# File 'lib/comply/cli/helpers/program.rb', line 61

def program_id_file
  File.join ENV['HOME'], '.aptible', 'programs.json'
end

#save_program_id(program_id) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/comply/cli/helpers/program.rb', line 39

def save_program_id(program_id)
  hash = current_program_id_hash.merge(
    Aptible::Comply.configuration.root_url => program_id
  )

  FileUtils.mkdir_p(File.dirname(program_id_file))

  File.open(program_id_file, 'w', 0o600) do |file|
    file.puts hash.to_json
  end
rescue StandardError => e
  m = "Could not write program to #{program_id_file}: #{e}. " \
      'Check filesystem permissions.'
  raise Thor::Error, m
end

#set_default_programObject



21
22
23
24
# File 'lib/comply/cli/helpers/program.rb', line 21

def set_default_program
  default_program = accessible_programs.first
  save_program_id(default_program.id) if default_program
end