Class: Vanagon::CLI::List

Inherits:
Vanagon::CLI show all
Defined in:
lib/vanagon/cli/list.rb

Constant Summary collapse

DOCUMENTATION =
<<~DOCOPT.freeze
  Usage:
    list [options]

  Options:
    -h, --help                       Display help
    -c, --configdir DIRECTORY        Configuration directory [default: #{Dir.pwd}/configs]
    -d, --defaults                   Display the list of default platforms
    -l, --platforms                  Display a list of platforms
    -r, --projects                   Display a list of projects
    -s, --use-spaces                 Displays the list as space separated
DOCOPT

Instance Method Summary collapse

Methods inherited from Vanagon::CLI

#options_validate

Instance Method Details

#check_directories(options) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/vanagon/cli/list.rb', line 61

def check_directories(options)
  platforms_directory = File.join(options[:configdir], 'platforms')
  projects_directory = File.join(options[:configdir], 'projects')

  unless Dir.exist?(platforms_directory)
    VanagonLogger.error "Platforms directory \"#{platforms_directory}\" does not exist."
    exit 1
  end

  unless Dir.exist?(projects_directory)
    VanagonLogger.error "Projectss directory \"#{projects_directory}\" does not exist."
    exit 1
  end
end

#options_translate(docopt_options) ⇒ Object



82
83
84
85
86
87
88
89
90
91
# File 'lib/vanagon/cli/list.rb', line 82

def options_translate(docopt_options)
  translations = {
    '--configdir' => :configdir,
    '--defaults' => :defaults,
    '--platforms' => :platforms,
    '--projects' => :projects,
    '--use-spaces' => :use_spaces,
  }
  return docopt_options.map { |k, v| [translations[k], v] }.to_h
end

#output(list, use_spaces) ⇒ Object



27
28
29
30
# File 'lib/vanagon/cli/list.rb', line 27

def output(list, use_spaces)
  return list.join(' ') if use_spaces
  return list
end

#parse(argv) ⇒ Object



20
21
22
23
24
25
# File 'lib/vanagon/cli/list.rb', line 20

def parse(argv)
  Docopt.docopt(DOCUMENTATION, { argv: argv })
rescue Docopt::Exit => e
  VanagonLogger.error e.message
  exit 1
end

#run(options) ⇒ Object

rubocop:disable Metrics/AbcSize



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
# File 'lib/vanagon/cli/list.rb', line 32

def run(options) # rubocop:disable Metrics/AbcSize
  check_directories(options)

  default_list = topic_list(File.dirname(__FILE__), '..', 'platform', 'defaults')
  platform_list = topic_list(options[:configdir], 'platforms')
  project_list = topic_list(options[:configdir], 'projects')

  if options[:defaults]
    puts "- Defaults", output(default_list, options[:use_spaces])
    return
  end

  if options[:projects] == options[:platforms]
    puts "- Projects", output(project_list, options[:use_spaces]), "\n",
         "- Platforms", output(platform_list, options[:use_spaces])
    return
  end

  if options[:projects]
    puts "- Projects", output(project_list, options[:use_spaces])
    return
  end

  if options[:platforms]
    puts "- Platforms", output(platform_list, options[:use_spaces])
    return
  end
end

#topic_list(*topic_path_items) ⇒ Object



76
77
78
79
80
# File 'lib/vanagon/cli/list.rb', line 76

def topic_list(*topic_path_items)
  Dir.children(File.join(topic_path_items)).map do |t|
    File.basename(t, File.extname(t))
  end.sort
end