-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix version injection due to move to goreleaser
- Loading branch information
Showing
3 changed files
with
122 additions
and
156 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,127 @@ | ||
package main | ||
|
||
import "github.com/instrumenta/kubeval/cmd" | ||
import ( | ||
"bufio" | ||
"bytes" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"runtime" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
|
||
"github.com/instrumenta/kubeval/kubeval" | ||
"github.com/instrumenta/kubeval/log" | ||
) | ||
|
||
var ( | ||
version = "dev" | ||
commit = "none" | ||
date = "unknown" | ||
) | ||
|
||
// RootCmd represents the the command to run when kubeval is run | ||
var RootCmd = &cobra.Command{ | ||
Use: "kubeval <file> [file...]", | ||
Short: "Validate a Kubernetes YAML file against the relevant schema", | ||
Long: `Validate a Kubernetes YAML file against the relevant schema`, | ||
Version: fmt.Sprintf("Version: %s\nCommit: %s\nDate: %s\n", version, commit, date), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
success := true | ||
windowsStdinIssue := false | ||
stat, err := os.Stdin.Stat() | ||
if err != nil { | ||
// Stat() will return an error on Windows in both Powershell and | ||
// console until go1.9 when nothing is passed on stdin. | ||
// See https://github.com/golang/go/issues/14853. | ||
if runtime.GOOS != "windows" { | ||
log.Error(err) | ||
os.Exit(1) | ||
} else { | ||
windowsStdinIssue = true | ||
} | ||
} | ||
// We detect whether we have anything on stdin to process if we have no arguments | ||
// or if the argument is a - | ||
if (len(args) < 1 || args[0] == "-") && !windowsStdinIssue && ((stat.Mode() & os.ModeCharDevice) == 0) { | ||
var buffer bytes.Buffer | ||
scanner := bufio.NewScanner(os.Stdin) | ||
for scanner.Scan() { | ||
buffer.WriteString(scanner.Text() + "\n") | ||
} | ||
results, err := kubeval.Validate(buffer.Bytes(), viper.GetString("filename")) | ||
if err != nil { | ||
log.Error(err) | ||
os.Exit(1) | ||
} | ||
success = logResults(results, success) | ||
} else { | ||
if len(args) < 1 { | ||
log.Error("You must pass at least one file as an argument") | ||
os.Exit(1) | ||
} | ||
for _, fileName := range args { | ||
filePath, _ := filepath.Abs(fileName) | ||
fileContents, err := ioutil.ReadFile(filePath) | ||
if err != nil { | ||
log.Error("Could not open file", fileName) | ||
os.Exit(1) | ||
} | ||
results, err := kubeval.Validate(fileContents, fileName) | ||
if err != nil { | ||
log.Error(err) | ||
os.Exit(1) | ||
} | ||
success = logResults(results, success) | ||
} | ||
} | ||
if !success { | ||
os.Exit(1) | ||
} | ||
}, | ||
} | ||
|
||
func logResults(results []kubeval.ValidationResult, success bool) bool { | ||
for _, result := range results { | ||
if len(result.Errors) > 0 { | ||
success = false | ||
log.Warn("The document", result.FileName, "contains an invalid", result.Kind) | ||
for _, desc := range result.Errors { | ||
log.Info("--->", desc) | ||
} | ||
} else if result.Kind == "" { | ||
log.Success("The document", result.FileName, "is empty") | ||
} else { | ||
log.Success("The document", result.FileName, "contains a valid", result.Kind) | ||
} | ||
} | ||
return success | ||
} | ||
|
||
// Execute adds all child commands to the root command sets flags appropriately. | ||
// This is called by main.main(). It only needs to happen once to the rootCmd. | ||
func Execute() { | ||
if err := RootCmd.Execute(); err != nil { | ||
log.Error(err) | ||
os.Exit(-1) | ||
} | ||
} | ||
|
||
func init() { | ||
viper.SetEnvPrefix("KUBEVAL") | ||
viper.AutomaticEnv() | ||
RootCmd.Flags().StringVarP(&kubeval.Version, "kubernetes-version", "v", "master", "Version of Kubernetes to validate against") | ||
RootCmd.Flags().StringVarP(&kubeval.SchemaLocation, "schema-location", "", kubeval.DefaultSchemaLocation, "Base URL used to download schemas. Can also be specified with the environment variable KUBEVAL_SCHEMA_LOCATION") | ||
RootCmd.Flags().BoolVarP(&kubeval.OpenShift, "openshift", "", false, "Use OpenShift schemas instead of upstream Kubernetes") | ||
RootCmd.Flags().BoolVarP(&kubeval.Strict, "strict", "", false, "Disallow additional properties not in schema") | ||
RootCmd.SetVersionTemplate(`{{.Version}}`) | ||
viper.BindPFlag("schema_location", RootCmd.Flags().Lookup("schema-location")) | ||
RootCmd.PersistentFlags().StringP("filename", "f", "stdin", "filename to be displayed when testing manifests read from stdin") | ||
viper.BindPFlag("filename", RootCmd.PersistentFlags().Lookup("filename")) | ||
} | ||
|
||
func main() { | ||
cmd.Execute() | ||
Execute() | ||
} |