Class: Chef::Knife::ConfigListProfiles

Inherits:
Chef::Knife show all
Defined in:
lib/chef/knife/config_list_profiles.rb

Constant Summary

Constants inherited from Chef::Knife

CHEF_ORGANIZATION_MANAGEMENT, OFFICIAL_PLUGINS, OPSCODE_HOSTED_CHEF_ACCESS_CONTROL

Instance Attribute Summary

Attributes inherited from Chef::Knife

#name_args, #ui

Instance Method Summary collapse

Methods inherited from Chef::Knife

#api_key, #apply_computed_config, category, chef_config_dir, #cli_keys, common_name, #config_file_settings, config_loader, #config_source, #configure_chef, #create_object, #delete_object, dependency_loaders, deps, #format_rest_error, guess_category, #humanize_exception, #humanize_http_exception, inherited, #initialize, list_commands, load_commands, load_config, load_deps, #maybe_setup_fips, #merge_configs, msg, #noauth_rest, #parse_options, reset_config_loader!, reset_subcommands!, #rest, run, #run_with_pretty_exceptions, #server_url, #show_usage, snake_case_name, subcommand_category, subcommand_class_from, subcommand_files, subcommand_loader, subcommands, subcommands_by_category, #test_mandatory_field, ui, unnamed?, use_separate_defaults?, #username

Methods included from Mixin::ConvertToClassName

#constantize, #convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #normalize_snake_case_name, #snake_case_basename

Constructor Details

This class inherits a constructor from Chef::Knife

Instance Method Details

#runObject



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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/chef/knife/config_list_profiles.rb', line 32

def run
  credentials_data = self.class.config_loader.parse_credentials_file
  if credentials_data.nil? || credentials_data.empty?
    # Should this just show the ambient knife.rb config as "default" instead?
    ui.fatal("No profiles found, #{self.class.config_loader.credentials_file_path} does not exist or is empty")
    exit 1
  end

  current_profile = self.class.config_loader.credentials_profile(config[:profile])
  profiles = credentials_data.keys.map do |profile|
    if config[:ignore_knife_rb]
      # Don't do any fancy loading nonsense, just the raw data.
      profile_data = credentials_data[profile]
      {
        profile: profile,
        active: profile == current_profile,
        client_name: profile_data["client_name"] || profile_data["node_name"],
        client_key: profile_data["client_key"],
        server_url: profile_data["chef_server_url"],
      }
    else
      # Fancy loading nonsense so we get what the actual config would be.
      # Note that this modifies the global config, after this, all bets are
      # off as to whats in the config.
      Chef::Config.reset
      wcl = Chef::WorkstationConfigLoader.new(nil, Chef::Log, profile: profile)
      wcl.load
      {
        profile: profile,
        active: profile == current_profile,
        client_name: Chef::Config[:node_name],
        client_key: Chef::Config[:client_key],
        server_url: Chef::Config[:chef_server_url],
      }
    end
  end

  # Try to reset the config.
  unless config[:ignore_knife_rb]
    Chef::Config.reset
    Chef::WorkstationConfigLoader.new(config[:config_file], Chef::Log, profile: config[:profile]).load
    apply_computed_config
  end

  if ui.interchange?
    # Machine-readable output.
    ui.output(profiles)
  else
    # Table output.
    ui.output(render_table(profiles))
  end
end