Sha256: 9cbeee5ca95be977fb8f45492c2a0b7c5a67c0c175901b8765e68bbe1b86d133

Contents?: true

Size: 1.52 KB

Versions: 2

Compression:

Stored size: 1.52 KB

Contents

module PadSec
  module ConfigFile

    # Gets the config file path.
    #
    # Based on `ENV['PADSTONE']`, gets the path to the config file.
    #
    # @return [String]
    def self.path
      if ENV['PADSTONE'] == 'development'
        "results/.padstone/account"
      else
        "#{ENV['HOME']}/.padstone/account"
      end
    end

    # 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
    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

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pad_sec-0.2.0 lib/pad_sec/config_file.rb
pad_sec-0.1.0 lib/pad_sec/config_file.rb