com.deviceinsight.helm.LintMojo.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of helm-maven-plugin Show documentation
Show all versions of helm-maven-plugin Show documentation
A Maven Plugin for Helm Charts
/*
* Copyright 2018-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.deviceinsight.helm
import org.apache.maven.plugin.MojoExecutionException
import org.apache.maven.plugins.annotations.LifecyclePhase
import org.apache.maven.plugins.annotations.Mojo
import org.apache.maven.plugins.annotations.Parameter
import java.io.File
@Mojo(name = "lint", defaultPhase = LifecyclePhase.PRE_INTEGRATION_TEST)
class LintMojo : AbstractHelmMojo() {
/**
* An optional values.yaml file that is used to run linting, relative to `${project.basedir}`.
*/
@Parameter(property = "valuesFile", required = false)
private var valuesFile: String? = null
@Parameter(property = "strictLint", required = false, defaultValue = "false")
private var strictLint: Boolean = false
@Parameter(property = "helm.skip", defaultValue = "false")
private var skip: Boolean = false
@Throws(MojoExecutionException::class)
override fun execute() {
if (skip) {
log.info("helm-lint has been skipped")
return
}
try {
if (!isChartFolderPresent()) {
log.warn("No sources found skipping helm lint.")
return
}
val helm = resolveHelmBinary()
val command = mutableListOf(helm, "lint", chartName())
if (strictLint) {
command.add("--strict")
}
if (valuesFile != null) {
command.add("--values")
command.add(quoteFilePath(project.basedir.resolve(valuesFile!!).absolutePath))
}
executeCommand(command)
} catch (e: Exception) {
throw MojoExecutionException("Error rendering helm lint: ${e.message}", e)
}
}
private fun executeCommand(command: List, directory: File = target()) {
val proc = ProcessBuilder(command)
.directory(directory)
.redirectOutput(ProcessBuilder.Redirect.PIPE)
.redirectErrorStream(true)
.start()
proc.waitFor()
log.debug("When executing '${command.joinToString(" ")}' in '${directory.absolutePath}', " +
"result was ${proc.exitValue()}")
proc.inputStream.bufferedReader().lines().forEach {
log.info("Output: $it")
}
if (proc.exitValue() != 0) {
throw RuntimeException(
"When executing '${command.joinToString(" ")}' got result code '${proc.exitValue()}'")
}
}
}