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

add support for json/yaml output to list #35

Merged
merged 3 commits into from
Jan 31, 2023
Merged
Changes from 2 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
62 changes: 59 additions & 3 deletions cmd/promlinter/main.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package main

import (
"encoding/json"
"fmt"
"go/ast"
"go/parser"
"go/token"
"gopkg.in/alecthomas/kingpin.v2"
"os"
"path/filepath"
"strings"
"text/tabwriter"

"gopkg.in/alecthomas/kingpin.v2"

"github.com/yeya24/promlinter"
)

Expand Down Expand Up @@ -58,6 +60,7 @@ func main() {
Default("false").Short('s').Bool()
listPrintAddPos := listCmd.Flag("add-position", "Add metric position column when printing the result.").Default("false").Bool()
listPrintAddHelp := listCmd.Flag("add-help", "Add metric help column when printing the result.").Default("false").Bool()
listPrintJSON := listCmd.Flag("json", "Print result as json.").Default("false").Bool()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of a bool for printing out using json format, what do you think about having an output flag as a string to be extensible to support different formats like json, markdown, etc


lintCmd := app.Command("lint", "Lint metrics via promlint.")
lintPaths := lintCmd.Arg("files", "Files to parse metrics.").Strings()
Expand All @@ -74,7 +77,7 @@ func main() {
switch parsedCmd {
case listCmd.FullCommand():
metrics := promlinter.RunList(fileSet, findFiles(*listPaths, fileSet), *listStrict)
printMetrics(metrics, *listPrintAddPos, *listPrintAddHelp)
printMetrics(metrics, *listPrintAddPos, *listPrintAddHelp, *listPrintJSON)
case lintCmd.FullCommand():
setting := promlinter.Setting{Strict: *lintStrict, DisabledLintFuncs: *disableLintFuncs}
for _, iss := range promlinter.RunLint(fileSet, findFiles(*lintPaths, fileSet), setting) {
Expand Down Expand Up @@ -127,7 +130,11 @@ func walkDir(root string) chan string {
return out
}

func printMetrics(metrics []promlinter.MetricFamilyWithPos, addPosition, addHelp bool) {
func printMetrics(metrics []promlinter.MetricFamilyWithPos, addPosition, addHelp, asJson bool) {
if asJson {
printAsJson(metrics)
return
}
tw := tabwriter.NewWriter(os.Stdout, 20, 1, 3, ' ', 0)
defer tw.Flush()

Expand Down Expand Up @@ -156,3 +163,52 @@ func printMetrics(metrics []promlinter.MetricFamilyWithPos, addPosition, addHelp
}
}
}

func printAsJson(metrics []promlinter.MetricFamilyWithPos) {

b, err := json.Marshal(toPrint(metrics))
if err != nil {
fmt.Printf("Failed: %v", err)
os.Exit(1)
}
fmt.Print(string(b))
}

type MetricForPrinting struct {
Name string
Help string
Type string
Filename string
Line int
Column int
}

func toPrint(metrics []promlinter.MetricFamilyWithPos) []MetricForPrinting {
p := []MetricForPrinting{}
for _, m := range metrics {
if m.MetricFamily != nil && *m.MetricFamily.Name != "" {
if m.MetricFamily.Type == nil {
continue
}
n := ""
h := ""

if m.MetricFamily.Name != nil {
n = *m.MetricFamily.Name
}
if m.MetricFamily.Help != nil {
h = *m.MetricFamily.Help
}
i := MetricForPrinting{
Name: n,
Help: h,
Type: MetricType[int32(*m.MetricFamily.Type)],
Filename: m.Pos.Filename,
Line: m.Pos.Line,
Column: m.Pos.Column,
}
p = append(p, i)
}
}
return p
}