pronto-terraform_format 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: cdc959164cd3d2df08770f87d939291167e72348c43f79254bbb0d4bdca93455
4
+ data.tar.gz: 414787306f0bf68e4a454bc0c3c64fd26059702fbbe9c4e6747dfc477c0b8e17
5
+ SHA512:
6
+ metadata.gz: 2559b22a5b2fec7b72962c5649819c0f0e676400080a36644e43ae452ee5f775148fc2a90b748c8ba3cbc7e3644ecd20b09ba95394885bc2f5c88856fbe1de0d
7
+ data.tar.gz: 3da9bea69e1b5563ba32dcc7b162461d78ba6feefeb27eb9c09d226498092a53b349d3b79349fdd1980b09467a8af2adcfa9f3d62f861c25ebf4da72efbcf9a8
data/.rubocop.yml ADDED
@@ -0,0 +1,18 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.5
3
+
4
+ Documentation:
5
+ Enabled: false
6
+
7
+ SignalException:
8
+ EnforcedStyle: only_raise
9
+
10
+ MultilineOperationIndentation:
11
+ EnforcedStyle: indented
12
+
13
+ MultilineMethodCallIndentation:
14
+ EnforcedStyle: indented
15
+
16
+ Metrics/BlockLength:
17
+ Exclude:
18
+ - spec/**/*.rb
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ - 2.4.0
5
+ - 2.5.0
6
+ before_install:
7
+ - curl -fSL "https://releases.hashicorp.com/terraform/0.11.7/terraform_0.11.7_linux_amd64.zip" -o terraform.zip
8
+ - sudo unzip terraform.zip -d /opt/terraform
9
+ - sudo ln -s /opt/terraform/terraform /usr/bin/terraform
10
+ - rm -f terraform.zip
11
+
data/Dockerfile ADDED
@@ -0,0 +1,14 @@
1
+ FROM ruby:2.5-alpine
2
+
3
+ RUN apk add --no-cache --virtual .builddeps make cmake musl-dev gcc \
4
+ && gem install --no-document pronto-terraform_format \
5
+ && apk del .builddeps
6
+
7
+ ENV TERRAFORM_VERSION=0.11.7
8
+
9
+ RUN apk --update add ca-certificates curl
10
+ RUN curl -LOs https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip
11
+ RUN unzip terraform_${TERRAFORM_VERSION}_linux_amd64.zip -d /usr/bin/
12
+ RUN rm -rf /var/cache/apk/* terraform_${TERRAFORM_VERSION}_linux_amd64.zip
13
+
14
+ ENTRYPOINT ["pronto"]
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Andrius Janauskas
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,13 @@
1
+ # Pronto runner for terraform format
2
+
3
+ [![Build Status](https://travis-ci.org/kjmkznr/pronto-terraform_format.svg?branch=master)](https://travis-ci.org/kjmkznr/pronto-terraform_format)
4
+
5
+ Pronto runner for [Terraform](https://www.terraform.io) format, a tool to reformat Terraform standard style.
6
+
7
+ ## Prerequisite
8
+
9
+ You'll need to install terraform cli.
10
+
11
+ ## Configuration
12
+
13
+ To use an absolute path to your terraform cli, use `PRONTO_TERRAFORM_PATH`.
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env rake
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/gem_tasks'
5
+ require 'rspec/core/rake_task'
6
+
7
+ RSpec::Core::RakeTask.new(:spec)
8
+
9
+ task default: :spec
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'unified_diff'
4
+
5
+ module Pronto
6
+ module TerraformFormat
7
+ class OutputParser
8
+ def parse(file_path, output)
9
+ begin
10
+ # skip first line
11
+ diff = UnifiedDiff.parse(output.lines[3..output.lines.length].join(''))
12
+ rescue StandardError => e
13
+ puts "pronto-terraform_format ERROR: failed to parse output. #{e}"
14
+ return {}
15
+ end
16
+
17
+ result = {}
18
+ diff.chunks.each do |chunk|
19
+ file = file_path
20
+ result[file] ||= []
21
+ result[file] << {
22
+ file: file,
23
+ line: chunk.modified_range.min,
24
+ message: 'Needs to run terraform fmt'
25
+ }
26
+ end
27
+ result
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pronto
4
+ module TerraformFormat
5
+ VERSION = '0.0.1'.freeze
6
+ end
7
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'open3'
4
+ require_relative 'output_parser'
5
+
6
+ module Pronto
7
+ module TerraformFormat
8
+ class Wrapper
9
+ def initialize
10
+ @terraform_path = ENV['PRONTO_TERRAFORM_PATH'] || 'terraform'
11
+ end
12
+
13
+ def run(file_path)
14
+ stdout, stderr, = Open3.capture3("#{@terraform_path} "\
15
+ 'fmt '\
16
+ '-write=false '\
17
+ '-list=false '\
18
+ '-diff=true '\
19
+ '-check=false '\
20
+ "#{file_path}")
21
+ if stderr && !stderr.empty?
22
+ puts "WARN: pronto-terraform_format: #{file_path}: #{stderr}"
23
+ end
24
+ return [] if stdout.nil? || stdout == 0
25
+ OutputParser.new.parse(file_path, stdout)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pronto/terraform_format/version'
4
+ require_relative 'terraform_format_runner'
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pronto/terraform_format/wrapper'
4
+ require 'pronto'
5
+
6
+ module Pronto
7
+ class TerraformFormatRunner < Runner
8
+ def initialize(patches, commit = nil)
9
+ super
10
+ @inspector = ::Pronto::TerraformFormat::Wrapper.new
11
+ if ENV['PRONTO_TERRAFORM_FORMAT_FILE_EXTS'].nil?
12
+ @tf_extensions = %w[.tf .tfvars]
13
+ else
14
+ comma_separated_exts = ENV['PRONTO_TERRAFORM_FORMAT_FILE_EXTS']
15
+ @tf_extensions = comma_separated_exts.split(',').map(&:strip)
16
+ end
17
+ end
18
+
19
+ def run
20
+ return [] if !@patches || @patches.count.zero?
21
+ @patches
22
+ .select { |p| tf_file?(p.new_file_full_path) }
23
+ .map { |p| inspect(p) }
24
+ .flatten.compact
25
+ .uniq(&:line) # generate only one message per line
26
+ end
27
+
28
+ def inspect(patch)
29
+ messages = []
30
+
31
+ file_path = patch.new_file_full_path.to_s
32
+ offences = @inspector.run(file_path)
33
+ if not offences[file_path].nil?
34
+ offences[file_path].each do |offence|
35
+ messages += patch
36
+ .added_lines
37
+ .select { |line| line.new_lineno == offence[:line] }
38
+ .map { |line| new_message(offence, line) }
39
+ end
40
+ end
41
+
42
+ messages.compact
43
+ end
44
+
45
+ def tf_file?(file_path)
46
+ @tf_extensions.include? File.extname(file_path)
47
+ end
48
+
49
+ def new_message(offence, line)
50
+ Message.new(
51
+ offence[:file],
52
+ line,
53
+ :warning,
54
+ offence[:message],
55
+ nil,
56
+ self.class
57
+ )
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'pronto/terraform_format/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'pronto-terraform_format'
9
+ spec.version = Pronto::TerraformFormat::VERSION
10
+ spec.authors = ['Kazunori Kojima']
11
+ spec.email = ['kjm.kznr@gmail.com']
12
+
13
+ spec.summary = 'Pronto runner for Terraform'
14
+ spec.description = <<-EOF
15
+ A pronto runner for Terraform.
16
+ EOF
17
+ spec.homepage = 'https://github.com/kjmkznr/pronto-terraform_format'
18
+ spec.license = 'MIT'
19
+
20
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ spec.bindir = 'exe'
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ['lib']
24
+
25
+ spec.add_dependency 'pronto', '~> 0.9.0'
26
+ spec.add_dependency 'unified_diff', '~> 0.3.6'
27
+
28
+ spec.add_development_dependency 'bundler', '~> 1.16'
29
+ spec.add_development_dependency 'rake', '~> 11.0'
30
+ spec.add_development_dependency 'rspec', '~> 3.7'
31
+ spec.add_development_dependency 'rspec-its', '~> 1.2'
32
+ spec.add_development_dependency 'rubocop', '~> 0.57'
33
+ end
metadata ADDED
@@ -0,0 +1,155 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pronto-terraform_format
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kazunori Kojima
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-07-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pronto
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.9.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.9.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: unified_diff
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.3.6
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.3.6
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.16'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.16'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '11.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '11.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.7'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.7'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec-its
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.2'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.2'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.57'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.57'
111
+ description: " A pronto runner for Terraform.\n"
112
+ email:
113
+ - kjm.kznr@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".rubocop.yml"
119
+ - ".travis.yml"
120
+ - Dockerfile
121
+ - Gemfile
122
+ - LICENSE.txt
123
+ - README.md
124
+ - Rakefile
125
+ - lib/pronto/terraform_format.rb
126
+ - lib/pronto/terraform_format/output_parser.rb
127
+ - lib/pronto/terraform_format/version.rb
128
+ - lib/pronto/terraform_format/wrapper.rb
129
+ - lib/pronto/terraform_format_runner.rb
130
+ - pronto-terraform_format.gemspec
131
+ homepage: https://github.com/kjmkznr/pronto-terraform_format
132
+ licenses:
133
+ - MIT
134
+ metadata: {}
135
+ post_install_message:
136
+ rdoc_options: []
137
+ require_paths:
138
+ - lib
139
+ required_ruby_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ required_rubygems_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ requirements: []
150
+ rubyforge_project:
151
+ rubygems_version: 2.7.7
152
+ signing_key:
153
+ specification_version: 4
154
+ summary: Pronto runner for Terraform
155
+ test_files: []