-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.go
82 lines (69 loc) 路 1.7 KB
/
run.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
package cmd
import (
"fmt"
"html/template"
"os"
"github.com/brittonhayes/zero/templates"
"github.com/brittonhayes/zero/zero"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// runCmd represents the go command
var runCmd = &cobra.Command{
Use: "run",
Short: "Fetch all matching zero day feed results",
Run: func(cmd *cobra.Command, args []string) {
z := zero.Setup()
matches, err := z.ReadRSS().FindMatches()
if err != nil {
log.Error(err)
return
}
// Content results in preferred format
printf(viper.GetBool("json"), matches)
// Run in CI mode if enabled
exitCheck(viper.GetBool("check"), len(matches))
},
}
func printf(enabled bool, m zero.Matches) {
if enabled {
b, err := m.MarshalJSON()
if err != nil {
log.Error(err)
}
fmt.Println(string(b))
return
}
log.WithFields(log.Fields{
"STATUS": "Complete",
}).Debug()
tpl, err := template.New("zero").Parse(templates.DefaultTemplate)
if err != nil {
log.Error(err)
return
}
err = tpl.Execute(os.Stdout, m)
if err != nil {
log.Error(err)
return
}
}
func exitCheck(enabled bool, count int) {
if enabled && count > 0 {
log.WithFields(log.Fields{
"STATUS": "failure",
"MATCHES": count,
"MESSAGE": "failed continuous integration check",
}).Error()
log.Exit(1)
}
}
func init() {
rootCmd.AddCommand(runCmd)
runCmd.Flags().StringP("template", "t", "", "render printf with custom template")
runCmd.Flags().BoolP("json", "j", false, "render printf as raw JSON")
runCmd.Flags().Bool("check", false, "Exit with status 1 if results are found")
_ = viper.BindPFlag("check", runCmd.Flags().Lookup("check"))
_ = viper.BindPFlag("json", runCmd.Flags().Lookup("json"))
}