module Comply::CLI::Helpers::Program

Constants

PROGRAM_ENV_VAR

Public Instance Methods

accessible_programs() click to toggle source
# 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_hash() click to toggle source
# File lib/comply/cli/helpers/program.rb, line 55
def current_program_id_hash
  JSON.parse(File.read(program_id_file))
rescue
  {}
end
default_program() click to toggle source
# 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_id() click to toggle source
# 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) click to toggle source
# File lib/comply/cli/helpers/program.rb, line 26
def pretty_print_program(program)
  "#{program.organization.name} (#{program.id})"
end
program_id_file() click to toggle source
# 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) click to toggle source
# 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_program() click to toggle source
# 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