Class: Puffy::Cli
- Inherits:
-
Object
- Object
- Puffy::Cli
- Defined in:
- lib/puffy/cli.rb
Overview
Command-line processing
Instance Method Summary collapse
- #execute(argv) ⇒ Object
-
#initialize ⇒ Cli
constructor
rubocop:disable Metrics/AbcSize, Metrics/MethodLength.
- #load_network(filename) ⇒ Object
Constructor Details
#initialize ⇒ Cli
rubocop:disable Metrics/AbcSize, Metrics/MethodLength
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 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/puffy/cli.rb', line 10 def initialize # rubocop:disable Metrics/AbcSize, Metrics/MethodLength cli = self @main = Cri::Command.define do name 'puffy' usage 'puffy [options] <command>' summary 'Network firewall rules made easy!' description " Generate firewall rules for multiple nodes from a single network\n specification file.\n DESCRIPTION\n\n flag :h, :help, 'Show help for this command' do |_value, cmd|\n puts cmd.help\n exit 0\n end\n\n run do |_opts, _args, cmd|\n puts cmd.help\n exit 0\n end\n end\n\n @main.define_command do\n name 'generate'\n usage 'generate [options] <network> <hostname>'\n summary 'Generate the firewall configuration for a node'\n\n description <<~DESCRIPTION\n Generate the firewall configuration for the node \"hostname\" for which\n the configuration is described in the \"network\" specification file.\n DESCRIPTION\n\n required :f, :formatter, 'The formatter to use', default: 'Pf'\n\n param('network')\n param('hostname')\n\n run do |opts, args|\n parser = cli.load_network(args[:network])\n rules = parser.ruleset_for(args[:hostname])\n policy = parser.policy_for(args[:hostname])\n\n formatter = Object.const_get(\"Puffy::Formatters::\#{opts[:formatter]}::Ruleset\").new\n puts formatter.emit_ruleset(rules, policy)\n end\n end\n\n puppet = @main.define_command do\n name 'puppet'\n usage 'puppet [options] <subcommand>'\n summary 'Run puppet actions'\n\n description <<~DESCRIPTION\n Manage a directory of firewall configurations files suitable for Puppet.\n DESCRIPTION\n\n required :o, :output, 'Base directory for network firewall rules', default: '.'\n\n run do |_opts, _args, cmd|\n puts cmd.help\n exit 0\n end\n end\n\n puppet.define_command do\n name 'diff'\n usage 'diff [options] <network>'\n summary 'Show differences between network specification and firewall rules'\n\n description <<~DESCRIPTION\n Show the changes that would be introduced by running `puffy puppet\n generate` with the current \"network\" specification file.\n DESCRIPTION\n\n param('network')\n\n run do |opts, args|\n parser = cli.load_network(args[:network])\n puppet = Puffy::Puppet.new(opts[:output], parser)\n puppet.diff\n end\n end\n\n puppet.define_command do\n name 'generate'\n usage 'generate [options] <network>'\n summary 'Generate network firewall rules according to network specification'\n\n description <<~DESCRIPTION\n Generate a tree of configuration files suitable for all supported\n firewalls for all the nodes described in the \"network\" specification\n file.\n DESCRIPTION\n\n param('network')\n\n run do |opts, args|\n parser = cli.load_network(args[:network])\n puppet = Puffy::Puppet.new(opts[:output], parser)\n puppet.save\n end\n end\n\n help_command = Cri::Command.new_basic_help.modify do\n summary 'Show help for a command'\n end\n\n @main.add_command(help_command)\nend\n" |
Instance Method Details
#execute(argv) ⇒ Object
128 129 130 |
# File 'lib/puffy/cli.rb', line 128 def execute(argv) @main.run(argv) end |
#load_network(filename) ⇒ Object
122 123 124 125 126 |
# File 'lib/puffy/cli.rb', line 122 def load_network(filename) parser = Puffy::Parser.new parser.parse_file(filename) parser end |