Class: Constancy::CLI
- Inherits:
-
Object
- Object
- Constancy::CLI
- Defined in:
- lib/constancy/cli.rb,
lib/constancy/cli/pull_command.rb,
lib/constancy/cli/push_command.rb,
lib/constancy/cli/check_command.rb,
lib/constancy/cli/config_command.rb,
lib/constancy/cli/targets_command.rb
Defined Under Namespace
Classes: CheckCommand, ConfigCommand, PullCommand, PushCommand, TargetsCommand
Class Attribute Summary collapse
-
.cli_mode ⇒ Object
Returns the value of attribute cli_mode.
-
.command ⇒ Object
Returns the value of attribute command.
-
.config_file ⇒ Object
Returns the value of attribute config_file.
-
.extra_args ⇒ Object
Returns the value of attribute extra_args.
-
.targets ⇒ Object
Returns the value of attribute targets.
Class Method Summary collapse
- .configure(call_external_apis: true) ⇒ Object
- .parse_args(args) ⇒ Object
- .print_usage ⇒ Object
- .run ⇒ Object
Class Attribute Details
.cli_mode ⇒ Object
Returns the value of attribute cli_mode.
14 15 16 |
# File 'lib/constancy/cli.rb', line 14 def cli_mode @cli_mode end |
.command ⇒ Object
Returns the value of attribute command.
14 15 16 |
# File 'lib/constancy/cli.rb', line 14 def command @command end |
.config_file ⇒ Object
Returns the value of attribute config_file.
14 15 16 |
# File 'lib/constancy/cli.rb', line 14 def config_file @config_file end |
.extra_args ⇒ Object
Returns the value of attribute extra_args.
14 15 16 |
# File 'lib/constancy/cli.rb', line 14 def extra_args @extra_args end |
.targets ⇒ Object
Returns the value of attribute targets.
14 15 16 |
# File 'lib/constancy/cli.rb', line 14 def targets @targets end |
Class Method Details
.configure(call_external_apis: true) ⇒ Object
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/constancy/cli.rb', line 80 def configure(call_external_apis: true) return if Constancy.configured? begin Constancy.configure(path: self.config_file, targets: self.targets, call_external_apis: call_external_apis) rescue Constancy::ConfigFileNotFound if self.config_file.nil? STDERR.puts "constancy: ERROR: No configuration file found" else STDERR.puts "constancy: ERROR: Configuration file '#{self.config_file}' was not found" end exit 1 rescue Constancy::ConfigFileInvalid => e if self.config_file.nil? STDERR.puts "constancy: ERROR: Configuration file is invalid:" else STDERR.puts "constancy: ERROR: Configuration file '#{self.config_file}' is invalid:" end STDERR.puts " #{e}" exit 1 rescue Constancy::ConsulTokenRequired => e STDERR.puts "constancy: ERROR: No Consul token could be found: #{e}" exit 1 rescue Constancy::VaultConfigInvalid => e STDERR.puts "constancy: ERROR: Vault configuration invalid: #{e}" exit 1 end if Constancy.config.sync_targets.count < 1 if self.targets.nil? STDERR.puts "constancy: WARNING: No sync targets are defined" else STDERR.puts "constancy: WARNING: No sync targets were found that matched the specified list" end end end |
.parse_args(args) ⇒ Object
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 |
# File 'lib/constancy/cli.rb', line 16 def parse_args(args) self.print_usage if args.count < 1 self.command = nil self.config_file = nil self.extra_args = [] self.cli_mode = :command while arg = args.shift case arg when "--help" self.cli_mode = :help when "--config" self.config_file = args.shift when "--target" self.targets = (args.shift||'').split(",") when /^-/ # additional option, maybe for the command self.extra_args << arg else if self.command.nil? # if command is not set, this is probably the command self.command = arg else # otherwise, pass it thru to the child command self.extra_args << arg end end end end |
.print_usage ⇒ Object
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 |
# File 'lib/constancy/cli.rb', line 50 def print_usage STDERR.puts <<USAGE Usage: #{File.basename($0)} <command> [options] Commands: check Print a summary of changes to be made push Push changes from filesystem to Consul pull Pull changes from Consul to filesystem config Print a summary of the active configuration targets List target names General options: --help Print help for the given command --config <file> Use the specified config file --target <tgt> Only apply to the specified target name or names (comma-separated) Options for 'check' command: --pull Perform dry run in pull mode Options for 'pull' command: --yes Skip confirmation prompt Options for 'push' command: --yes Skip confirmation prompt USAGE exit 1 end |
.run ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/constancy/cli.rb', line 122 def run self.parse_args(ARGV) case self.cli_mode when :help # TODO: per-command help self.print_usage when :command case self.command when 'check' then Constancy::CLI::CheckCommand.run(self.extra_args) when 'push' then Constancy::CLI::PushCommand.run(self.extra_args) when 'pull' then Constancy::CLI::PullCommand.run(self.extra_args) when 'config' then Constancy::CLI::ConfigCommand.run when 'targets' then Constancy::CLI::TargetsCommand.run when nil then self.print_usage else STDERR.puts "constancy: ERROR: unknown command '#{self.command}'" STDERR.puts self.print_usage end else STDERR.puts "constancy: ERROR: unknown CLI mode '#{self.cli_mode}'" exit 1 end end |