Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

better wrap and pass through terraform args #172

Merged
merged 1 commit into from
Dec 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 21 additions & 26 deletions lib/terraspace/cli.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
module Terraspace
class CLI < Command
include Help
include Concern

class_option :verbose, type: :boolean
class_option :noop, type: :boolean

yes_option = Proc.new {
option :yes, aliases: :y, type: :boolean, desc: "-auto-approve the terraform apply"
}
format_option = Proc.new {
option :format, desc: "output formats: json, text"
}
out_option = Proc.new {
option :out, aliases: :o, desc: "Write the output to path"
}
Expand Down Expand Up @@ -73,8 +70,8 @@ def check_setup
desc "console STACK", "Run console in built terraform project."
long_desc Help.text(:console)
instance_option.call
def console(mod)
Commander.new("console", options.merge(mod: mod, shell: "system")).run
def console(mod, *args)
Commander.new("console", options.merge(mod: mod, args: args, shell: "system")).run
end

desc "down STACK", "Destroy infrastructure stack."
Expand All @@ -83,8 +80,8 @@ def console(mod)
yes_option.call
reconfigure_option.call
option :destroy_workspace, type: :boolean, desc: "Also destroy the Cloud workspace. Only applies when using Terraform Cloud remote backend."
def down(mod)
Down.new(options.merge(mod: mod)).run
def down(mod, *args)
Down.new(options.merge(mod: mod, args: args)).run
end

desc "force_unlock", "Calls terrform force-unlock"
Expand Down Expand Up @@ -113,8 +110,8 @@ def info(mod)
desc "init STACK", "Run init in built terraform project."
long_desc Help.text(:init)
instance_option.call
def init(mod)
Commander.new("init", options.merge(mod: mod, quiet: false)).run
def init(mod, *args)
Commander.new("init", options.merge(mod: mod, args: args, quiet: false)).run
end

desc "list", "List stacks and modules."
Expand Down Expand Up @@ -143,22 +140,22 @@ def logs(action=nil, stack=nil)
out_option.call
reconfigure_option.call
option :copy_to_root, type: :boolean, default: true, desc: "Copy plan file generated in the cache folder back to project root"
def plan(mod)
Commander.new("plan", options.merge(mod: mod)).run
def plan(mod, *args)
Commander.new("plan", options.merge(mod: mod, args: args)).run
end

desc "providers STACK", "Show providers."
long_desc Help.text(:providers)
instance_option.call
def providers(mod)
Commander.new("providers", options.merge(mod: mod)).run
def providers(mod, *args)
Commander.new("providers", options.merge(mod: mod, args: args)).run
end

desc "refresh STACK", "Run refresh."
long_desc Help.text(:refresh)
instance_option.call
def refresh(mod)
Commander.new("refresh", options.merge(mod: mod)).run
def refresh(mod, *args)
Commander.new("refresh", options.merge(mod: mod, args: args)).run
end

desc "seed STACK", "Build starer seed tfvars file."
Expand All @@ -184,9 +181,8 @@ def summary
long_desc Help.text(:show)
instance_option.call
option :plan, desc: "path to created.plan"
option :json, type: :boolean, desc: "show plan in json format"
def show(mod)
Commander.new("show", options.merge(mod: mod)).run
def show(mod, *args)
Commander.new("show", options.merge(mod: mod, args: args)).run
end

desc "state SUBCOMMAND STACK", "Run state."
Expand All @@ -203,11 +199,10 @@ def test

desc "output STACK", "Run output."
long_desc Help.text(:output)
format_option.call
instance_option.call
out_option.call
def output(mod)
Commander.new("output", options.merge(mod: mod)).run
def output(mod, *args)
Commander.new("output", options.merge(mod: mod, args: args)).run
end

desc "up STACK", "Deploy infrastructure stack."
Expand All @@ -220,15 +215,15 @@ def output(mod)
reconfigure_option.call
option :plan, desc: "Execution plan that can be used to only execute a pre-determined set of actions."
option :var_files, type: :array, desc: "list of var files"
def up(mod)
Up.new(options.merge(mod: mod)).run
def up(mod, *args)
Up.new(options.merge(mod: mod, args: args)).run
end

desc "validate STACK", "Validate stack."
long_desc Help.text(:validate)
instance_option.call
def validate(mod)
Commander.new("validate", options.merge(mod: mod)).run
def validate(mod, *args)
Commander.new("validate", options.merge(mod: mod, args: args)).run
end

desc "completion *PARAMS", "Prints words for auto-completion."
Expand Down
111 changes: 111 additions & 0 deletions lib/terraspace/cli/concern.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
class Terraspace::CLI
module Concern
extend ActiveSupport::Concern

