Module: PadSec::ConfigFile

Defined in:
lib/pad_sec/config_file.rb

Class Method Summary collapse

Class Method Details

.pathString

Gets the config file path.

Based on ‘ENV`, gets the path to the config file.

Returns:

  • (String)


9
10
11
12
13
14
15
# File 'lib/pad_sec/config_file.rb', line 9

def self.path
  if ENV['PADSTONE'] == 'development'
    "results/.padstone/account"
  else
    "#{ENV['HOME']}/.padstone/account"
  end
end

.write_config(username: nil, pwd: nil, token: nil) ⇒ Void

Note:

A username and pwd are optional if a config file already exists.

Creates or updates the config file.

Parameters:

  • username (String) (defaults to: nil)
  • pwd (String) (defaults to: nil)
  • token (String) (defaults to: nil)

Returns:

  • (Void)

    nothing



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/pad_sec/config_file.rb', line 25

def self.write_config(username: nil, pwd: nil, token: nil)
  hash = {}
  config_file = PadSec::ConfigFile::path

  # Check if there's already a config
  if PadUtils.file_exist? config_file
    # Yes. Get the username and pwd from it if not given in the params
    config = PadUtils.json_file_to_hash(config_file)

    if username.nil? && !config[:username].nil?
      hash[:username] = config[:username]
    else
      hash[:username] = username
    end

    if pwd.nil? && !config[:pwd].nil?
      hash[:pwd] = config[:pwd]
    else
      hash[:pwd] = pwd
    end
  else
    # No. Get the values from the params.
    hash = {
      username: username,
      pwd: pwd
    }
  end

  # The token is always coming from the params.
  hash[:token] = token

  # Creates or overwrites the file
  PadUtils.hash_to_json_file(config_file, hash)
end