Class: Sem::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/sem/configuration.rb

Constant Summary collapse

CREDENTIALS_PATH =
File.expand_path("~/.sem/credentials").freeze
API_URL_PATH =
File.expand_path("~/.sem/api_url").freeze
DEFAULT_API_URL =
"https://api.semaphoreci.com".freeze

Class Method Summary collapse

Class Method Details

.api_urlObject



41
42
43
44
45
# File 'lib/sem/configuration.rb', line 41

def api_url
  return DEFAULT_API_URL unless File.file?(API_URL_PATH)

  File.read(API_URL_PATH).strip
end

.auth_tokenObject



35
36
37
38
39
# File 'lib/sem/configuration.rb', line 35

def auth_token
  raise Sem::Errors::Auth::NoCredentials unless File.file?(CREDENTIALS_PATH)

  File.read(CREDENTIALS_PATH).strip
end

.delete_auth_tokenObject



31
32
33
# File 'lib/sem/configuration.rb', line 31

def delete_auth_token
  FileUtils.rm_f(CREDENTIALS_PATH)
end

.export_auth_token(auth_token) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/sem/configuration.rb', line 23

def export_auth_token(auth_token)
  dirname = File.dirname(CREDENTIALS_PATH)
  FileUtils.mkdir_p(dirname)

  File.write(CREDENTIALS_PATH, auth_token)
  File.chmod(0o0600, CREDENTIALS_PATH)
end

.valid_auth_token?(auth_token) ⇒ Boolean



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/sem/configuration.rb', line 9

def valid_auth_token?(auth_token)
  client = SemaphoreClient.new(
    auth_token,
    :api_url => api_url,
    :verbose => (Sem.log_level == Sem::LOG_LEVEL_TRACE)
  )

  client.orgs.list!

  true
rescue SemaphoreClient::Exceptions::Base
  false
end