Class: Jatai::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/jatai/config.rb

Constant Summary collapse

CONFIG_DIR =
File.expand_path("~/.jatai").freeze
CREDENTIALS_FILE =
File.join(CONFIG_DIR, "credentials.json").freeze
PROJECT_FILE =
".jatai.yml".freeze
LEGACY_CONFIG_DIR =

Compat com a CLI anterior (brasa) — fallback leve (beta fechado).

File.expand_path("~/.brasa").freeze
LEGACY_CREDENTIALS_FILE =
File.join(LEGACY_CONFIG_DIR, "credentials.json").freeze
LEGACY_PROJECT_FILE =
".brasa.yml".freeze
DEFAULT_API_URL =
"https://api.usebrasa.com.br".freeze

Class Method Summary collapse

Class Method Details

.api_urlObject



18
19
20
# File 'lib/jatai/config.rb', line 18

def api_url
  ENV.fetch("JATAI_API_URL") { ENV.fetch("BRASA_API_URL", DEFAULT_API_URL) }
end

.clear_tokenObject



33
34
35
36
# File 'lib/jatai/config.rb', line 33

def clear_token
  File.delete(CREDENTIALS_FILE) if File.exist?(CREDENTIALS_FILE)
  File.delete(LEGACY_CREDENTIALS_FILE) if File.exist?(LEGACY_CREDENTIALS_FILE)
end

.logged_in?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/jatai/config.rb', line 38

def logged_in?
  !token.nil? && !token.empty?
end

.project_file_pathObject

Caminho do arquivo de projeto no diretório atual, com fallback ao legado.



43
44
45
46
47
48
49
# File 'lib/jatai/config.rb', line 43

def project_file_path
  new_path = File.join(Dir.pwd, PROJECT_FILE)
  return new_path if File.exist?(new_path)

  legacy_path = File.join(Dir.pwd, LEGACY_PROJECT_FILE)
  File.exist?(legacy_path) ? legacy_path : new_path
end

.save_token(token) ⇒ Object



26
27
28
29
30
31
# File 'lib/jatai/config.rb', line 26

def save_token(token)
  FileUtils.mkdir_p(CONFIG_DIR)
  File.open(CREDENTIALS_FILE, "w", 0600) do |f|
    f.write(JSON.generate({ token: token }))
  end
end

.tokenObject



22
23
24
# File 'lib/jatai/config.rb', line 22

def token
  credentials["token"]
end