This repository has been archived by the owner on Mar 3, 2023. It is now read-only.
forked from leominov/prometheus-devops-linter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
114 lines (98 loc) · 2.16 KB
/
main.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"errors"
"fmt"
"os"
"path"
"strings"
"github.com/leominov/prometheus-devops-linter/linter"
"github.com/prometheus/common/version"
"github.com/sirupsen/logrus"
)
const (
DefaultConfigFilename = ".prometheus-linter.yaml"
)
func DiscoverConfigFile() string {
configEnv := os.Getenv("PROM_LINTER_CONFIG")
if len(configEnv) > 0 {
return configEnv
}
dir, err := os.Getwd()
if err != nil {
dir = "./"
}
filename := path.Join(dir, DefaultConfigFilename)
if _, err := os.Stat(filename); err == nil {
return filename
}
return ""
}
func ParseArgs() (string, []string, error) {
args := os.Args
if len(args) < 1 {
return "", []string{}, errors.New("Linter type must be specified")
}
if len(args) < 2 {
return "", []string{}, errors.New("Directory must be specified")
}
linterType := strings.ToLower(args[1])
paths := args[2:]
return linterType, paths, nil
}
func HasVersionFlag() bool {
for _, arg := range os.Args {
if arg == "--version" || arg == "-v" {
return true
}
}
return false
}
func HasHelpFlag() bool {
for _, arg := range os.Args {
if arg == "--help" || arg == "-h" {
return true
}
}
return false
}
func main() {
if HasVersionFlag() {
fmt.Println(version.Info())
fmt.Println(version.BuildContext())
os.Exit(0)
}
if HasHelpFlag() {
fmt.Println("Usage:")
fmt.Println("prometheus-devops-linter jobs jobs/*.*")
fmt.Println("prometheus-devops-linter rules rules/*.*")
os.Exit(0)
}
err := ConfigureLogging(
os.Getenv("LOG_LEVEL"),
os.Getenv("LOG_FORMAT"),
)
if err != nil {
logrus.Fatal(err)
}
configFile := DiscoverConfigFile()
logrus.Infof("Starting prometheus-devops-linter %s...", version.Info())
if len(configFile) > 0 {
logrus.Infof("Configuration path: %s", configFile)
} else {
logrus.Warn("Configuration file is not defined, default configuration will be used")
}
config, err := linter.NewConfig(configFile)
if err != nil {
logrus.Fatal(err)
}
ml := linter.NewMetaLinter(config)
linterType, paths, err := ParseArgs()
if err != nil {
logrus.Fatal(err)
}
err = ml.LintFilesAs(linterType, paths)
if err != nil {
logrus.Fatal(err)
}
logrus.Info("Done without errors")
}