-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement puppet-metadata with various subcommands
This implements a puppet-metadata executable which now only implements the eol command to show which operating systems in metadata are end of life.
- Loading branch information
Showing
2 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#!/usr/bin/env ruby | ||
require 'optparse' | ||
require 'json' | ||
require 'puppet_metadata' | ||
|
||
class Command | ||
def initialize(arguments); end | ||
|
||
def metadata | ||
@metadata ||= begin | ||
@filename = 'metadata.json' if @filename.nil? || @filename.empty? | ||
|
||
PuppetMetadata.read(@filename) | ||
end | ||
rescue StandardError => e | ||
warn "Failed to read #{filename}: #{e}" | ||
exit 2 | ||
end | ||
|
||
def self.commands | ||
Command.subclasses.to_h { |subclass| [subclass.command, subclass] } | ||
end | ||
|
||
def self.command | ||
name.gsub(/Command$/, '') | ||
.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') | ||
.gsub(/([a-z\d])([A-Z])/, '\1_\2') | ||
.downcase | ||
end | ||
|
||
def self.parser | ||
raise NotImplementedError | ||
end | ||
end | ||
|
||
class EolCommand < Command | ||
def initialize(arguments) | ||
super | ||
|
||
@filename = arguments.shift | ||
|
||
# TODO: expose --at $DATE | ||
eol = metadata.eol_operatingsystems | ||
|
||
if eol.any? | ||
width = eol.each_key.map(&:length).max | ||
|
||
puts 'Found EOL operating systems' | ||
eol.each do |os, versions| | ||
puts "#{os.ljust(width)} #{versions.join(', ')}" | ||
end | ||
else | ||
puts 'All operating systems up to date' | ||
end | ||
end | ||
end | ||
|
||
def main | ||
command = ARGV[0] | ||
command_class = Command.commands[command] | ||
unless command_class | ||
warn "Command '#{command}' not understood." | ||
exit 2 | ||
end | ||
|
||
command_class.new(ARGV[1..]) | ||
end | ||
|
||
main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters