summaryrefslogtreecommitdiff
path: root/chef/lib/chef/knife/list.rb
blob: 30fcb5fa3510a35759a4db751ca5eb1698010616 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
require 'chef/chef_fs/knife'
require 'chef/chef_fs/file_system'

class Chef
  class Knife
    class List < Chef::ChefFS::Knife
      banner "knife list [-dR] [PATTERN1 ... PATTERNn]"

      common_options

      option :recursive,
        :short => '-R',
        :boolean => true,
        :description => "List directories recursively."
      option :bare_directories,
        :short => '-d',
        :boolean => true,
        :description => "When directories match the pattern, do not show the directories' children."

      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

      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

      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

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

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