<commit_before>require 'fileutils'
require 'yaml'
require 'json'
require 'io/console'
require 'rest_client'
require 'morpheus/logging'
require 'morpheus/cli/mixins/print_helper'

module Morpheus
  module Cli
    class Credentials
      include Morpheus::Cli::PrintHelper

      @@appliance_credentials_map = nil

      def initialize(appliance_name, appliance_url)
        @appliance_name = appliance_name ? appliance_name.to_sym : nil
        @appliance_url = appliance_url
      end
      
      def request_credentials(opts = {})
        #puts "request_credentials(#{opts})"
        username = nil
        password = <PASSWORD>
        access_token = nil
        skip_save = false
        
        if opts[:remote_token]
          begin
            whoami_interface = Morpheus::WhoamiInterface.new(opts[:remote_token], nil, nil, @appliance_url)
            whoami_response = whoami_interface.get()
            if opts[:json]
              print JSON.pretty_generate(whoami_response)
              print reset, "\n"
            end
            access_token = opts[:remote_token]
            username = whoami_response['user']['username']
            unless skip_save
              save_credentials(@appliance_name, access_token)
            end
          rescue ::RestClient::Exception => e
            #raise e
            if (e.response && e.response.code == 400)
              print_red_alert "Token not valid."
              if opts[:json]
                whoami_response = JSON.parse(e.response.to_s)
                print JSON.pretty_generate(whoami_response)
                print reset, "\n"
              end
            else
              #print_rest_exception(e, opts)
              print_red_alert "Token not valid."
            end
            access_token = nil
          end
        else
        
          if !opts[:remote_username].nil?
            username = opts[:remote_username]
            password = opts[:remote_password]
            skip_save = opts[:remote_url] ? true : false
          else
            access_token = load_saved_credentials
          end<commit_msg>Fix whoami -j not printing errors<commit_after>require 'fileutils'
require 'yaml'
require 'json'
require 'io/console'
require 'rest_client'
require 'morpheus/logging'
require 'morpheus/cli/mixins/print_helper'

module Morpheus
  module Cli
    class Credentials
      include Morpheus::Cli::PrintHelper

      @@appliance_credentials_map = nil

      def initialize(appliance_name, appliance_url)
        @appliance_name = appliance_name ? appliance_name.to_sym : nil
        @appliance_url = appliance_url
      end
      
      def request_credentials(opts = {})
        #puts "request_credentials(#{opts})"
        username = nil
        password = nil
        access_token = nil
        skip_save = false
        
        if opts[:remote_token]
          begin
            whoami_interface = Morpheus::WhoamiInterface.new(opts[:remote_token], nil, nil, @appliance_url)
            whoami_response = whoami_interface.get()
            if opts[:json]
              print JSON.pretty_generate(whoami_response)
              print reset, "\n"
            end
            access_token = opts[:remote_token]
            username = whoami_response['user']['username']
            unless skip_save
              save_credentials(@appliance_name, access_token)
            end
          rescue ::RestClient::Exception => e
            #raise e
            print_red_alert "Token not valid."
            if opts[:debug] || opts[:debug]
              print_rest_exception(e, opts)
            end
            access_token = nil
          end
        else
        
          if !opts[:remote_username].nil?
            username = opts[:remote_username]
            password = opts[:remote_password]
            skip_save = opts[:remote_url] ? true : false
          else
            access_token = load_saved_credentials
          end
{'fraction_non_alphanumeric': 0.08059168579444019, 'fraction_numerical': 0.0007651109410864575, 'mean_word_length': 1.9444444444444444, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3722150', 'n_tokens_mistral': 1091, 'n_tokens_neox': 1043, 'n_words': 232}