Skip to content

Commit

Permalink
Merge pull request #128 from boltops-tools/argv
Browse files Browse the repository at this point in the history
Argv
  • Loading branch information
tongueroo authored Jul 26, 2021
2 parents d47f731 + b1d504b commit 954665b
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 13 deletions.
4 changes: 2 additions & 2 deletions lib/terraspace/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ def show(mod)

desc "state SUBCOMMAND STACK", "Run state."
long_desc Help.text(:state)
def state(subcommand, mod)
State.new(options.merge(subcommand: subcommand, mod: mod)).run
def state(subcommand, mod, *rest)
State.new(options.merge(subcommand: subcommand, mod: mod, rest: rest)).run
end

desc "test", "Run test."
Expand Down
2 changes: 1 addition & 1 deletion lib/terraspace/cli/check_setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def check_required_version!
end

def check_command?
ARGV[0] == "check_setup"
Terraspace.argv[0] == "check_setup"
end

def terraform_is_not_installed
Expand Down
30 changes: 30 additions & 0 deletions lib/terraspace/cli/help/state.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
## Examples

terraspace state list demo
terraspace state mv demo
terraspace state pull demo
terraspace state push demo
terraspace state replace demo
terraspace state rm demo
terraspace state show demo

## Args Straight Delegation

The `terraspace state` command delegates to the `terraform state` commands passing the arguments straight through. Refer to the underlying `terraform` command help for arguments. Example:

terraform state list -h
...
Options:
...
-id=ID Filters the results to include only instances whose
resource types have an attribute named "id" whose value
equals the given id string.

This means we can use the `-id` or `--id` option and terraspace will pass it straight through. Example:

terraspace state list demo --id enabled-bull
Building .terraspace-cache/us-west-2/dev/stacks/demo
Built in .terraspace-cache/us-west-2/dev/stacks/demo
Current directory: .terraspace-cache/us-west-2/dev/stacks/demo
=> terraform state list --id enabled-bull
random_pet.this
2 changes: 1 addition & 1 deletion lib/terraspace/cli/init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def already_init?

# only top level command considered
def calling_command
ARGV[0]
Terraspace.argv[0]
end
end
end
10 changes: 9 additions & 1 deletion lib/terraspace/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class << self
include Terraspace::Util::Logging

def dispatch(m, args, options, config)
# Terraspace.argv provides consistency when terraspace is being called by rspec-terrspace test harness
Terraspace.argv = args.clone # important to clone since Thor removes the first argv

check_standalone_install!
check_project!(args.first)

Expand All @@ -42,7 +45,6 @@ def dispatch(m, args, options, config)
# as well thor's normal way:
#
# terraspace help command
help_flags = Thor::HELP_MAPPINGS + ["help"]
if args.length > 1 && !(args & help_flags).empty?
args -= help_flags
args.insert(-2, "help")
Expand All @@ -59,6 +61,11 @@ def dispatch(m, args, options, config)
super
end

def help_flags
Thor::HELP_MAPPINGS + ["help"]
end
private :help_flags

def check_standalone_install!
return unless opt?
version_manager = "rvm" if rvm?
Expand Down Expand Up @@ -94,6 +101,7 @@ def rbenv?
def check_project!(command_name)
return if subcommand?
return if command_name.nil?
return if help_flags.include?(Terraspace.argv.last) # IE: -h help
return if %w[-h -v check_setup completion completion_script help new test version].include?(command_name)
return if File.exist?("#{Terraspace.root}/config/app.rb")
logger.error "ERROR: It doesnt look like this is a terraspace project. Are you sure you are in a terraspace project?".color(:red)
Expand Down
4 changes: 2 additions & 2 deletions lib/terraspace/compiler/commands_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ def requires_backend_commands
def command_is?(*commands)
commands.flatten!
commands.map!(&:to_s)
commands.include?(ARGV[0]) || # IE: terraspace up
ARGV[0] == "all" && commands.include?(ARGV[1]) # IE: terraspace all up
commands.include?(Terraspace.argv[0]) || # IE: terraspace up
Terraspace.argv[0] == "all" && commands.include?(Terraspace.argv[1]) # IE: terraspace all up
end
end
end
2 changes: 1 addition & 1 deletion lib/terraspace/compiler/dsl/syntax/helpers/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def local
# terraspace_command('-') => "terraspace-up-demo"
#
def terraspace_command(separator=' ')
args = ARGV[0..1] || []
args = Terraspace.argv[0..1] || []
command = ["terraspace"] + args
command.join(separator)
end
Expand Down
10 changes: 10 additions & 0 deletions lib/terraspace/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,15 @@ def pass_file?(path)
i.is_a?(Regexp) ? path =~ i : path.include?(i)
end
end

# Terraspace.argv provides consistency when terraspace is being called by rspec-terrspace test harness
# So use Terraspace.argv instead of ARGV constant
def argv=(argv)
@@argv = argv
end

def argv
@@argv
end
end
end
20 changes: 16 additions & 4 deletions lib/terraspace/terraform/args/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,24 @@ def args
# https://terraspace.cloud/docs/ci-automation/
ENV['TF_IN_AUTOMATION'] = '1' if @options[:auto]

args_meth = "#{@name}_args"
args = []

if straight_delegate_args?
args += @options[:rest]
args.flatten!
end

args_meth = "#{@name}_args".gsub(' ', '_') # IE: apply_args, init_args
if respond_to?(args_meth)
send(args_meth)
else
[]
args += send(args_meth)
end

args
end

# delegate args straight through for special commands, currently state seems to be the only case
def straight_delegate_args?
@name.include?("state") # IE: "state list", "state pull", "state show"
end

def force_unlock_args
Expand Down
2 changes: 1 addition & 1 deletion terraspace.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Gem::Specification.new do |spec|
spec.add_dependency "terraspace_plugin_aws", "~> 0.3.0"
spec.add_dependency "terraspace_plugin_azurerm", "~> 0.3.0"
spec.add_dependency "terraspace_plugin_google", "~> 0.3.0"
spec.add_dependency "rspec-terraspace", "~> 0.2.0"
spec.add_dependency "rspec-terraspace", "~> 0.3.0"

spec.add_development_dependency "bundler"
spec.add_development_dependency "byebug"
Expand Down

0 comments on commit 954665b

Please sign in to comment.