Class: Chef::Knife::List

Inherits:
ChefFS::Knife show all
Defined in:
lib/chef/knife/list.rb

Instance Attribute Summary

Attributes inherited from Chef::Knife

#name_args, #ui

Instance Method Summary collapse

Methods inherited from ChefFS::Knife

#base_path, #chef_fs, #chef_repo, common_options, #format_path, #local_fs, #pattern_args, #pattern_args_from

Methods inherited from Chef::Knife

#api_key, #apply_computed_config, category, common_name, #config_file_settings, #configure_chef, #create_object, #delete_object, deps, #format_rest_error, guess_category, #highlight_config_error, #humanize_exception, #humanize_http_exception, inherited, #initialize, list_commands, load_commands, load_deps, #locate_config_file, #merge_configs, msg, #noauth_rest, #parse_options, #read_config_file, reset_subcommands!, #rest, run, #run_with_pretty_exceptions, #server_url, #show_usage, snake_case_name, subcommand_category, subcommand_class_from, subcommand_loader, subcommands, subcommands_by_category, ui, unnamed?, use_separate_defaults?, #username

Methods included from Mixin::ConvertToClassName

#convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #snake_case_basename

Methods included from Mixin::PathSanity

#enforce_path_sanity

Constructor Details

This class inherits a constructor from Chef::Knife

Instance Method Details

#add_dir_result(result) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/chef/knife/list.rb', line 54

def add_dir_result(result)
  begin
    children = result.children.sort_by { |child| child.name }
  rescue Chef::ChefFS::FileSystem::NotFoundError
    STDERR.puts "#{format_path(result.path)}: No such file or directory"
    return []
  end

  result = [ [ result, children ] ]
  if config[:recursive]
    children.each do |child|
      if child.dir?
        result += add_dir_result(child)
      end
    end
  end
  result
end

#list_dirs_recursive(children) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/chef/knife/list.rb', line 73

def list_dirs_recursive(children)
  results = children.select { |child| child.dir? }.to_a
  results.each do |child|
    results += list_dirs_recursive(child.children)
  end
  results
end


81
82
83
# File 'lib/chef/knife/list.rb', line 81

def print_result_paths(results, indent = "")
  print_results(results.map { |result| format_path(result.path) }, indent)
end


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/chef/knife/list.rb', line 85

def print_results(results, indent)
  return if results.length == 0

  print_space = results.map { |result| result.length }.max + 2
  # TODO: tput cols is not cross platform
  columns = $stdout.isatty ? Integer(`tput cols`) : 0
  current_column = 0
  results.each do |result|
    if current_column != 0 && current_column + print_space > columns
      puts ""
      current_column = 0
    end
    if current_column == 0
      print indent
      current_column += indent.length
    end
    print result + (' ' * (print_space - result.length))
    current_column += print_space
  end
  puts ""
end

#runObject



20
21
22
23
24
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
# File 'lib/chef/knife/list.rb', line 20

def run
  patterns = name_args.length == 0 ? [""] : name_args

  # Get the matches (recursively)
  results = []
  dir_results = []
  pattern_args_from(patterns).each do |pattern|
    Chef::ChefFS::FileSystem.list(chef_fs, pattern) do |result|
      if result.dir? && !config[:bare_directories]
        dir_results += add_dir_result(result)
      elsif result.exists?
        results << result
      elsif pattern.exact_path
        STDERR.puts "#{format_path(result.path)}: No such file or directory"
      end
    end
  end

  results = results.sort_by { |result| result.path }
  dir_results = dir_results.sort_by { |result| result[0].path }

  if results.length == 0 && dir_results.length == 1
    results = dir_results[0][1]
    dir_results = []
  end

  print_result_paths results
  dir_results.each do |result, children|
    puts ""
    puts "#{format_path(result.path)}:"
    print_results(children.map { |result| result.name }.sort, "")
  end
end