Skip to content

Commit

Permalink
feat: report and spectral-report accept stdin and stdout #220
Browse files Browse the repository at this point in the history
the machine readable features should operate correctly as a pipeable tool. New flags `-i` for use stdin and `-o`
  • Loading branch information
daveshanley committed Dec 30, 2022
1 parent 2943321 commit e3edfc0
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 16 deletions.
61 changes: 52 additions & 9 deletions cmd/spectral_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
package cmd

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"github.com/daveshanley/vacuum/model"
"github.com/daveshanley/vacuum/motor"
"github.com/daveshanley/vacuum/rulesets"
Expand All @@ -17,7 +19,7 @@ import (

func GetSpectralReportCommand() *cobra.Command {

return &cobra.Command{
cmd := &cobra.Command{
SilenceUsage: true,
SilenceErrors: true,
Use: "spectral-report",
Expand All @@ -37,17 +39,24 @@ func GetSpectralReportCommand() *cobra.Command {
},
RunE: func(cmd *cobra.Command, args []string) error {

PrintBanner()
stdIn, _ := cmd.Flags().GetBool("stdin")
stdOut, _ := cmd.Flags().GetBool("stdout")

if !stdIn && !stdOut {
PrintBanner()
}

// check for file args
if len(args) == 0 {
errText := "please supply an OpenAPI specification to generate a report"
if !stdIn && len(args) == 0 {
errText := "please supply an OpenAPI specification to generate a spectral report, or use " +
"the -i flag to use stdin"
pterm.Error.Println(errText)
pterm.Println()
return errors.New(errText)
}

timeFlag, _ := cmd.Flags().GetBool("time")
noPretty, _ := cmd.Flags().GetBool("no-pretty")

reportOutput := "vacuum-spectral-report.json"

Expand All @@ -57,8 +66,20 @@ func GetSpectralReportCommand() *cobra.Command {

start := time.Now()

// read file.
specBytes, fileError := os.ReadFile(args[0])
var specBytes []byte
var fileError error

if stdIn {
// read file from stdin
inputReader := cmd.InOrStdin()
buf := &bytes.Buffer{}
_, fileError = buf.ReadFrom(inputReader)
specBytes = buf.Bytes()

} else {
// read file from filesystem
specBytes, fileError = os.ReadFile(args[0])
}

if fileError != nil {
pterm.Error.Printf("Unable to read file '%s': %s\n", args[0], fileError.Error())
Expand Down Expand Up @@ -94,7 +115,9 @@ func GetSpectralReportCommand() *cobra.Command {
}
}

pterm.Info.Printf("Linting against %d rules: %s\n", len(selectedRS.Rules), selectedRS.DocumentationURI)
if !stdIn && !stdOut {
pterm.Info.Printf("Linting against %d rules: %s\n", len(selectedRS.Rules), selectedRS.DocumentationURI)
}

ruleset := motor.ApplyRulesToRuleSet(&motor.RuleSetExecution{
RuleSet: selectedRS,
Expand All @@ -107,10 +130,26 @@ func GetSpectralReportCommand() *cobra.Command {

duration := time.Since(start)

var source string
if stdIn {
source = "stdin"
} else {
source = args[0] // todo: convert to full path.
}
// serialize
spectralReport := resultSet.GenerateSpectralReport(args[0]) // todo: convert to full path.
spectralReport := resultSet.GenerateSpectralReport(source)

data, _ := json.MarshalIndent(spectralReport, "", " ")
var data []byte
if noPretty {
data, _ = json.Marshal(spectralReport)
} else {
data, _ = json.MarshalIndent(spectralReport, "", " ")
}

if stdOut {
fmt.Print(string(data))
return nil
}

err := os.WriteFile(reportOutput, data, 0664)

Expand All @@ -129,5 +168,9 @@ func GetSpectralReportCommand() *cobra.Command {
return nil
},
}
cmd.Flags().BoolP("stdin", "i", false, "Use stdin as input, instead of a file")
cmd.Flags().BoolP("stdout", "o", false, "Use stdout as output, instead of a file")
cmd.Flags().BoolP("no-pretty", "n", false, "Render JSON with no formatting")
return cmd

}
39 changes: 32 additions & 7 deletions cmd/vacuum_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,16 @@ func GetVacuumReportCommand() *cobra.Command {
},
RunE: func(cmd *cobra.Command, args []string) error {

PrintBanner()
stdIn, _ := cmd.Flags().GetBool("stdin")
stdOut, _ := cmd.Flags().GetBool("stdout")

if !stdIn && !stdOut {
PrintBanner()
}

// check for file args
if len(args) == 0 {
errText := "please supply an OpenAPI specification to generate a report"
if !stdIn && len(args) == 0 {
errText := "please supply an OpenAPI specification to generate a report, or use the -i flag to use stdin"
pterm.Error.Println(errText)
pterm.Println()
return errors.New(errText)
Expand All @@ -63,8 +68,20 @@ func GetVacuumReportCommand() *cobra.Command {

start := time.Now()

// read file.
specBytes, fileError := os.ReadFile(args[0])
var specBytes []byte
var fileError error

if stdIn {
// read file from stdin
inputReader := cmd.InOrStdin()
buf := &bytes.Buffer{}
_, fileError = buf.ReadFrom(inputReader)
specBytes = buf.Bytes()

} else {
// read file from filesystem
specBytes, fileError = os.ReadFile(args[0])
}

if fileError != nil {
pterm.Error.Printf("Unable to read file '%s': %s\n", args[0], fileError.Error())
Expand Down Expand Up @@ -99,7 +116,9 @@ func GetVacuumReportCommand() *cobra.Command {
}
}

pterm.Info.Printf("Linting against %d rules: %s\n", len(selectedRS.Rules), selectedRS.DocumentationURI)
if !stdIn && !stdOut {
pterm.Info.Printf("Linting against %d rules: %s\n", len(selectedRS.Rules), selectedRS.DocumentationURI)
}

ruleset := motor.ApplyRulesToRuleSet(&motor.RuleSetExecution{
RuleSet: selectedRS,
Expand Down Expand Up @@ -137,6 +156,11 @@ func GetVacuumReportCommand() *cobra.Command {

reportData := data

if stdOut {
fmt.Print(string(reportData))
return nil
}

if compress {

var b bytes.Buffer
Expand All @@ -157,7 +181,6 @@ func GetVacuumReportCommand() *cobra.Command {
reportOutput, vr.Generated.Format("01-02-06-15_04_05"), extension)

err = os.WriteFile(reportOutputName, reportData, 0664)

if err != nil {
pterm.Error.Printf("Unable to write report file: '%s': %s\n", reportOutputName, err.Error())
pterm.Println()
Expand All @@ -173,6 +196,8 @@ func GetVacuumReportCommand() *cobra.Command {
return nil
},
}
cmd.Flags().BoolP("stdin", "i", false, "Use stdin as input, instead of a file")
cmd.Flags().BoolP("stdout", "o", false, "Use stdout as output, instead of a file")
cmd.Flags().BoolP("compress", "c", false, "Compress results using gzip")
cmd.Flags().BoolP("no-pretty", "n", false, "Render JSON with no formatting")
return cmd
Expand Down

0 comments on commit e3edfc0

Please sign in to comment.