forked from xm1k3/cent
/
validate.go
70 lines (60 loc) · 1.44 KB
/
validate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sync"
"github.com/spf13/cobra"
"github.com/abd-4fg/cent/pkg/jobs"
)
// validateCmd represents the validate command
var validateCmd = &cobra.Command{
Use: "validate",
Short: "Validate templates",
Long: `The validate command is a part of the application's functionality to validate templates.
When executed, it scans a specified folder for YAML files. Each YAML file is checked for validity.
If a template is found to be invalid, it is deleted from the folder.`,
Run: func(cmd *cobra.Command, args []string) {
path, _ := rootCmd.Flags().GetString("path")
var wg sync.WaitGroup
filepath.Walk(path, func(filePath string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
} else if filepath.Ext(filePath) == ".yaml" {
wg.Add(1)
go func(filePath string) {
defer wg.Done()
data, err := readFile(filePath)
if err != nil {
return
}
isValid, err := jobs.ValidateTemplate(data)
if err != nil {
fmt.Println(filePath, err)
}
if !isValid {
os.Remove(filePath)
}
}(filePath)
}
return nil
})
wg.Wait()
},
}
func readFile(filePath string) (string, error) {
data, err := ioutil.ReadFile(filePath)
if err != nil {
return "", err
}
return string(data), nil
}
func init() {
rootCmd.AddCommand(validateCmd)
}