Sha256: a22317f57f6d7b0e40e6c5938b4b2ff6896ddde113fac244ebd4597090a6571b

Contents?: true

Size: 1.82 KB

Versions: 84

Compression:

Stored size: 1.82 KB

Contents

class Terraspace::CLI::New
  class Hook < Thor::Group
    include Thor::Actions

    argument :stack, required: false

    def self.options
      [
        [:force, aliases: %w[y], type: :boolean, desc: "Bypass overwrite are you sure prompt for existing files"],
        [:kind, default: "terraform", desc: "terraform or terraspace"],
        [:name, desc: "Command name. Defaults to apply for terraform kind and build for terraspace kind"],
        [:type, default: "project", desc: "project, stack or module"],
      ]
    end
    options.each { |args| class_option(*args) }

    def self.source_root
      File.expand_path("../../../templates/base/hook", __dir__)
    end

  private
    def kind
      valid_kinds = %w[terraform terraspace]
      kind = @options[:kind]
      valid_kinds.include?(kind) ? kind : "terraform" # fallback to terraform if user provides invalid type
    end

    def type
      valid_types = %w[project stack module]
      type = @options[:type]
      valid_types.include?(type) ? type : "project" # fallback to project if user provides invalid type
    end

    def name
      return options[:name] if options[:name]
      kind == "terraform" ? "apply" : "build"
    end

    def dest
      map = {
        project: "config/hooks",
        stack:   "app/stacks/#{stack}/config/hooks",
        module:  "app/modules/#{stack}/config/hooks",
      }
      map[type.to_sym]
    end

    def hook_path
      "#{dest}/#{kind}.rb"
    end

  public

    def check_stack_arg
      return if type == "project"
      return unless stack.nil?
      # Else check for STACK argument for type module or stack
      puts <<~EOL
        Required STACK argument, either the module or stack name. Usage:

            terraspace new hook STACK --type #{type}
      EOL
      exit 1
    end

    def create
      directory ".", dest
    end
  end
end

Version data entries

84 entries across 84 versions & 1 rubygems

Version Path
terraspace-2.2.17 lib/terraspace/cli/new/hook.rb
terraspace-2.2.16 lib/terraspace/cli/new/hook.rb
terraspace-2.2.15 lib/terraspace/cli/new/hook.rb
terraspace-2.2.14 lib/terraspace/cli/new/hook.rb
terraspace-2.2.13 lib/terraspace/cli/new/hook.rb
terraspace-2.2.12 lib/terraspace/cli/new/hook.rb
terraspace-2.2.11 lib/terraspace/cli/new/hook.rb
terraspace-2.2.10 lib/terraspace/cli/new/hook.rb
terraspace-2.2.9 lib/terraspace/cli/new/hook.rb
terraspace-2.2.8 lib/terraspace/cli/new/hook.rb
terraspace-2.2.7 lib/terraspace/cli/new/hook.rb
terraspace-2.2.6 lib/terraspace/cli/new/hook.rb
terraspace-2.2.5 lib/terraspace/cli/new/hook.rb
terraspace-2.2.4 lib/terraspace/cli/new/hook.rb
terraspace-2.2.3 lib/terraspace/cli/new/hook.rb
terraspace-2.2.2 lib/terraspace/cli/new/hook.rb
terraspace-2.2.1 lib/terraspace/cli/new/hook.rb
terraspace-2.2.0 lib/terraspace/cli/new/hook.rb
terraspace-2.1.7 lib/terraspace/cli/new/hook.rb
terraspace-2.1.6 lib/terraspace/cli/new/hook.rb