tfstats 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 70b3d2ba865aef7602725a1d6455bcb4d57631c8354c71de7365a54ace5bd7d3
4
+ data.tar.gz: 653a5d4b7170b20e7eb7ca495fc1effde5c93971b120cee6cc133f0ad64ea786
5
+ SHA512:
6
+ metadata.gz: 9d17b69b8c47a8c795fc3ec43303c6a8936815b2c64143a95a59fa7f1203bd5cbdafb8d1081a3f3abfe6a6e2fec00c59f283d8bb2b23319593ab84a07c251efc
7
+ data.tar.gz: 64e81819a34481020ae918c35dbaad0325ef761dc414a95dd24f08af69b258eba25aec78d8ffa802624ab16a35535f878cc567260037e6c3d86369e7847f4394
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.5.8
6
+ before_install: gem install bundler -v 2.1.4
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at knut.stenmark@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in tfstats.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,34 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ tfstats (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.4.4)
10
+ rake (12.3.3)
11
+ rspec (3.10.0)
12
+ rspec-core (~> 3.10.0)
13
+ rspec-expectations (~> 3.10.0)
14
+ rspec-mocks (~> 3.10.0)
15
+ rspec-core (3.10.1)
16
+ rspec-support (~> 3.10.0)
17
+ rspec-expectations (3.10.1)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.10.0)
20
+ rspec-mocks (3.10.2)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.10.0)
23
+ rspec-support (3.10.3)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ rake (~> 12.0)
30
+ rspec (~> 3.0)
31
+ tfstats!
32
+
33
+ BUNDLED WITH
34
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 knut
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,91 @@
1
+ # Tfstats - Terraform Statistics
2
+
3
+ This gem report code statistics (KLOCs, etc) from the terraform in current or specified directory. It also support recursion from a start point.
4
+
5
+ Supported:
6
+
7
+ * `tfstats` - command line
8
+ * `rake stats:terraform` - as a rake task
9
+
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'tfstats'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle install
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install tfstats
26
+
27
+
28
+ ### Installing rake task
29
+
30
+ In your Rakefile add the following:
31
+
32
+ ```rake
33
+ require 'rake/tfstats_task'
34
+ Rake::TfstatsTask.new do |task|
35
+ task.directory = "."
36
+ task.filespec = "*.tf"
37
+ task.recursive = false
38
+ task.tabseparated = false
39
+ end
40
+ ```
41
+
42
+ All parameters are optional. The above shows the default:
43
+
44
+ | Parameter | Description | Example |
45
+ |---|---|---|
46
+ | `directory` | Specify the directory to run from. The directory specified is relative to current directory | `task.directory = "terraform"` |
47
+ | `filespec` | You can specify a different filespec. | `task.filespec = "*.{tf,sh,erb,tpl}"` |
48
+ | `recursive` | Set to true to operate recursively on sub-directories. If no files are found in the directory, it will not be listed. | `task.recursive = true` |
49
+ | `tabseparated` | Output as a tab separated file and not as tabular text | `task.tabseparated = true` |
50
+
51
+ ## Usage
52
+
53
+ ### Run directly from command line
54
+
55
+ ```bash
56
+ Usage: tfstats [options] [directory]
57
+
58
+ Commands:
59
+ -r / recursive : run recursive.
60
+ -f <filespec> : specify filespec. Defaults to '*.tf' (use single quote!)
61
+ -v / verbose : Output debug information
62
+ -t / tab : Tab separated output
63
+
64
+ If no directory is specified, statistics is collected from current directory
65
+ ```
66
+
67
+
68
+ ### Run as a rake task
69
+
70
+ ```bash
71
+ rake stats:terraform
72
+ ```
73
+
74
+ ## Development
75
+
76
+ 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.
77
+
78
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
79
+
80
+ ## Contributing
81
+
82
+ Bug reports and pull requests are welcome on GitHub at https://github.com/stonefield/tfstats. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/stonefield/tfstats/blob/master/CODE_OF_CONDUCT.md).
83
+
84
+
85
+ ## License
86
+
87
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
88
+
89
+ ## Code of Conduct
90
+
91
+ Everyone interacting in the Tfstats project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/stonefield/tfstats/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "tfstats"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/bin/tfstats ADDED
@@ -0,0 +1,54 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH << File.expand_path('../../lib', __FILE__)
4
+ require 'tfstats'
5
+
6
+ def main
7
+ #STDERR.puts "Called with: #{ARGV.inspect}"
8
+ recursive = false
9
+ filespec = "*.tf"
10
+ directory = '.'
11
+ tabseparated = false
12
+ while arg = ARGV.shift
13
+ case arg
14
+ when 'help', '--help', '-h'
15
+ help
16
+ when '-r', 'recursive'
17
+ recursive = true
18
+ when '-f'
19
+ filespec = ARGV.shift
20
+ when '-v', 'verbose'
21
+ Tfstats.verbose = true
22
+ when '-t', 'tab'
23
+ tabseparated = true
24
+ else
25
+ directory = arg
26
+ end
27
+ end
28
+
29
+ Tfstats::Collector.collect(directory, filespec, recursive, tabseparated)
30
+ end
31
+
32
+ def help
33
+ puts HELP_TEXT
34
+ exit 3
35
+ end
36
+
37
+ HELP_TEXT = <<-DOC
38
+
39
+ This program collects terraform statistics.
40
+ You can also include this in projects with a rake task.
41
+
42
+ Usage: #{__FILE__.sub(/.*\//,'')} [options] [directory]
43
+
44
+ Commands:
45
+ -r / recursive : run recursive.
46
+ -f <filespec> : specify filespec. Defaults to '*.tf' (use single quote!)
47
+ -v / verbose : Output debug information
48
+ -t / tab : Tab separated output
49
+
50
+ If no directory is specified, statistics is collected from current directory
51
+
52
+ DOC
53
+
54
+ main
@@ -0,0 +1,26 @@
1
+ require 'tfstats'
2
+
3
+ class Rake::TfstatsTask < Rake::TaskLib
4
+ attr_accessor :recursive, :filespec, :directory, :tabseparated, :verbose
5
+
6
+ def initialize()
7
+ self.recursive = false
8
+ self.filespec = "*.tf"
9
+ self.directory = '.'
10
+ self.tabseparated = false
11
+ self.verbose = false
12
+ yield self if block_given?
13
+ self.define
14
+ end
15
+
16
+
17
+ def define
18
+ namespace :stats do
19
+ desc 'Display terraform statistics'
20
+ task :terraform do
21
+ Tfstats.verbose = self.verbose
22
+ Tfstats::Collector.collect(directory, filespec, recursive, tabseparated)
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,127 @@
1
+ #encoding: UTF-8
2
+ Encoding.default_external = Encoding::UTF_8
3
+ require "fileutils"
4
+ module Tfstats
5
+ class Collector
6
+ class << self
7
+ def collect(directory, filespec, recursive, tabseparated)
8
+ statistics = process_dir(directory, filespec, recursive)
9
+ puts output( statistics, tabseparated)
10
+ end
11
+
12
+ def process_dir(directory, filespec, recursive)
13
+ statistics = {}
14
+ basename = File.basename File.expand_path(directory)
15
+ info "directory: #{directory}"
16
+ info "basename: #{basename}"
17
+ info "filespec: #{filespec}"
18
+ info "recursive: #{recursive}"
19
+ if recursive
20
+ Dir.glob("#{directory}/**/").each do |dir|
21
+ info "traversing: #{dir}"
22
+ placeholder = dir.sub(/^#{Regexp.escape(directory.to_s)}/, basename).chop
23
+ statistics[placeholder] = new(dir, filespec).collect
24
+ end
25
+ else
26
+ statistics[basename] = new(directory,filespec).collect
27
+ end
28
+ statistics
29
+ end
30
+
31
+ def output(statistics, tabseparated)
32
+ str = ''
33
+ if tabseparated
34
+ pattern = (["%s"] * 8).join("\t") + + "\n"
35
+ separator = ''
36
+ else
37
+ max_name = statistics.keys.max_by { |s| s.size }.size
38
+ pattern = "| %-#{max_name}.#{max_name}s |" + " %9.9s |" * 7 + "\n"
39
+ separator = '+-'+ ('-' * max_name) + '-+' + ('-' * 11 + '+') * 7 + "\n"
40
+ end
41
+ fields = %i(files modules resources data variables lines loc)
42
+ str << separator
43
+ str << (pattern % %w(Directory Files modules resources data variables Lines LOC))
44
+ str << separator
45
+ statistics.each do |dir, filespecs|
46
+ t = statistics[dir]
47
+ unless empty?(t)
48
+ str << (pattern % ([dir ] + t.values_at(*fields) ))
49
+ end
50
+ end
51
+ str << separator
52
+ str << (pattern % (["Total"] + sum(statistics).values_at(*fields) ))
53
+ str << separator
54
+ str
55
+ end
56
+
57
+ def sum(statistics)
58
+ s = {}
59
+ s.default = 0
60
+ statistics.each do |k, stats|
61
+ stats.each do |k,v|
62
+ s[k] += v
63
+ end
64
+ end
65
+ s
66
+ end
67
+
68
+ def empty?(stats)
69
+ stats.values.sum == 0
70
+ end
71
+
72
+ def info(msg)
73
+ if Tfstats.verbose
74
+ puts msg
75
+ end
76
+ end
77
+ end # << self
78
+
79
+ attr_accessor :stats, :directory, :filespec
80
+
81
+ def initialize(directory, filespec)
82
+ @directory, @filespec = directory, filespec
83
+ @stats = {}
84
+ @stats.default = 0
85
+ end
86
+
87
+ def info(msg)
88
+ self.class.info msg
89
+ end
90
+
91
+ def collect
92
+ spec = File.join(directory, filespec)
93
+ info "Fetching data from: #{spec}"
94
+ Dir[spec].each do |file|
95
+ info "Reading file: #{file}"
96
+ @stats[:files] += 1
97
+ begin
98
+ File.read(file).each_line.with_index do |line,i|
99
+ begin
100
+ case line
101
+ when /^\s*#/, /^\s*$/
102
+ when /^\s*resource/
103
+ @stats[:resources] += 1
104
+ when /^\s*module/
105
+ @stats[:modules] += 1
106
+ when /^\s*variable/
107
+ @stats[:variables] += 1
108
+ when /^\s*data/
109
+ @stats[:data] += 1
110
+ else
111
+ @stats[:loc] += 1
112
+ end
113
+ rescue ArgumentError => e
114
+ STDERR.puts "#{file}:#{i+1} #{e}"
115
+ end
116
+ @stats[:lines] += 1
117
+ end
118
+ rescue => e
119
+ STDERR.puts e
120
+ end
121
+
122
+ end
123
+ @stats
124
+ end
125
+
126
+ end # Collector
127
+ end
@@ -0,0 +1,3 @@
1
+ module Tfstats
2
+ VERSION = "0.1.0"
3
+ end
data/lib/tfstats.rb ADDED
@@ -0,0 +1,15 @@
1
+ require "tfstats/version"
2
+ require "tfstats/collector"
3
+
4
+ module Tfstats
5
+ class Error < StandardError; end
6
+ # Your code goes here...
7
+ %i(dryrun verbose).each do |key|
8
+ self.class.define_method(key) do
9
+ instance_variable_get(:"@#{key}")
10
+ end
11
+ self.class.define_method(:"#{key}=") do |value|
12
+ instance_variable_set(:"@#{key}", value)
13
+ end
14
+ end
15
+ end
data/tfstats.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ require_relative 'lib/tfstats/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "tfstats"
5
+ spec.version = Tfstats::VERSION
6
+ spec.authors = ["stonefield"]
7
+ spec.email = ["knut.stenmark@gmail.com"]
8
+
9
+ spec.summary = %q{Collects statistics for terraform}
10
+ spec.description = %q{Supports both comman line and rake task}
11
+ spec.homepage = "https://github.com/stonefield/tfstats"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ #spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/stonefield/tfstats"
19
+ spec.metadata["changelog_uri"] = "https://github.com/stonefield/tfstats/master/CHANGELOG.md"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+ spec.bindir = "bin"
27
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tfstats
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - stonefield
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-12-02 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Supports both comman line and rake task
14
+ email:
15
+ - knut.stenmark@gmail.com
16
+ executables:
17
+ - console
18
+ - setup
19
+ - tfstats
20
+ extensions: []
21
+ extra_rdoc_files: []
22
+ files:
23
+ - ".gitignore"
24
+ - ".rspec"
25
+ - ".travis.yml"
26
+ - CODE_OF_CONDUCT.md
27
+ - Gemfile
28
+ - Gemfile.lock
29
+ - LICENSE.txt
30
+ - README.md
31
+ - Rakefile
32
+ - bin/console
33
+ - bin/setup
34
+ - bin/tfstats
35
+ - lib/rake/tfstats_task.rb
36
+ - lib/tfstats.rb
37
+ - lib/tfstats/collector.rb
38
+ - lib/tfstats/version.rb
39
+ - tfstats.gemspec
40
+ homepage: https://github.com/stonefield/tfstats
41
+ licenses:
42
+ - MIT
43
+ metadata:
44
+ homepage_uri: https://github.com/stonefield/tfstats
45
+ source_code_uri: https://github.com/stonefield/tfstats
46
+ changelog_uri: https://github.com/stonefield/tfstats/master/CHANGELOG.md
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: 2.3.0
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubygems_version: 3.1.6
63
+ signing_key:
64
+ specification_version: 4
65
+ summary: Collects statistics for terraform
66
+ test_files: []