class Ops

executes commands based on local `ops.yml`

Constants

ACTION_CONFIG_ERROR_EXIT_CODE
ACTION_NOT_ALLOWED_IN_ENV_EXIT_CODE
BUILTIN_SYNTAX_ERROR_EXIT_CODE
ERROR_LOADING_APP_CONFIG_EXIT_CODE
INVALID_SYNTAX_EXIT_CODE
MIN_VERSION_NOT_MET_EXIT_CODE
RECOMMEND_HELP_TEXT
UNKNOWN_ACTION_EXIT_CODE

Public Class Methods

new(argv, config_file: nil) click to toggle source
# File lib/ops.rb, line 33
def initialize(argv, config_file: nil)
        @action_name = argv[0]
        @args = argv[1..-1]
        @config_file = config_file || "ops.yml"

        Options.set(config["options"] || {})
end
project_name() click to toggle source
# File lib/ops.rb, line 28
def project_name
        File.basename(::Dir.pwd)
end

Public Instance Methods

run() click to toggle source

rubocop:disable Metrics/MethodLength better to have all the rescues in one place

# File lib/ops.rb, line 43
def run
        # "return" is here to allow specs to stub "exit" without executing everything after it
        return exit(INVALID_SYNTAX_EXIT_CODE) unless syntax_valid?
        return exit(MIN_VERSION_NOT_MET_EXIT_CODE) unless min_version_met?

        runner.run
rescue Runner::UnknownActionError => e
        Output.error(e.to_s)
        Output.out(RECOMMEND_HELP_TEXT) unless print_did_you_mean
        exit(UNKNOWN_ACTION_EXIT_CODE)
rescue Runner::ActionConfigError => e
        Output.error("Error(s) running action '#{@action_name}': #{e}")
        exit(ACTION_CONFIG_ERROR_EXIT_CODE)
rescue Builtin::ArgumentError => e
        Output.error("Error running builtin '#{@action_name}': #{e}")
        exit(BUILTIN_SYNTAX_ERROR_EXIT_CODE)
rescue AppConfig::ParsingError => e
        Output.error("Error parsing app config: #{e}")
        exit(ERROR_LOADING_APP_CONFIG_EXIT_CODE)
rescue Runner::NotAllowedInEnvError => e
        Output.error("Error running action #{@action_name}: #{e}")
        exit(ACTION_NOT_ALLOWED_IN_ENV_EXIT_CODE)
end

Private Instance Methods

config() click to toggle source
# File lib/ops.rb, line 103
def config
        @config ||= begin
                if config_file_exists?
                        parsed_config_contents
                else
                        Output.warn("File '#{@config_file}' does not exist.") unless @action_name == "init"
                        {}
                end
        end
end
config_file_absolute_path() click to toggle source
# File lib/ops.rb, line 125
def config_file_absolute_path
        File.expand_path(@config_file)
end
config_file_exists?() click to toggle source
# File lib/ops.rb, line 121
def config_file_exists?
        File.exist?(@config_file)
end
min_version() click to toggle source
# File lib/ops.rb, line 95
def min_version
        config["min_version"]
end
min_version_met?() click to toggle source
# File lib/ops.rb, line 84
def min_version_met?
        return true unless min_version

        if Version.min_version_met?(min_version)
                true
        else
                Output.error("ops.yml specifies minimum version of #{min_version}, but ops version is #{Version.version}")
                false
        end
end
parsed_config_contents() click to toggle source
# File lib/ops.rb, line 114
def parsed_config_contents
        YAML.load_file(@config_file)
rescue StandardError => e
        Output.warn("Error parsing '#{@config_file}': #{e}")
        {}
end
print_did_you_mean() click to toggle source
runner() click to toggle source
# File lib/ops.rb, line 99
def runner
        @runner ||= Runner.new(@action_name, @args, config, config_file_absolute_path)
end
syntax_valid?() click to toggle source

rubocop:enable Metrics/MethodLength

# File lib/ops.rb, line 70
def syntax_valid?
        return true unless @action_name.nil?

        Output.error("Usage: ops <action>")
        Output.out(RECOMMEND_HELP_TEXT)
        false
end