Skip to content

Commit

Permalink
Clean sweep after linting for the first time.
Browse files Browse the repository at this point in the history
Cleaned up a variety of linting errors, lowering the coverage no doubt. However, it needs to be done.

Signed-off-by: Dave Shanley <dshanley@splunk.com>
  • Loading branch information
daveshanley committed Jul 22, 2022
1 parent 67b4b3e commit ff0bd3f
Show file tree
Hide file tree
Showing 60 changed files with 506 additions and 364 deletions.
6 changes: 4 additions & 2 deletions cmd/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ func GetDashboardCommand() *cobra.Command {

rulesetFlag, _ := cmd.Flags().GetString("ruleset")
resultSet, ruleset, err = BuildResults(rulesetFlag, specBytes, customFunctions)
if err != nil {
return err
}
specIndex = ruleset.Index
specInfo = ruleset.SpecInfo
specInfo.Generated = time.Now()
Expand All @@ -74,8 +77,7 @@ func GetDashboardCommand() *cobra.Command {

dash := cui.CreateDashboard(resultSet, specIndex, specInfo)
dash.Version = Version
dash.Render()
return nil
return dash.Render()
},
}
}
8 changes: 6 additions & 2 deletions cmd/generate_ruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ func GetGenerateRulesetCommand() *cobra.Command {
// is to re-encode from rules to ruleDefinitions (which is a proxy property)
encoded, _ := json.Marshal(selectedRuleSet.Rules)
encodedMap := make(map[string]interface{})
json.Unmarshal(encoded, &encodedMap)
err := json.Unmarshal(encoded, &encodedMap)
if err != nil {
return err
}

selectedRuleSet.RuleDefinitions = encodedMap

pterm.Info.Printf("Generating RuleSet rules: %s", selectedRuleSet.DocumentationURI)
Expand All @@ -76,7 +80,7 @@ func GetGenerateRulesetCommand() *cobra.Command {

reportOutputName := fmt.Sprintf("%s-%s%s", reportOutput, args[0], extension)

err := ioutil.WriteFile(reportOutputName, yamlBytes, 0664)
err = ioutil.WriteFile(reportOutputName, yamlBytes, 0664)

if err != nil {
pterm.Error.Printf("Unable to write RuleSet file: '%s': %s\n", reportOutputName, err.Error())
Expand Down
3 changes: 3 additions & 0 deletions cmd/html_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ func GetHTMLReportCommand() *cobra.Command {

rulesetFlag, _ := cmd.Flags().GetString("ruleset")
resultSet, ruleset, err = BuildResults(rulesetFlag, specBytes, customFunctions)
if err != nil {
return err
}
specIndex = ruleset.Index
specInfo = ruleset.SpecInfo
specInfo.Generated = time.Now()
Expand Down
12 changes: 7 additions & 5 deletions cmd/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func GetLintCommand() *cobra.Command {
informs := resultSet.GetInfoCount()

if !detailsFlag {
RenderSummary(resultSet, args, silent)
RenderSummary(resultSet, silent)
RenderTime(timeFlag, duration, fi)
return CheckFailureSeverity(failSeverityFlag, errors, warnings, informs)
}
Expand Down Expand Up @@ -170,7 +170,7 @@ func GetLintCommand() *cobra.Command {
pterm.Println()
} // Blank line

RenderSummary(resultSet, args, silent)
RenderSummary(resultSet, silent)
RenderTime(timeFlag, duration, fi)
return CheckFailureSeverity(failSeverityFlag, errors, warnings, informs)
},
Expand Down Expand Up @@ -247,7 +247,6 @@ func processResults(results []*model.RuleFunctionResult, specData []string, snip
_ = pterm.DefaultTable.WithHasHeader().WithData(tableData).Render()
renderCodeSnippet(r, specData)
}
i++
}

if !snippets && !silent {
Expand Down Expand Up @@ -279,7 +278,7 @@ func renderCodeSnippet(r *model.RuleFunctionResult, specData []string) {
}
}

func RenderSummary(rs *model.RuleResultSet, args []string, silent bool) {
func RenderSummary(rs *model.RuleResultSet, silent bool) {

tableData := [][]string{{"Category", pterm.LightRed("Errors"), pterm.LightYellow("Warnings"),
pterm.LightBlue("Info")}}
Expand All @@ -299,7 +298,10 @@ func RenderSummary(rs *model.RuleResultSet, args []string, silent bool) {

if len(rs.Results) > 0 {
if !silent {
pterm.DefaultTable.WithHasHeader().WithData(tableData).Render()
err := pterm.DefaultTable.WithHasHeader().WithData(tableData).Render()
if err != nil {
pterm.Error.Printf("error rendering table '%v'", err.Error())
}
pterm.Println()
pterm.Println()
}
Expand Down
6 changes: 2 additions & 4 deletions cmd/lint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ func TestGetLintCommand(t *testing.T) {
b := bytes.NewBufferString("")
cmd.SetOut(b)
cmd.SetArgs([]string{"../model/test_files/burgershop.openapi.yaml"})
cmd.Execute()
exErr := cmd.Execute()
assert.NoError(t, exErr)
outBytes, err := ioutil.ReadAll(b)
assert.NoError(t, err)
assert.NotNil(t, outBytes)
Expand Down Expand Up @@ -153,7 +154,6 @@ func TestGetLintCommand_InvalidRuleset(t *testing.T) {
}`

tmp, _ := ioutil.TempFile("", "")
defer tmp.Close()
_, _ = io.WriteString(tmp, json)

cmd := GetLintCommand()
Expand Down Expand Up @@ -457,7 +457,6 @@ rules:
oas3-valid-schema-example: error`

tmp, _ := ioutil.TempFile("", "")
defer tmp.Close()
_, _ = io.WriteString(tmp, yaml)

cmd := GetLintCommand()
Expand Down Expand Up @@ -486,7 +485,6 @@ rules:
oas3-valid-schema-example: true`

tmp, _ := ioutil.TempFile("", "")
defer tmp.Close()
_, _ = io.WriteString(tmp, yaml)

cmd := GetLintCommand()
Expand Down
10 changes: 8 additions & 2 deletions cmd/vacuum_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,14 @@ func GetVacuumReportCommand() *cobra.Command {

var b bytes.Buffer
gz := gzip.NewWriter(&b)
gz.Write(data)
gz.Close()
_, wErr := gz.Write(data)
if wErr != nil {
return wErr
}
wErr = gz.Close()
if wErr != nil {
return wErr
}
reportData = b.Bytes()
extension = ".json.gz"
}
Expand Down
149 changes: 73 additions & 76 deletions cui/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,28 @@ import (

// Dashboard represents the dashboard controlling container
type Dashboard struct {
C chan bool
run bool
grid *ui.Grid
helpGrid *ui.Grid
title *widgets.Paragraph
tabs TabbedView
healthGaugeItems []ui.GridItem
categoryHealthGauge []CategoryGauge
resultSet *model.RuleResultSet
index *index.SpecIndex
info *datamodel.SpecInfo
selectedTabIndex int
ruleCategories []*model.RuleCategory
selectedCategory *model.RuleCategory
selectedCategoryDescription *ui.GridItem
selectedRule *model.Rule
selectedRuleIndex int
selectedViolationIndex int
selectedViolation *model.RuleFunctionResult
violationViewActive bool
helpViewActive bool
uiEvents <-chan ui.Event
Version string
C chan bool
run bool
grid *ui.Grid
helpGrid *ui.Grid
title *widgets.Paragraph
tabs TabbedView
healthGaugeItems []ui.GridItem
categoryHealthGauge []CategoryGauge
resultSet *model.RuleResultSet
index *index.SpecIndex
info *datamodel.SpecInfo
selectedTabIndex int
ruleCategories []*model.RuleCategory
selectedCategory *model.RuleCategory
selectedRule *model.Rule
selectedRuleIndex int
selectedViolationIndex int
selectedViolation *model.RuleFunctionResult
violationViewActive bool
helpViewActive bool
uiEvents <-chan ui.Event
Version string
}

func CreateDashboard(resultSet *model.RuleResultSet, index *index.SpecIndex, info *datamodel.SpecInfo) *Dashboard {
Expand Down Expand Up @@ -134,64 +133,62 @@ func (dash *Dashboard) eventLoop(cats []*model.RuleCategory) {
}
// TODO: clean this damn mess up.
for {
select {
case e := <-uiEvents:
switch e.ID {
case "q", "<C-c>":
return
case "h":
dash.helpViewActive = true
case "<Tab>":
dash.violationViewActive = false
if dash.tabs.tv.ActiveTabIndex == len(cats)-1 { // loop around and around.
dash.tabs.tv.ActiveTabIndex = 0
} else {
dash.tabs.tv.FocusRight()
}
dash.tabs.setActiveCategoryIndex(dash.tabs.tv.ActiveTabIndex)
dash.generateViewsAfterEvent()

case "<Enter>":
dash.violationViewActive = true
dash.generateViewsAfterEvent()

case "<Escape>":
dash.violationViewActive = false
dash.helpViewActive = false
dash.generateViewsAfterEvent()

case "x", "<Right>":
dash.violationViewActive = false
e := <-uiEvents
switch e.ID {
case "q", "<C-c>":
return
case "h":
dash.helpViewActive = true
case "<Tab>":
dash.violationViewActive = false
if dash.tabs.tv.ActiveTabIndex == len(cats)-1 { // loop around and around.
dash.tabs.tv.ActiveTabIndex = 0
} else {
dash.tabs.tv.FocusRight()
dash.tabs.setActiveCategoryIndex(dash.tabs.tv.ActiveTabIndex)
dash.generateViewsAfterEvent()

case "s", "<Left>":
dash.violationViewActive = false
dash.tabs.tv.FocusLeft()
dash.tabs.setActiveCategoryIndex(dash.tabs.tv.ActiveTabIndex)
dash.generateViewsAfterEvent()

case "z", "<Down>":
if dash.violationViewActive {
dash.tabs.scrollViolationsDown()
} else {
dash.tabs.scrollRulesDown()
}
case "a", "<Up>":
if dash.violationViewActive {
dash.tabs.scrollViolationsUp()
} else {
dash.tabs.scrollRulesUp()
}
}
ui.Clear()
if dash.helpViewActive {
ui.Render(dash.helpGrid)
dash.tabs.setActiveCategoryIndex(dash.tabs.tv.ActiveTabIndex)
dash.generateViewsAfterEvent()

case "<Enter>":
dash.violationViewActive = true
dash.generateViewsAfterEvent()

case "<Escape>":
dash.violationViewActive = false
dash.helpViewActive = false
dash.generateViewsAfterEvent()

case "x", "<Right>":
dash.violationViewActive = false
dash.tabs.tv.FocusRight()
dash.tabs.setActiveCategoryIndex(dash.tabs.tv.ActiveTabIndex)
dash.generateViewsAfterEvent()

case "s", "<Left>":
dash.violationViewActive = false
dash.tabs.tv.FocusLeft()
dash.tabs.setActiveCategoryIndex(dash.tabs.tv.ActiveTabIndex)
dash.generateViewsAfterEvent()

case "z", "<Down>":
if dash.violationViewActive {
dash.tabs.scrollViolationsDown()
} else {
dash.tabs.scrollRulesDown()
}
case "a", "<Up>":
if dash.violationViewActive {
dash.tabs.scrollViolationsUp()
} else {
ui.Render(dash.grid, dash.title)
dash.tabs.scrollRulesUp()
}
}
ui.Clear()
if dash.helpViewActive {
ui.Render(dash.helpGrid)
} else {
ui.Render(dash.grid, dash.title)
}
}
}

Expand Down
18 changes: 11 additions & 7 deletions cui/dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ import (
)

func TestCreateDashboard(t *testing.T) {
resultSet, index, info := testBootDashboard()
dash := CreateDashboard(resultSet, index, info)
resultSet, idx, info := testBootDashboard()
dash := CreateDashboard(resultSet, idx, info)
assert.Equal(t, "openapi", dash.info.SpecType)
}

func TestDashboard_GenerateTabbedView(t *testing.T) {

resultSet, index, info := testBootDashboard()
dash := CreateDashboard(resultSet, index, info)
resultSet, idx, info := testBootDashboard()
dash := CreateDashboard(resultSet, idx, info)
dash.ruleCategories = model.RuleCategoriesOrdered
dash.GenerateTabbedView()
assert.Equal(t, "information", dash.selectedCategory.Id)
Expand All @@ -35,8 +35,8 @@ func TestDashboard_GenerateTabbedView(t *testing.T) {

func TestDashboard_Render(t *testing.T) {

resultSet, index, info := testBootDashboard()
dash := CreateDashboard(resultSet, index, info)
resultSet, idx, info := testBootDashboard()
dash := CreateDashboard(resultSet, idx, info)
dash.ruleCategories = model.RuleCategoriesOrdered

// define our own events channel, so we can trigger the UI in any sequence we want.
Expand Down Expand Up @@ -119,7 +119,11 @@ func testBootDashboard() (*model.RuleResultSet, *index.SpecIndex, *datamodel.Spe

info, _ := datamodel.ExtractSpecInfo(yamlBytes)

yaml.Unmarshal(yamlBytes, &rootNode)
mErr := yaml.Unmarshal(yamlBytes, &rootNode)
if mErr != nil {
return nil, nil, nil
}

index := index.NewSpecIndex(&rootNode)

// let's go ahead and lint the spec and pass the results to the dashboard.
Expand Down
7 changes: 4 additions & 3 deletions cui/stats_chart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ func TestNewStatsChart(t *testing.T) {
yamlBytes, _ := ioutil.ReadFile("../model/test_files/burgershop.openapi.yaml")

info, _ := datamodel.ExtractSpecInfo(yamlBytes)
yaml.Unmarshal(yamlBytes, &rootNode)
index := index.NewSpecIndex(&rootNode)
mErr := yaml.Unmarshal(yamlBytes, &rootNode)
assert.NoError(t, mErr)
idx := index.NewSpecIndex(&rootNode)

chart := NewStatsChart(index, info)
chart := NewStatsChart(idx, info)

assert.Equal(t, "Filesize: [11kb](fg:green)", chart.bc.Rows[0])
}
Loading

0 comments on commit ff0bd3f

Please sign in to comment.