Class: Puffy::Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/puffy/cli.rb

Overview

Command-line processing

Instance Method Summary collapse

Constructor Details

#initializeCli

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 <<~DESCRIPTION
      Generate firewall rules for multiple nodes from a single network
      specification file.
    DESCRIPTION

    flag :h, :help, 'Show help for this command' do |_value, cmd|
      puts cmd.help
      exit 0
    end

    run do |_opts, _args, cmd|
      puts cmd.help
      exit 0
    end
  end

  @main.define_command do
    name    'generate'
    usage   'generate [options] <network> <hostname>'
    summary 'Generate the firewall configuration for a node'

    description <<~DESCRIPTION
      Generate the firewall configuration for the node "hostname" for which
      the configuration is described in the "network" specification file.
    DESCRIPTION

    required :f, :formatter, 'The formatter to use', default: 'Pf'

    param('network')
    param('hostname')

    run do |opts, args|
      parser = cli.load_network(args[:network])
      rules = parser.ruleset_for(args[:hostname])
      policy = parser.policy_for(args[:hostname])

      formatter = Object.const_get("Puffy::Formatters::#{opts[:formatter]}::Ruleset").new
      puts formatter.emit_ruleset(rules, policy)
    end
  end

  puppet = @main.define_command do
    name    'puppet'
    usage   'puppet [options] <subcommand>'
    summary 'Run puppet actions'

    description <<~DESCRIPTION
      Manage a directory of firewall configurations files suitable for Puppet.
    DESCRIPTION

    required :o, :output, 'Base directory for network firewall rules', default: '.'

    run do |_opts, _args, cmd|
      puts cmd.help
      exit 0
    end
  end

  puppet.define_command do
    name    'diff'
    usage   'diff [options] <network>'
    summary 'Show differences between network specification and firewall rules'

    description <<~DESCRIPTION
      Show the changes that would be introduced by running `puffy puppet
      generate` with the current "network" specification file.
    DESCRIPTION

    param('network')

    run do |opts, args|
      parser = cli.load_network(args[:network])
      puppet = Puffy::Puppet.new(opts[:output], parser)
      puppet.diff
    end
  end

  puppet.define_command do
    name    'generate'
    usage   'generate [options] <network>'
    summary 'Generate network firewall rules according to network specification'

    description <<~DESCRIPTION
      Generate a tree of configuration files suitable for all supported
      firewalls for all the nodes described in the "network" specification
      file.
    DESCRIPTION

    param('network')

    run do |opts, args|
      parser = cli.load_network(args[:network])
      puppet = Puffy::Puppet.new(opts[:output], parser)
      puppet.save
    end
  end

  help_command = Cri::Command.new_basic_help.modify do
    summary 'Show help for a command'
  end

  @main.add_command(help_command)
end

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