Class: RubyNative::CLI::Credentials

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_native/cli/credentials.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.pathObject

Lazy: Dir.home raises in HOME-less containers, and that must not kill commands that only use RUBY_NATIVE_TOKEN.



13
14
15
# File 'lib/ruby_native/cli/credentials.rb', line 13

def self.path
  @path ||= File.join(Dir.home, ".ruby_native", "credentials")
end

Class Method Details

.clearObject



41
42
43
# File 'lib/ruby_native/cli/credentials.rb', line 41

def self.clear
  File.delete(path) if File.exist?(path)
end

.env_tokenObject

A misspelled CI secret interpolates to "", which must not shadow the credentials file or ruby_native login can never fix auth.



23
24
25
26
# File 'lib/ruby_native/cli/credentials.rb', line 23

def self.env_token
  value = ENV["RUBY_NATIVE_TOKEN"].to_s.strip
  value unless value.empty?
end

.file_tokenObject



28
29
30
31
32
33
# File 'lib/ruby_native/cli/credentials.rb', line 28

def self.file_token
  return unless File.exist?(path)
  JSON.parse(File.read(path))["token"]
rescue JSON::ParserError, ArgumentError
  nil
end

.save(token) ⇒ Object



35
36
37
38
39
# File 'lib/ruby_native/cli/credentials.rb', line 35

def self.save(token)
  FileUtils.mkdir_p(File.dirname(path))
  File.write(path, JSON.generate(token: token))
  File.chmod(0600, path)
end

.tokenObject



17
18
19
# File 'lib/ruby_native/cli/credentials.rb', line 17

def self.token
  env_token || file_token
end