tfstats 0.1.2 → 1.0.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4c8cbe2d7c9f90fda5372250525decbfeee1c5f2ba67444a2bc1064654149bbd
4
- data.tar.gz: 36767c31891db3dc03d202a5301b223364d32bb911e7b7f1cb298a1cb630b3a5
3
+ metadata.gz: 91c8270f08cae5469a806f4bc110ce70d6140e4572abafdaada4057c29340aab
4
+ data.tar.gz: 58df16ddf2406c6423a4c209beff8fdcb7264e8f4f7255788e1bba123efc8900
5
5
  SHA512:
6
- metadata.gz: 1522702b467a722c0b70980deeba1a33171cc076a7f38d0efd7ec8d9ddb4c4aa1a3f1dbc320aa99c68530b36bc90fb028663a6a5149cae08756268c33e3b9c29
7
- data.tar.gz: 006ea377946e9ad2675e6d5e2454141be5c5ff7661194b756e7b49051dd04a450404c86fbc39a5adfa9df1578550c8e7e97eb76ef9a125e59687305cdf7b75e3
6
+ metadata.gz: c2936e8d0794de0b5fd625ec0a5f674cb404ff1c18188ecf7b0e3253c36031a7fd8625faea0ab067a7eb8ab7c1231cfe8248e8772531854cc75e615019f5e7b9
7
+ data.tar.gz: 0573dcbfdc122afe92fbad64246ca1026c7ea67bbf6726c6dd3cb8bea48253eca067e32fa70fe9f005affeec85351c5bc9c258737a7548cd6673f825aadd17d1
data/CHANGELOG.md ADDED
@@ -0,0 +1,15 @@
1
+
2
+ <a name="v1.0.0"></a>
3
+ ## [v1.0.0](https://github.com/stonefield/tfstats/compare/v0.1.2...v1.0.0)
4
+
5
+ * Feature: Display versioning information for a project
6
+
7
+ <a name="v0.1.2"></a>
8
+ ## [v0.1.2](https://github.com/stonefield/tfstats/compare/v0.1.1...v0.1.2)
9
+
10
+ * Bug: Missed reference to rake/tasklib
11
+
12
+ <a name="v0.1.1"></a>
13
+ ## [v0.1.1](https://github.com/stonefield/tfstats/compare/v0.1.0...v0.1.1)
14
+
15
+ * Documentation: Updated gem description
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- tfstats (0.1.2)
4
+ tfstats (1.0.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -56,11 +56,12 @@ All parameters are optional. The above shows the default:
56
56
  Usage: tfstats [options] [directory]
57
57
 
58
58
  Commands:
59
- -r / recursive : run recursive.
60
59
  -f <filespec> : specify filespec. Defaults to '*.tf' (use single quote!)
61
- -v / verbose : Output debug information
60
+ -r / recursive : run recursive.
62
61
  -t / tab : Tab separated output
63
-
62
+ -v / verbose : Output debug information
63
+ -x / versions. : Check versions of terraform and providers
64
+
64
65
  If no directory is specified, statistics is collected from current directory
65
66
  ```
66
67
 
@@ -71,6 +72,14 @@ All parameters are optional. The above shows the default:
71
72
  rake stats:terraform
72
73
  ```
73
74
 
75
+ ### Identify vesions used in a project
76
+
77
+ The following command will list terraform versions and provider versions used in a project
78
+
79
+ ```bash
80
+ tfstats -r -x
81
+ ```
82
+
74
83
  ## Development
75
84
 
76
85
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
data/bin/tfstats CHANGED
@@ -9,6 +9,7 @@ def main
9
9
  filespec = "*.tf"
10
10
  directory = '.'
11
11
  tabseparated = false
12
+ versions = false
12
13
  while arg = ARGV.shift
13
14
  case arg
14
15
  when 'help', '--help', '-h'
@@ -21,12 +22,17 @@ def main
21
22
  Tfstats.verbose = true
22
23
  when '-t', 'tab'
23
24
  tabseparated = true
25
+ when '-x', 'versions'
26
+ versions = true
24
27
  else
25
28
  directory = arg
26
29
  end
27
30
  end
28
-
29
- Tfstats::Collector.collect(directory, filespec, recursive, tabseparated)
31
+ if versions
32
+ Tfstats::Collector.versions(directory, filespec, recursive, tabseparated)
33
+ else
34
+ Tfstats::Collector.collect(directory, filespec, recursive, tabseparated)
35
+ end
30
36
  end
31
37
 
32
38
  def help
@@ -42,10 +48,11 @@ HELP_TEXT = <<-DOC
42
48
  Usage: #{__FILE__.sub(/.*\//,'')} [options] [directory]
43
49
 
44
50
  Commands:
45
- -r / recursive : run recursive.
46
51
  -f <filespec> : specify filespec. Defaults to '*.tf' (use single quote!)
47
- -v / verbose : Output debug information
52
+ -r / recursive : run recursive.
48
53
  -t / tab : Tab separated output
54
+ -v / verbose : Output debug information
55
+ -x / versions. : Check versions of terraform and providers
49
56
 
50
57
  If no directory is specified, statistics is collected from current directory
51
58
 
@@ -5,12 +5,23 @@ module Tfstats
5
5
  class Collector
6
6
  class << self
7
7
  def collect(directory, filespec, recursive, tabseparated)
8
- statistics = process_dir(directory, filespec, recursive)
8
+ statistics = {}
9
+ process_dir(directory, filespec, recursive) do |collector, placeholder|
10
+ statistics[placeholder] = collector.collect
11
+ end
9
12
  puts output( statistics, tabseparated)
10
13
  end
11
14
 
15
+ def versions(directory, filespec, recursive, tabseparated)
16
+ versions = {}
17
+ process_dir(directory, filespec, recursive) do |collector, placeholder|
18
+ versions[placeholder] = collector.versions if collector.any? # Check versions only if there are any relevant files in the directory
19
+ end
20
+ puts output_versions( versions, tabseparated)
21
+ end
22
+
23
+
12
24
  def process_dir(directory, filespec, recursive)
13
- statistics = {}
14
25
  basename = File.basename File.expand_path(directory)
15
26
  info "directory: #{directory}"
16
27
  info "basename: #{basename}"
@@ -20,12 +31,11 @@ module Tfstats
20
31
  Dir.glob("#{directory}/**/").each do |dir|
21
32
  info "traversing: #{dir}"
22
33
  placeholder = dir.sub(/^#{Regexp.escape(directory.to_s)}/, basename).chop
23
- statistics[placeholder] = new(dir, filespec).collect
34
+ yield new(dir, filespec), placeholder
24
35
  end
25
36
  else
26
- statistics[basename] = new(directory,filespec).collect
37
+ yield new(directory,filespec), basename
27
38
  end
28
- statistics
29
39
  end
30
40
 
31
41
  def output(statistics, tabseparated)
@@ -54,6 +64,48 @@ module Tfstats
54
64
  str
55
65
  end
56
66
 
67
+ def output_versions(statistics, tabseparated)
68
+ str = ''
69
+ info "statistics collected: #{statistics.inspect}"
70
+ if tabseparated
71
+ pattern = (["%s"] * 3).join("\t") + "\n"
72
+ separator = ''
73
+ str << (pattern % %w(Directory Provider Version))
74
+ statistics.each do |dir, info|
75
+ str << (pattern % [dir, 'terraform-version', info[:terraform_version]])
76
+ if info[:providers].is_a? Hash
77
+ info[:providers].each do |provider, version|
78
+ str << (pattern % [dir, provider, version])
79
+ end
80
+ else
81
+ str << (pattern % [dir, 'providers', info[:providers]])
82
+ end
83
+ end
84
+ else
85
+ max_name = statistics.map do |dir, info|
86
+ if info[:providers].is_a? Hash
87
+ info[:providers].map { |k,v| k.size }
88
+ else
89
+ info[:providers].size
90
+ end
91
+ end.flatten.max + 1
92
+ pattern = " %-#{max_name}.#{max_name}s %8s\n"
93
+ statistics.each do |dir, info|
94
+ str << "#{dir}:\n"
95
+ str << " terraform-version: #{info[:terraform_version]}\n"
96
+ if info[:providers].is_a? Hash
97
+ str << " providers:\n"
98
+ info[:providers].each do |provider,version|
99
+ str << ( pattern % ["#{provider}:",version])
100
+ end
101
+ else
102
+ str << " providers: #{info[:providers]}\n"
103
+ end
104
+ end
105
+ end
106
+ str
107
+ end
108
+
57
109
  def sum(statistics)
58
110
  s = {}
59
111
  s.default = 0
@@ -84,6 +136,11 @@ module Tfstats
84
136
  @stats.default = 0
85
137
  end
86
138
 
139
+ def any?
140
+ spec = File.join(directory, filespec)
141
+ Dir[spec].any?
142
+ end
143
+
87
144
  def info(msg)
88
145
  self.class.info msg
89
146
  end
@@ -123,5 +180,63 @@ module Tfstats
123
180
  @stats
124
181
  end
125
182
 
183
+ def versions
184
+ @stats[:terraform_version] = terraform_version
185
+ @stats[:providers] = provider_versions
186
+ @stats
187
+ end
188
+
189
+ def terraform_version
190
+ file = File.join(directory, '.terraform-version')
191
+ version = ''
192
+ if File.exist? file
193
+ info "Fetching version from: #{file}"
194
+ version = File.read(file).chomp
195
+ else
196
+ info "No terraform version file found (#{file})"
197
+ end
198
+ version == '' ? 'undefined' : version
199
+ end
200
+
201
+ def provider_versions
202
+ file = File.join(directory, '.terraform.lock.hcl')
203
+ providers = {}
204
+ if File.exist? file
205
+ state = :root
206
+ provider = nil
207
+ begin
208
+ info "Fetching provider versions from: #{file}"
209
+ File.read(file).each_line.with_index do |line,i|
210
+ begin
211
+ case state
212
+ when :root
213
+ if line =~ /^\s*provider\s*\"(.+)\"\s*\{\s*$/
214
+ provider = $1
215
+ state = :provider
216
+ end
217
+ when :provider
218
+ if line =~ /^\s*version\s*=\s*"(.*)"\s*$/
219
+ providers[provider] = $1
220
+ state = :version
221
+ end
222
+ when :version
223
+ if line =~ /^\s*\}\s*$/
224
+ provider = nil
225
+ state = :root
226
+ end
227
+ else
228
+ raise ArgumentError, "File out of sync."
229
+ end
230
+ rescue ArgumentError => e
231
+ STDERR.puts "#{file}:#{i+1} #{e}"
232
+ end
233
+ end
234
+ rescue => e
235
+ STDERR.puts e
236
+ end
237
+ end
238
+ providers == {} ? 'undefined' : providers
239
+ end
240
+
126
241
  end # Collector
127
242
  end
@@ -1,3 +1,3 @@
1
1
  module Tfstats
2
- VERSION = "0.1.2"
2
+ VERSION = "1.0.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tfstats
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - stonefield
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-05-20 00:00:00.000000000 Z
11
+ date: 2022-08-26 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Collects statistics for terraform - Supports both comman line and rake
14
14
  task
@@ -24,6 +24,7 @@ files:
24
24
  - ".gitignore"
25
25
  - ".rspec"
26
26
  - ".travis.yml"
27
+ - CHANGELOG.md
27
28
  - CODE_OF_CONDUCT.md
28
29
  - Gemfile
29
30
  - Gemfile.lock
@@ -60,7 +61,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
60
61
  - !ruby/object:Gem::Version
61
62
  version: '0'
62
63
  requirements: []
63
- rubygems_version: 3.3.14
64
+ rubygems_version: 3.1.6
64
65
  signing_key:
65
66
  specification_version: 4
66
67
  summary: Collects statistics for terraform