Module: Xolo::Server::CommandLine

Included in:
Xolo::Server
Defined in:
lib/xolo/server/command_line.rb

Overview

Module for parsing and validating the xadm options from the commandline

Constant Summary collapse

CONFIG_CMD =

Constants

'config'
SUBCOMMANDS =
[CONFIG_CMD].freeze
CLI_OPTIONS =
{
  production: {
    label: 'Production',
    cli: :p,
    walkthru: false,
    desc: <<~ENDDESC
      Run xoloserver in production mode.
      This sets various server settings to production mode, including setting the log-level to 'info' at start-time, unless  -d is also given.

      By default the server starts in development mode, and the log level is 'debug'
    ENDDESC
  },

  debug: {
    label: 'Debug',
    cli: :d,
    walkthru: false,
    desc: <<~ENDDESC
      Run xoloserver in debug mode. This sets the log-level to 'debug' at start-time in production mode.
    ENDDESC
  }
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(extender) ⇒ Object

when this module is extended



24
25
26
# File 'lib/xolo/server/command_line.rb', line 24

def self.extended(extender)
  Xolo.verbose_extend extender, self
end

.included(includer) ⇒ Object

when this module is included



19
20
21
# File 'lib/xolo/server/command_line.rb', line 19

def self.included(includer)
  Xolo.verbose_include includer, self
end

Instance Method Details

#cli_optsObject

An OStruct to hold the CLI options



66
67
68
# File 'lib/xolo/server/command_line.rb', line 66

def cli_opts
  @cli_opts ||= {}
end

#config_optsObject

An OStruct to hold the config subcommand options



72
73
74
# File 'lib/xolo/server/command_line.rb', line 72

def config_opts
  @config_opts ||= {}
end

#parse_cliObject

Use optimist to parse ARGV.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/xolo/server/command_line.rb', line 78

def parse_cli
  # get the global options
  parse_global_opts
  return if ARGV.empty?

  # if there are subcommands, parse them
  subcommand = ARGV.shift
  case subcommand
  when CONFIG_CMD
    parse_config_opts
  else
    Optimist.die "Unknown subcommand: #{subcommand}"
  end
end

#parse_config_optsObject

Parse the config subcommand options



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/xolo/server/command_line.rb', line 127

def parse_config_opts
  if ARGV.empty?
    config_opts[:show] = true
    return
  end

  @config_opts = Optimist.options do
    synopsis <<~ENDSYNOPSIS
      NAME
          #{Xolo::Server::EXECUTABLE_FILENAME} #{CONFIG_CMD} - Manage the server configuration file

      SYNOPSIS
          #{Xolo::Server::EXECUTABLE_FILENAME} #{CONFIG_CMD} [--show] [--expand] [config_key ...]

          #{Xolo::Server::EXECUTABLE_FILENAME} #{CONFIG_CMD} --set --config-key=value ...

          #{Xolo::Server::EXECUTABLE_FILENAME} #{CONFIG_CMD} --help


      DESCRIPTION
          The Xolo server configuration file is a YAML file located at
            #{Xolo::Server.config.conf_file}"

          It contains a Ruby Hash of configuration options, the keys are Symbols and the values are the
          configuration values. While the file can be edited directly, it is recommended to use the
          'config' subcommand to view and set the values.

          Some sensitive values may be stored in the configuration file as a command or file path from
          which to read the actual value. Config values starting with a pipe '|' are executed as a command
          (after removing the pipe) and the value to be used is read from the standard output of the
          command. Values that are file paths are read from the file at the path. If the stored value
          doesn't start with a pipe, and is not a valid file path, the value is used as is. Be wary of
          security issues, permissions, etc when working with these values.

          See '#{Xolo::Server::EXECUTABLE_FILENAME} #{CONFIG_CMD} --help' for details on which configuration
          keys are used this way.

        Showing configuration values:
          When used without --set, --show is implied. If no config keys are given, all keys are shown.
          Keys can be given as 'key_name' or 'key-name'.

          Values are shown as stored in the config file. With --expand, the actual value used by the server
          is shown, after reading from commands or files.

        Setting configuration values:
          To set configuration values, use --set followed by one or more config keys as options, e.g.
          --config-key=value. You must restart the server to apply changes.

          NOTE: As of this writing, there is very little validation of the values you set, nor any
          enforcement of required values. Be careful.

        Help:
          Using --help shows this help message, and descriptions of all available config_keys as options
          to --set.

        Private values:
          Config keys marked Private (see #{Xolo::Server::EXECUTABLE_FILENAME} #{CONFIG_CMD} --help) are
          not shown when the config values are displayed to users of xadm, instead they see '#{Xolo::Server::Configuration::PRIVATE}'
    ENDSYNOPSIS

    # add a blank line between each of the cli options in the help output
    # NOTE: chrisl added this to the optimist.rb included in this project.
    insert_blanks

    opt :show, 'Show configuration values', short: :none
    opt :expand, 'Show expanded configuration values', short: :none
    opt :set, 'Set configuration values', short: :none

    Xolo::Server::Configuration::KEYS.each do |key, deets|
      # puts "defining: #{key} "

      moinfo = deets[:required] ? 'Required if not already set ' : ''
      moinfo = "#{moinfo}[Private]" if deets[:private]
      moinfo = "#{moinfo.strip}\n" unless moinfo.empty?

      desc = "#{moinfo}#{deets[:desc]}"
      opt key, desc, default: deets[:default], type: deets[:type], short: :none
    end # KEYS.each
  end # Optimist.options

  # any other args are the keys to display,
  # convert them to symbols and store them in the config_opts
  config_opts[:keys_to_display] = ARGV.map { |k| k.gsub('-', '_').to_sym } unless ARGV.empty?
end

#parse_global_optsObject

Parse the main/global options



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
121
122
123
# File 'lib/xolo/server/command_line.rb', line 95

def parse_global_opts
  usg = usage
  @cli_opts = Optimist.options do
    stop_on SUBCOMMANDS
    version "Xolo version: #{Xolo::VERSION}"
    synopsis <<~SYNOPSIS
      Name:
       #{Xolo::Server::EXECUTABLE_FILENAME}, The server for 'xolo', a tool for managing Patch Titles and Versions in Jamf Pro

      Usage:
        #{usg}

      See '#{Xolo::Server::EXECUTABLE_FILENAME} config --help' for configuration options.
    SYNOPSIS

    # add a blank line between each of the cli options in the help output
    # NOTE: chrisl added this to the optimist.rb included in this project.
    insert_blanks

    # The global opts
    ## manually set :version and :help here, or they appear at the bottom of the help
    opt :version, 'Print version and exit'
    opt :help, 'Show this help and exit'

    CLI_OPTIONS.each do |opt_key, deets|
      opt opt_key, deets[:desc], short: deets[:cli]
    end
  end # Optimist.options
end

#usageObject

CLI usage message



60
61
62
# File 'lib/xolo/server/command_line.rb', line 60

def usage
  @usage ||= "#{Xolo::Server::EXECUTABLE_FILENAME} --production --debug --help --version [config --help options args]"
end