class_methods do
# So the Thor::Parser::Options#parse allows flag switch shorthand notation. IE:
#
# command -abc
#
# Is same as:
#
# command -a -b -c
#
# This messes up the options like -destroy.
#
# terraspace plan -destroy
#
# Since only a single dash (-) is passed. It's interpreted as a bunch of flag switches.
# Providing -- works just fine
#
# terraspace plan --destroy
#
# But it'll be nice if user can use -destroy or --destroy
#
# Interestingly, -no-color won't be interpreted by Thor as switch flag due to the - inbetween.
#
# Looked into the Thor code:
#
# Thor::Parser::Options#parse https://github.com/rails/thor/blob/5c666b4c25e748e57eec2d529d94c5059030979e/lib/thor/parser/options.rb#L88
# Thor::Parser::Options#current_is_switch? https://github.com/rails/thor/blob/5c666b4c25e748e57eec2d529d94c5059030979e/lib/thor/parser/options.rb#L165
#
# Overriding the current_is_switch? is dirtier than overriding start
# and adjusting the argv before passing to Thor.
#
# There are only so few Terraform boolean options that are single words. To find (fish shell)
#
# for i in init validate plan apply destroy console fmt force-unlock get graph import login logout output providers refresh show state taint test untaint version workspace ; echo "$i:" ; terraform $i -h | grep '^ -' ; end
# for i in init validate plan apply destroy console fmt force-unlock get graph import login logout output providers refresh show state taint test untaint version workspace ; echo "$i:" ; terraform $i -h | grep '^ -' | grep -v = | sed 's/ -//' | grep -v - | sed -r 's/\s+.*//' | sed 's/^/ /' ; end
# for i in init validate plan apply destroy console fmt force-unlock get graph import login logout output providers refresh show state taint test untaint version workspace ; terraform $i -h | grep '^ -' | grep -v = | sed 's/ -//' | grep -v - | sed -r 's/\s+.*//' ; end | sort | uniq
#
def start(argv)
single_word_boolean_args = %w[
check
destroy
diff
force
json
raw
reconfigure
recursive
upgrade
]
argv.map! do |arg|
word = arg.sub(/^-/,'')
if single_word_boolean_args.include?(word)
# Add double dash (--).
# Later in Terraspace::Terraform::Args::Pass#args a single dash (-) is ensured.
"--#{word}" # IE: destroy => --destroy
else
arg
end
end
super(argv)
end

def main_commands
%w[
all
build
bundle
down
list
new
plan
seed
up
]
end

def help(shell, subcommand)
list = printable_commands(true, subcommand)
list.sort! { |a, b| a[0] <=> b[0] }
filter = Proc.new do |command, desc|
main_commands.detect { |name| command =~ Regexp.new("^terraspace #{name}") }
end
main = list.select(&filter)
other = list.reject(&filter)

shell.say <<~EOL
Usage: terraspace COMMAND [args]

The available commands are listed below.
The primary workflow commands are given first, followed by
less common or more advanced commands.
EOL
shell.say "\nMain Commands:\n\n"
shell.print_table(main, indent: 2, truncate: true)
shell.say "\nOther Commands:\n\n"
shell.print_table(other, indent: 2, truncate: true)
shell.say <<~EOL

For more help on each command, you can use the -h option. Example:

terraspace up -h

CLI Reference also available at: https://terraspace.cloud/reference/
EOL
end
end
end
end
50 changes: 1 addition & 49 deletions lib/terraspace/cli/help.rb
Original file line number Diff line number Diff line change
@@ -1,58 +1,10 @@
class Terraspace::CLI
module Help
extend ActiveSupport::Concern

class_methods do
def main_commands
%w[
all
build
bundle
down
list
new
plan
seed
up
]
end

def help(shell, subcommand)
list = printable_commands(true, subcommand)
list.sort! { |a, b| a[0] <=> b[0] }
filter = Proc.new do |command, desc|
main_commands.detect { |name| command =~ Regexp.new("^terraspace #{name}") }
end
main = list.select(&filter)
other = list.reject(&filter)

shell.say <<~EOL
Usage: terraspace COMMAND [args]

The available commands are listed below.
The primary workflow commands are given first, followed by
less common or more advanced commands.
EOL
shell.say "\nMain Commands:\n\n"
shell.print_table(main, indent: 2, truncate: true)
shell.say "\nOther Commands:\n\n"
shell.print_table(other, indent: 2, truncate: true)
shell.say <<~EOL

For more help on each command, you can use the -h option. Example:

terraspace up -h

CLI Reference also available at: https://terraspace.cloud/reference/
EOL
end
end

def text(namespaced_command)
path = namespaced_command.to_s.gsub(':','/')
path = File.expand_path("../help/#{path}.md", __FILE__)
IO.read(path) if File.exist?(path)
end
extend self
end
end
end
17 changes: 11 additions & 6 deletions lib/terraspace/terraform/args/custom.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,28 @@ def build
memoize :build

def args
terraform_args + var_file_args
end

def env_vars
build
dig("env", {})
end

private
def terraform_args
build
args = dig("args")
args.compact.flatten
end

def var_files
def var_file_args
build
var_files = dig("var_files")
var_files.select! { |f| var_file_exist?(f) }
var_files.map { |f| "-var-file=#{f}" }
end

def env_vars
build
dig("env", {})
end

def var_file_exist?(var_file)
File.exist?("#{@mod.cache_dir}/#{var_file}")
end
Expand Down
Loading