Class: Paramsync::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/paramsync/cli.rb,
lib/paramsync/cli/pull_command.rb,
lib/paramsync/cli/push_command.rb,
lib/paramsync/cli/check_command.rb,
lib/paramsync/cli/config_command.rb,
lib/paramsync/cli/encrypt_command.rb,
lib/paramsync/cli/targets_command.rb

Defined Under Namespace

Classes: CheckCommand, ConfigCommand, EncryptCommand, PullCommand, PushCommand, TargetsCommand

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.cli_modeObject

Returns the value of attribute cli_mode.



15
16
17
# File 'lib/paramsync/cli.rb', line 15

def cli_mode
  @cli_mode
end

.commandObject

Returns the value of attribute command.



15
16
17
# File 'lib/paramsync/cli.rb', line 15

def command
  @command
end

.config_fileObject

Returns the value of attribute config_file.



15
16
17
# File 'lib/paramsync/cli.rb', line 15

def config_file
  @config_file
end

.extra_argsObject

Returns the value of attribute extra_args.



15
16
17
# File 'lib/paramsync/cli.rb', line 15

def extra_args
  @extra_args
end

.targetsObject

Returns the value of attribute targets.



15
16
17
# File 'lib/paramsync/cli.rb', line 15

def targets
  @targets
end

Class Method Details

.configureObject



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
# File 'lib/paramsync/cli.rb', line 82

def configure
  return if Paramsync.configured?

  begin
    Paramsync.configure(path: self.config_file, targets: self.targets)

  rescue Paramsync::ConfigFileNotFound
    if self.config_file.nil?
      STDERR.puts "paramsync: ERROR: No configuration file found"
    else
      STDERR.puts "paramsync: ERROR: Configuration file '#{self.config_file}' was not found"
    end
    exit 1

  rescue Paramsync::ConfigFileInvalid => e
    if self.config_file.nil?
      STDERR.puts "paramsync: ERROR: Configuration file is invalid:"
    else
      STDERR.puts "paramsync: ERROR: Configuration file '#{self.config_file}' is invalid:"
    end
    STDERR.puts "  #{e}"
    exit 1

  end

  if Paramsync.config.sync_targets.count < 1
    if self.targets.nil?
      STDERR.puts "paramsync: WARNING: No sync targets are defined"
    else
      STDERR.puts "paramsync: WARNING: No sync targets were found that matched the specified list"
    end
  end
end

.parse_args(args) ⇒ Object



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
# File 'lib/paramsync/cli.rb', line 17

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


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
# File 'lib/paramsync/cli.rb', line 51

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 parameter store
  pull         Pull changes from parameter store to filesystem
  encrypt      Encrypt a string for the file
  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

.runObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/paramsync/cli.rb', line 116

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 Paramsync::CLI::CheckCommand.run(self.extra_args)
    when 'push'       then Paramsync::CLI::PushCommand.run(self.extra_args)
    when 'pull'       then Paramsync::CLI::PullCommand.run(self.extra_args)
    when 'encrypt'    then Paramsync::CLI::EncryptCommand.run(self.extra_args)
    when 'config'     then Paramsync::CLI::ConfigCommand.run
    when 'targets'    then Paramsync::CLI::TargetsCommand.run
    when nil          then self.print_usage

    else
      STDERR.puts "paramsync: ERROR: unknown command '#{self.command}'"
      STDERR.puts
      self.print_usage
    end

  else
    STDERR.puts "paramsync: ERROR: unknown CLI mode '#{self.cli_mode}'"
    exit 1

  end
end