module PadSec::ConfigFile

Public Class Methods

path() click to toggle source

Gets the config file path.

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

@return [String]

# 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) click to toggle source

Creates or updates the config file.

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

@param username [String] @param pwd [String] @param token [String] @return [Void] nothing

# 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