terraspace 0.5.12 → 0.6.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +30 -7
- data/README.md +2 -0
- data/lib/terraspace/app.rb +35 -15
- data/lib/terraspace/app/inits.rb +13 -0
- data/lib/terraspace/booter.rb +19 -2
- data/lib/terraspace/cli.rb +18 -5
- data/lib/terraspace/cli/commander.rb +1 -1
- data/lib/terraspace/cli/concerns/source_dirs.rb +13 -0
- data/lib/terraspace/cli/down.rb +1 -1
- data/lib/terraspace/cli/fmt.rb +21 -0
- data/lib/terraspace/cli/fmt/runner.rb +64 -0
- data/lib/terraspace/cli/help/fmt.md +10 -0
- data/lib/terraspace/cli/help/{cloud → tfc}/destroy.md +1 -1
- data/lib/terraspace/cli/help/{cloud → tfc}/list.md +1 -1
- data/lib/terraspace/cli/help/{cloud → tfc}/runs/list.md +3 -3
- data/lib/terraspace/cli/help/{cloud → tfc}/runs/prune.md +3 -3
- data/lib/terraspace/cli/help/{cloud → tfc}/sync.md +3 -3
- data/lib/terraspace/cli/init.rb +21 -8
- data/lib/terraspace/cli/list.rb +3 -2
- data/lib/terraspace/cli/new/helper.rb +2 -2
- data/lib/terraspace/cli/state.rb +10 -0
- data/lib/terraspace/cli/{cloud.rb → tfc.rb} +3 -3
- data/lib/terraspace/cli/{cloud → tfc}/runs.rb +4 -4
- data/lib/terraspace/compiler/builder.rb +4 -1
- data/lib/terraspace/compiler/helper_extender.rb +3 -3
- data/lib/terraspace/compiler/strategy/tfvar.rb +16 -4
- data/lib/terraspace/compiler/strategy/tfvar/layer.rb +75 -52
- data/lib/terraspace/ext.rb +1 -0
- data/lib/terraspace/ext/core/string.rb +5 -0
- data/lib/terraspace/layering.rb +24 -0
- data/lib/terraspace/logger.rb +8 -1
- data/lib/terraspace/mod.rb +18 -3
- data/lib/terraspace/plugin/expander/friendly.rb +10 -0
- data/lib/terraspace/plugin/expander/interface.rb +6 -1
- data/lib/terraspace/plugin/summary/interface.rb +1 -1
- data/lib/terraspace/shell.rb +16 -1
- data/lib/terraspace/terraform/api/runs.rb +13 -2
- data/lib/terraspace/terraform/api/token.rb +2 -2
- data/lib/terraspace/terraform/api/var.rb +1 -1
- data/lib/terraspace/terraform/api/vars.rb +1 -1
- data/lib/terraspace/terraform/api/vars/base.rb +2 -0
- data/lib/terraspace/terraform/api/vars/json.rb +13 -1
- data/lib/terraspace/terraform/api/workspace.rb +10 -3
- data/lib/terraspace/terraform/args/default.rb +23 -13
- data/lib/terraspace/terraform/ihooks/after/plan.rb +17 -0
- data/lib/terraspace/terraform/ihooks/base.rb +8 -0
- data/lib/terraspace/terraform/ihooks/before/plan.rb +14 -0
- data/lib/terraspace/terraform/remote_state/fetcher.rb +1 -1
- data/lib/terraspace/terraform/runner.rb +12 -0
- data/lib/terraspace/terraform/{cloud → tfc}/runs.rb +1 -1
- data/lib/terraspace/terraform/{cloud → tfc}/runs/base.rb +1 -1
- data/lib/terraspace/terraform/{cloud → tfc}/runs/item_presenter.rb +1 -1
- data/lib/terraspace/terraform/{cloud → tfc}/runs/lister.rb +1 -1
- data/lib/terraspace/terraform/{cloud → tfc}/runs/pruner.rb +1 -1
- data/lib/terraspace/terraform/{cloud → tfc}/sync.rb +2 -2
- data/lib/terraspace/terraform/{cloud → tfc}/syncer.rb +1 -1
- data/lib/terraspace/terraform/{cloud → tfc}/workspace.rb +2 -3
- data/lib/terraspace/util/pretty.rb +2 -1
- data/lib/terraspace/version.rb +1 -1
- metadata +29 -18
- data/lib/terraspace/app/hooks.rb +0 -18
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 56392f488a05d42760586d379da36022589979733b654ea3f5f3d5fcb879de26
|
|
4
|
+
data.tar.gz: 690b3d0fa3e192aee02fd34ae187ab4628caccd335a1733ce10040ff2ef55b32
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 4dcc7b6c1d1f2d21be6115d02cc34bbeb9f773e6ece16e8549ac3a8e6a4167fb6392a2917399399a0ba0ddde4ff880c9dff8de2ee5261def5250fc2525527ce1
|
|
7
|
+
data.tar.gz: 476d2c10897c2915a3cd601b556e783a639cb29a97c11505b5e34f32d8079c311e5ad8f6efeeee6cf9a143a2f63b9b46b76a9b6741d78802fd53baf8de319b29
|
data/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,29 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
This project *loosely tries* to adhere to [Semantic Versioning](http://semver.org/), even before v1.0.
|
|
5
5
|
|
|
6
|
+
## [0.6.4] - 2021-03-22
|
|
7
|
+
- [#94](https://github.com/boltops-tools/terraspace/pull/94) terraspace fmt command
|
|
8
|
+
|
|
9
|
+
## [0.6.3] - 2021-03-12
|
|
10
|
+
- [#91](https://github.com/boltops-tools/terraspace/pull/91) Camelcase
|
|
11
|
+
- [#92](https://github.com/boltops-tools/terraspace/pull/92) disable terraform.plugin_cache by default
|
|
12
|
+
- skip build config/helpers
|
|
13
|
+
|
|
14
|
+
## [0.6.2] - 2021-03-05
|
|
15
|
+
- [#90](https://github.com/boltops-tools/terraspace/pull/90) Boot hooks: new and improved boot hooks interface
|
|
16
|
+
- remove old config.hooks.on_boot
|
|
17
|
+
|
|
18
|
+
## [0.6.1] - 2021-03-04
|
|
19
|
+
- [#89](https://github.com/boltops-tools/terraspace/pull/89) rename option to enable_names.expansion
|
|
20
|
+
|
|
21
|
+
## [0.6.0] - 2021-03-03
|
|
22
|
+
- [#87](https://github.com/boltops-tools/terraspace/pull/87) rename cloud to tfc command and improvements
|
|
23
|
+
- [#88](https://github.com/boltops-tools/terraspace/pull/88) custom layering support
|
|
24
|
+
- Improve stdout handling, so this works: `terraspace show demo --json | jq`
|
|
25
|
+
- `terraspace plan --output plan.save` writes to relative cache path.
|
|
26
|
+
- `terraspace up --plan plan.save` copies plan.save to cache path.
|
|
27
|
+
- Add state command. Initial simple version.
|
|
28
|
+
|
|
6
29
|
## [0.5.12] - 2021-02-27
|
|
7
30
|
- [#79](https://github.com/boltops-tools/terraspace/pull/79) Fix syntax issue
|
|
8
31
|
- [#85](https://github.com/boltops-tools/terraspace/pull/85) Add all.include_stacks option and fix all.ignore_stacks option when building dependency graph
|
|
@@ -108,7 +131,7 @@ This project *loosely tries* to adhere to [Semantic Versioning](http://semver.or
|
|
|
108
131
|
* fix build for edge case when app/modules exist but app/stacks do not
|
|
109
132
|
* terraspace new project: do not generate spec folder by default
|
|
110
133
|
* improve all output summary
|
|
111
|
-
* remove redundant `terraspace
|
|
134
|
+
* remove redundant `terraspace tfc setup`, instead use: `terraspace tfc sync`
|
|
112
135
|
* improve terraspace info output
|
|
113
136
|
* fix integration test pipeline
|
|
114
137
|
|
|
@@ -127,8 +150,8 @@ This project *loosely tries* to adhere to [Semantic Versioning](http://semver.or
|
|
|
127
150
|
* Terraspace log: view and tail log files
|
|
128
151
|
* Terraspace logs management commands: `terraspace logs truncate` and `terraspace logs remove`
|
|
129
152
|
* TFC/TFE: Improve support. `config.cloud.vars`, `config.cloud.workspace.attrs`
|
|
130
|
-
* TFC commands: terraspace
|
|
131
|
-
* TFC VCS also sync as part of deploy. Also separate `terraspace
|
|
153
|
+
* TFC commands: terraspace tfc runs list, terraspace tfc runs prune
|
|
154
|
+
* TFC VCS also sync as part of deploy. Also separate `terraspace tfc sync` command
|
|
132
155
|
* Logger improvements: configurable formatter, log to stderr by default
|
|
133
156
|
* Rename: `cloud.relative_root` to `cloud.working_dir_prefix`
|
|
134
157
|
* Run a plan to capture the diff as part of `-y` option. IE: `terraspace up demo -y`
|
|
@@ -161,11 +184,11 @@ This project *loosely tries* to adhere to [Semantic Versioning](http://semver.or
|
|
|
161
184
|
* New expander variables: TYPE_INSTANCE, INSTANCE, CACHE_ROOT. Also added strip trailing - and / behavior.
|
|
162
185
|
* Timeout for terraform init. The default timeout is 10m and will then print out the terraform init log.
|
|
163
186
|
* Terraspace 0.2.x is compatible with terraspace\_plugin_aws 0.2.x, terraspace\_plugin_google 0.2.x, and terraspace\_plugin_azurerm 0.2.x
|
|
164
|
-
* New commands: terraspace list, terraspace
|
|
187
|
+
* New commands: terraspace list, terraspace tfc list, terraspace tfc setup, terraspace tfc destroy, terraspace new shim, terraspace new git_hook
|
|
165
188
|
* terraspace list: list of modules and stacks
|
|
166
|
-
* terraspace
|
|
167
|
-
* terraspace
|
|
168
|
-
* terraspace
|
|
189
|
+
* terraspace tfc list: shows list of TFC workspaces
|
|
190
|
+
* terraspace tfc setup: setups up TFC workspace for VCS-driven workflow. This automatically happens for the TFC CLI-driven workflow.
|
|
191
|
+
* terraspace tfc destroy: destroys the TFC workspace associated with the stack. Can also use the `terraspace down demo --destroy-workspace` option.
|
|
169
192
|
* terraspace new shim: An quick way to generate a terraspace shim.
|
|
170
193
|
* terraspace new git_hook: An quick way to set up a git pre-push hook for the TFC VCS-driven workflow.
|
|
171
194
|
* terraspace down: works even if there's no app/stacks folder. A fake stack is built.
|
data/README.md
CHANGED
data/lib/terraspace/app.rb
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
module Terraspace
|
|
2
2
|
class App
|
|
3
|
-
|
|
3
|
+
extend Memoist
|
|
4
4
|
include DslEvaluator
|
|
5
|
+
include Singleton
|
|
6
|
+
include Terraspace::Util::Logging
|
|
5
7
|
|
|
6
8
|
attr_reader :config
|
|
7
9
|
def initialize
|
|
@@ -9,8 +11,6 @@ module Terraspace
|
|
|
9
11
|
end
|
|
10
12
|
|
|
11
13
|
def defaults
|
|
12
|
-
ts_logger = Logger.new(ENV['TS_LOG_PATH'] || $stderr)
|
|
13
|
-
|
|
14
14
|
config = ActiveSupport::OrderedOptions.new
|
|
15
15
|
config.all = ActiveSupport::OrderedOptions.new
|
|
16
16
|
config.all.concurrency = 5
|
|
@@ -29,17 +29,6 @@ module Terraspace
|
|
|
29
29
|
config.build.clean_cache = nil # defaults to /full/path/to/.terraspace-cache
|
|
30
30
|
config.bundle = ActiveSupport::OrderedOptions.new
|
|
31
31
|
config.bundle.logger = ts_logger
|
|
32
|
-
config.cloud = ActiveSupport::OrderedOptions.new
|
|
33
|
-
config.cloud.auto_sync = true
|
|
34
|
-
config.cloud.hostname = nil
|
|
35
|
-
config.cloud.vars = ActiveSupport::OrderedOptions.new
|
|
36
|
-
config.cloud.vars.overwrite = true
|
|
37
|
-
config.cloud.vars.overwrite_sensitive = true
|
|
38
|
-
config.cloud.vars.show_message = "create"
|
|
39
|
-
config.cloud.working_dir_prefix = nil
|
|
40
|
-
config.cloud.workspace = ActiveSupport::OrderedOptions.new
|
|
41
|
-
config.cloud.workspace.attrs = ActiveSupport::OrderedOptions.new
|
|
42
|
-
config.hooks = Hooks.new
|
|
43
32
|
config.init = ActiveSupport::OrderedOptions.new
|
|
44
33
|
config.init.mode = "auto" # auto, never, always
|
|
45
34
|
config.log = ActiveSupport::OrderedOptions.new
|
|
@@ -47,22 +36,53 @@ module Terraspace
|
|
|
47
36
|
config.logger = ts_logger
|
|
48
37
|
config.logger.formatter = Logger::Formatter.new
|
|
49
38
|
config.logger.level = ENV['TS_LOG_LEVEL'] || :info
|
|
39
|
+
config.layering = ActiveSupport::OrderedOptions.new
|
|
40
|
+
config.layering.names = {}
|
|
41
|
+
config.layering.enable_names = ActiveSupport::OrderedOptions.new
|
|
42
|
+
config.layering.enable_names.expansion = true
|
|
43
|
+
config.summary = ActiveSupport::OrderedOptions.new
|
|
44
|
+
config.summary.prune = false
|
|
50
45
|
config.terraform = ActiveSupport::OrderedOptions.new
|
|
51
46
|
config.terraform.plugin_cache = ActiveSupport::OrderedOptions.new
|
|
52
47
|
config.terraform.plugin_cache.dir = ENV['TF_PLUGIN_CACHE_DIR'] || "#{Terraspace.tmp_root}/plugin_cache"
|
|
53
|
-
config.terraform.plugin_cache.enabled =
|
|
48
|
+
config.terraform.plugin_cache.enabled = false
|
|
54
49
|
config.terraform.plugin_cache.purge_on_error = true
|
|
55
50
|
config.test_framework = "rspec"
|
|
51
|
+
config.tfc = ActiveSupport::OrderedOptions.new
|
|
52
|
+
config.tfc.auto_sync = true
|
|
53
|
+
config.tfc.hostname = nil
|
|
54
|
+
config.tfc.vars = ActiveSupport::OrderedOptions.new
|
|
55
|
+
config.tfc.vars.overwrite = true
|
|
56
|
+
config.tfc.vars.overwrite_sensitive = true
|
|
57
|
+
config.tfc.vars.show_message = "create"
|
|
58
|
+
config.tfc.working_dir_prefix = nil
|
|
59
|
+
config.tfc.workspace = ActiveSupport::OrderedOptions.new
|
|
60
|
+
config.tfc.workspace.attrs = ActiveSupport::OrderedOptions.new
|
|
56
61
|
config
|
|
57
62
|
end
|
|
58
63
|
|
|
64
|
+
def ts_logger
|
|
65
|
+
Logger.new(ENV['TS_LOG_PATH'] || $stderr)
|
|
66
|
+
end
|
|
67
|
+
memoize :ts_logger
|
|
68
|
+
|
|
59
69
|
def configure
|
|
60
70
|
yield(@config)
|
|
61
71
|
end
|
|
62
72
|
|
|
63
73
|
def load_project_config
|
|
64
74
|
evaluate_file("#{Terraspace.root}/config/app.rb")
|
|
75
|
+
|
|
76
|
+
# deprecated config/env for config/envs
|
|
65
77
|
path = "#{Terraspace.root}/config/env/#{Terraspace.env}.rb"
|
|
78
|
+
if File.exist?(path)
|
|
79
|
+
# so early on in the boot process that logger.info is unavailable, use ts_logger which is available and same thing.
|
|
80
|
+
ts_logger.info "DEPRECATED: Please rename config/env to config/envs. IE:".color(:yellow)
|
|
81
|
+
ts_logger.info " mv config/env config/envs"
|
|
82
|
+
evaluate_file(path)
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
path = "#{Terraspace.root}/config/envs/#{Terraspace.env}.rb"
|
|
66
86
|
evaluate_file(path)
|
|
67
87
|
end
|
|
68
88
|
end
|
data/lib/terraspace/booter.rb
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
module Terraspace
|
|
2
2
|
module Booter
|
|
3
3
|
def boot
|
|
4
|
+
run_hooks
|
|
4
5
|
Terraspace::Bundle.require # load plugins
|
|
5
6
|
load_plugin_default_configs
|
|
6
|
-
Terraspace.
|
|
7
|
-
Terraspace::App::Hooks.run_hook(:on_boot)
|
|
7
|
+
Terraspace::App::Inits.run_all
|
|
8
8
|
set_plugin_cache!
|
|
9
9
|
end
|
|
10
10
|
|
|
@@ -23,6 +23,23 @@ module Terraspace
|
|
|
23
23
|
dir
|
|
24
24
|
end
|
|
25
25
|
|
|
26
|
+
# Special boot hooks run super early, even before plugins are loaded.
|
|
27
|
+
# Useful for setting env vars and other early things.
|
|
28
|
+
#
|
|
29
|
+
# config/boot.rb
|
|
30
|
+
# config/boot/dev.rb
|
|
31
|
+
#
|
|
32
|
+
def run_hooks
|
|
33
|
+
run_hook
|
|
34
|
+
run_hook(Terraspace.env)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def run_hook(env=nil)
|
|
38
|
+
name = env ? "boot/#{env}" : "boot"
|
|
39
|
+
path = "#{Terraspace.root}/config/#{name}.rb"
|
|
40
|
+
require path if File.exist?(path)
|
|
41
|
+
end
|
|
42
|
+
|
|
26
43
|
extend self
|
|
27
44
|
end
|
|
28
45
|
end
|
data/lib/terraspace/cli.rb
CHANGED
|
@@ -10,7 +10,7 @@ module Terraspace
|
|
|
10
10
|
option :format, desc: "output formats: json, text"
|
|
11
11
|
}
|
|
12
12
|
out_option = Proc.new {
|
|
13
|
-
option :out, aliases: :o, desc: "
|
|
13
|
+
option :out, aliases: :o, desc: "Write the output to path"
|
|
14
14
|
}
|
|
15
15
|
input_option = Proc.new {
|
|
16
16
|
option :input, type: :boolean, desc: "Ask for input for variables if not directly set."
|
|
@@ -36,14 +36,14 @@ module Terraspace
|
|
|
36
36
|
long_desc Help.text(:clean)
|
|
37
37
|
subcommand "clean", Clean
|
|
38
38
|
|
|
39
|
-
desc "cloud SUBCOMMAND", "cloud subcommands"
|
|
40
|
-
long_desc Help.text(:cloud)
|
|
41
|
-
subcommand "cloud", Cloud
|
|
42
|
-
|
|
43
39
|
desc "new SUBCOMMAND", "new subcommands"
|
|
44
40
|
long_desc Help.text(:new)
|
|
45
41
|
subcommand "new", New
|
|
46
42
|
|
|
43
|
+
desc "tfc SUBCOMMAND", "tfc subcommands"
|
|
44
|
+
long_desc Help.text(:tfc)
|
|
45
|
+
subcommand "tfc", Tfc
|
|
46
|
+
|
|
47
47
|
desc "build [STACK]", "Build project."
|
|
48
48
|
long_desc Help.text(:build)
|
|
49
49
|
option :quiet, type: :boolean, desc: "quiet output"
|
|
@@ -82,6 +82,12 @@ module Terraspace
|
|
|
82
82
|
Down.new(options.merge(mod: mod)).run
|
|
83
83
|
end
|
|
84
84
|
|
|
85
|
+
desc "fmt", "Run terraform fmt"
|
|
86
|
+
long_desc Help.text(:fmt)
|
|
87
|
+
def fmt
|
|
88
|
+
Fmt.new(options).run
|
|
89
|
+
end
|
|
90
|
+
|
|
85
91
|
desc "info STACK", "Get info about stack."
|
|
86
92
|
long_desc Help.text(:info)
|
|
87
93
|
instance_option.call
|
|
@@ -123,6 +129,7 @@ module Terraspace
|
|
|
123
129
|
instance_option.call
|
|
124
130
|
out_option.call
|
|
125
131
|
reconfigure_option.call
|
|
132
|
+
option :copy_to_root, type: :boolean, default: true, desc: "Copy plan file generated in the cache folder back to project root"
|
|
126
133
|
def plan(mod)
|
|
127
134
|
Commander.new("plan", options.merge(mod: mod)).run
|
|
128
135
|
end
|
|
@@ -169,6 +176,12 @@ module Terraspace
|
|
|
169
176
|
Commander.new("show", options.merge(mod: mod)).run
|
|
170
177
|
end
|
|
171
178
|
|
|
179
|
+
desc "state SUBCOMMAND STACK", "Run state."
|
|
180
|
+
long_desc Help.text(:state)
|
|
181
|
+
def state(subcommand, mod)
|
|
182
|
+
State.new(options.merge(subcommand: subcommand, mod: mod)).run
|
|
183
|
+
end
|
|
184
|
+
|
|
172
185
|
desc "test", "Run test."
|
|
173
186
|
long_desc Help.text(:test)
|
|
174
187
|
def test
|
|
@@ -7,7 +7,7 @@ class Terraspace::CLI
|
|
|
7
7
|
|
|
8
8
|
def run
|
|
9
9
|
Terraspace::Builder.new(@options).run unless @options[:build] # Up already ran build
|
|
10
|
-
Init.new(@options
|
|
10
|
+
Init.new(@options).run
|
|
11
11
|
Terraspace::Terraform::Runner.new(@name, @options).run
|
|
12
12
|
end
|
|
13
13
|
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
module Terraspace::CLI::Concerns
|
|
2
|
+
module SourceDirs
|
|
3
|
+
# used by list
|
|
4
|
+
def source_dirs
|
|
5
|
+
Dir.glob("{app,vendor}/{modules,stacks}/*").select { |p| File.directory?(p) }.sort
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
# dont include vendor: used by fmt
|
|
9
|
+
def app_source_dirs
|
|
10
|
+
Dir.glob("{app}/{modules,stacks}/*").select { |p| File.directory?(p) }.sort
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
data/lib/terraspace/cli/down.rb
CHANGED
|
@@ -14,7 +14,7 @@ class Terraspace::CLI
|
|
|
14
14
|
|
|
15
15
|
def destroy
|
|
16
16
|
Commander.new("destroy", @options.merge(command: "down")).run
|
|
17
|
-
Terraspace::Terraform::
|
|
17
|
+
Terraspace::Terraform::Tfc::Workspace.new(@options).destroy if @options[:destroy_workspace]
|
|
18
18
|
end
|
|
19
19
|
end
|
|
20
20
|
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
class Terraspace::CLI
|
|
2
|
+
class Fmt
|
|
3
|
+
include Concerns::SourceDirs
|
|
4
|
+
include Terraspace::Util::Logging
|
|
5
|
+
|
|
6
|
+
def initialize(options={})
|
|
7
|
+
@options = options
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def run
|
|
11
|
+
logger.info "Formating terraform files"
|
|
12
|
+
app_source_dirs.each do |dir|
|
|
13
|
+
format(dir)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def format(dir)
|
|
18
|
+
Runner.new(dir).format!
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
class Terraspace::CLI::Fmt
|
|
2
|
+
class Runner
|
|
3
|
+
include Terraspace::CLI::Concerns::SourceDirs
|
|
4
|
+
include Terraspace::Util::Logging
|
|
5
|
+
SKIP_PATTERN = /\.skip$/
|
|
6
|
+
|
|
7
|
+
def initialize(dir)
|
|
8
|
+
@dir = dir
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def format!
|
|
12
|
+
logger.info @dir.color(:green)
|
|
13
|
+
Dir.chdir(@dir) do
|
|
14
|
+
skip_rename
|
|
15
|
+
begin
|
|
16
|
+
terraform_fmt
|
|
17
|
+
ensure
|
|
18
|
+
restore_rename
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def skip_rename
|
|
24
|
+
tf_files.each do |path|
|
|
25
|
+
if !skip?(path) && erb?(path)
|
|
26
|
+
FileUtils.mv(path, "#{path}.skip")
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def terraform_fmt
|
|
32
|
+
sh "terraform fmt"
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def sh(command)
|
|
36
|
+
logger.debug("=> #{command}")
|
|
37
|
+
success = system(command)
|
|
38
|
+
return if success
|
|
39
|
+
logger.info "WARN: There were some errors running terraform fmt for files in #{@dir}:".color(:yellow)
|
|
40
|
+
logger.info "The errors are shown above"
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def restore_rename
|
|
44
|
+
tf_files.each do |path|
|
|
45
|
+
if skip?(path) && erb?(path)
|
|
46
|
+
FileUtils.mv(path, path.sub(SKIP_PATTERN, '')) # original name
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
private
|
|
52
|
+
def skip?(path)
|
|
53
|
+
!!(path =~ SKIP_PATTERN)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def erb?(path)
|
|
57
|
+
IO.readlines(path).detect { |l| l.include?('<%') }
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def tf_files
|
|
61
|
+
Dir.glob("#{Terraspace.root}/#{@dir}/**/*.{tf,skip}").select { |p| File.file?(p) }
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|