Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

creating yaml-lint cmd #3681

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions cmd/yaml-lint/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
Copyright 2020 The Kubernetes 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 cmd

import (
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"k8s.io/release/pkg/gcp/build"
"sigs.k8s.io/release-utils/log"
)

var rootCmd = &cobra.Command{
Short: "yaml-lint → A tool for linting yaml directories",
Long: `yaml-lint → A tool for linting yaml directories

This tool lets software developers lint yaml directories.

`,
Use: "yaml-lint",
SilenceUsage: true,
SilenceErrors: true,
PersistentPreRunE: initLogging,
RunE: func(cmd *cobra.Command, args []string) error {
return run()
},
}

type commandLineOptions struct {
directory string
}

var commandLineOpts = &commandLineOptions{}

func init() {
rootCmd.PersistentFlags().StringVarP(
&commandLineOpts.directory,
"directory",
"d",
"",
"directory path where yaml files to be linted",
)
}

// Execute builds the command.
func Execute() {
if err := rootCmd.Execute(); err != nil {
logrus.Fatal(err)
}
}

func run() error {
prepareBuildErr := validateYAMLFilesInDirectory(commandLineOpts.directory)

Check failure on line 66 in cmd/yaml-lint/cmd/root.go

View workflow job for this annotation

GitHub Actions / lint

undefined: validateYAMLFilesInDirectory
if prepareBuildErr != nil {
return prepareBuildErr
}

buildErrors := build.RunBuildJobs(buildOpts)

Check failure on line 71 in cmd/yaml-lint/cmd/root.go

View workflow job for this annotation

GitHub Actions / lint

undefined: buildOpts
if len(buildErrors) != 0 {
logrus.Fatalf("Failed to run some build jobs: %v", buildErrors)
}
logrus.Info("Finished.")

return nil
}

func initLogging(*cobra.Command, []string) error {
return log.SetupGlobalLogger(rootOpts.logLevel)

Check failure on line 81 in cmd/yaml-lint/cmd/root.go

View workflow job for this annotation

GitHub Actions / lint

undefined: rootOpts (typecheck)
}
25 changes: 25 additions & 0 deletions cmd/yaml-lint/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
Copyright 2020 The Kubernetes 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 main

import (
"k8s.io/release/cmd/publish-release/cmd"
)

func main() {
cmd.Execute()
}
15 changes: 15 additions & 0 deletions cmd/yaml-lint/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/sh
set -Eeuo pipefail

#export directory to scan for yaml linting issues to env var DIR_TO_LINT
YAML_FILES_TO_LINT=$(find $DIR_TO_LINT -type f \( -name "*.yaml" -o -name "*.yml" \))
if [ -n "$YAML_FILES_TO_LINT" ]; then
echo "Validating yaml files: \n${YAML_FILES_TO_LINT}"
OUTPUT=$(go run ./validate_yaml.go $YAML_FILES_TO_LINT)
if [[ "$OUTPUT" == *"YAML Validation Failed"* ]]; then
echo "Yaml linting issues:"
echo "$OUTPUT"
fi
else
echo "No YAML linting issue for $YAML_FILES_TO_LINT"
fi
82 changes: 82 additions & 0 deletions pkg/yamllint/yamllint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package yamllint

import (
"fmt"
"os"
"path/filepath"
"strings"

"sigs.k8s.io/yaml"
)

// validateYAML reads and parses the YAML file at the given path.
func validateYAML(filePath string) string {
data, err := os.ReadFile(filePath)
if err != nil {
return fmt.Sprintf("Error reading %s:\n%s", filePath, err.Error())
}

var content interface{}
if err := yaml.UnmarshalStrict(data, &content); err != nil {
return fmt.Sprintf("Error in %s:\n%s", filePath, err.Error())
}

return "Valid"
}

func FindYAMLFiles(dirPath string) ([]string, error) {
var yamlFiles []string

err := filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() && (strings.HasSuffix(info.Name(), ".yaml") || strings.HasSuffix(info.Name(), ".yml")) {
yamlFiles = append(yamlFiles, path)
}
return nil
})

return yamlFiles, err
}

// validateYAMLFilesInDirectory finds and validates all YAML files in the given directory.
func validateYAMLFilesInDirectory(dirPath string) {
files, err := FindYAMLFiles(dirPath)
if err != nil {
fmt.Printf("Error finding YAML files in %s:\n%s", dirPath, err.Error())
return
}

if len(files) == 0 {
fmt.Println("No YAML files found.")
return
}
invalidFiles := []string{}
for _, file := range files {
result := validateYAML(file)
if result != "Valid" {
invalidFiles = append(invalidFiles, result)
}
}

if len(invalidFiles) > 0 {
fmt.Println("YAML Validation Failed:\n")
for _, errorMsg := range invalidFiles {
fmt.Println(errorMsg)
fmt.Println("\n" + strings.Repeat("-", 40) + "\n")
}
} else {
fmt.Println("All YAML files are valid.")
}
}

// func main() {
// if len(os.Args) < 2 {
// fmt.Println("No directory specified.")
// os.Exit(1)
// }

// dirPath := os.Args[1]
// validateYAMLFilesInDirectory(dirPath)
// }
Loading