Skip to content

Commit

Permalink
Added silence option for custom functions code.
Browse files Browse the repository at this point in the history
Signed-off-by: quobix <dave@quobix.com>
  • Loading branch information
daveshanley committed Feb 3, 2024
1 parent 1720ca4 commit 5e80769
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 21 deletions.
3 changes: 2 additions & 1 deletion cmd/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func GetDashboardCommand() *cobra.Command {
skipCheckFlag, _ := cmd.Flags().GetBool("skip-check")
timeoutFlag, _ := cmd.Flags().GetInt("timeout")
hardModeFlag, _ := cmd.Flags().GetBool("hard-mode")
silent, _ := cmd.Flags().GetBool("silent")

var err error
vacuumReport, specBytes, _ := vacuum_report.BuildVacuumReportFromFile(args[0])
Expand All @@ -61,7 +62,7 @@ func GetDashboardCommand() *cobra.Command {
if vacuumReport == nil {

functionsFlag, _ := cmd.Flags().GetString("functions")
customFunctions, _ := LoadCustomFunctions(functionsFlag)
customFunctions, _ := LoadCustomFunctions(functionsFlag, silent)

rulesetFlag, _ := cmd.Flags().GetString("ruleset")
resultSet, ruleset, err = BuildResultsWithDocCheckSkip(false, hardModeFlag, rulesetFlag, specBytes, customFunctions,
Expand Down
3 changes: 2 additions & 1 deletion cmd/html_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func GetHTMLReportCommand() *cobra.Command {
skipCheckFlag, _ := cmd.Flags().GetBool("skip-check")
timeoutFlag, _ := cmd.Flags().GetInt("timeout")
hardModeFlag, _ := cmd.Flags().GetBool("hard-mode")
silent, _ := cmd.Flags().GetBool("silent")

// disable color and styling, for CI/CD use.
// https://github.com/daveshanley/vacuum/issues/234
Expand Down Expand Up @@ -91,7 +92,7 @@ func GetHTMLReportCommand() *cobra.Command {
if vacuumReport == nil {

functionsFlag, _ := cmd.Flags().GetString("functions")
customFunctions, _ := LoadCustomFunctions(functionsFlag)
customFunctions, _ := LoadCustomFunctions(functionsFlag, silent)

rulesetFlag, _ := cmd.Flags().GetString("ruleset")
resultSet, ruleset, err = BuildResultsWithDocCheckSkip(false, hardModeFlag, rulesetFlag, specBytes, customFunctions,
Expand Down
2 changes: 1 addition & 1 deletion cmd/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func GetLintCommand() *cobra.Command {

defaultRuleSets := rulesets.BuildDefaultRuleSetsWithLogger(logger)
selectedRS := defaultRuleSets.GenerateOpenAPIRecommendedRuleSet()
customFunctions, _ := LoadCustomFunctions(functionsFlag)
customFunctions, _ := LoadCustomFunctions(functionsFlag, silent)

// HARD MODE
if hardModeFlag {
Expand Down
4 changes: 2 additions & 2 deletions cmd/shared_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ func PrintBanner() {
}

// LoadCustomFunctions will scan for (and load) custom functions defined as vacuum plugins.
func LoadCustomFunctions(functionsFlag string) (map[string]model.RuleFunction, error) {
func LoadCustomFunctions(functionsFlag string, silence bool) (map[string]model.RuleFunction, error) {
// check custom functions
if functionsFlag != "" {
pm, err := plugin.LoadFunctions(functionsFlag)
pm, err := plugin.LoadFunctions(functionsFlag, silence)
if err != nil {
pterm.Error.Printf("Unable to open custom functions: %v\n", err)
pterm.Println()
Expand Down
2 changes: 1 addition & 1 deletion cmd/spectral_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func GetSpectralReportCommand() *cobra.Command {
// and see if it's valid. If so - let's go!
if rulesetFlag != "" {

customFunctions, _ = LoadCustomFunctions(functionsFlag)
customFunctions, _ = LoadCustomFunctions(functionsFlag, true)
rsBytes, rsErr := os.ReadFile(rulesetFlag)
if rsErr != nil {
pterm.Error.Printf("Unable to read ruleset file '%s': %s\n", rulesetFlag, rsErr.Error())
Expand Down
2 changes: 1 addition & 1 deletion cmd/vacuum_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func GetVacuumReportCommand() *cobra.Command {
// and see if it's valid. If so - let's go!
if rulesetFlag != "" {

customFunctions, _ = LoadCustomFunctions(functionsFlag)
customFunctions, _ = LoadCustomFunctions(functionsFlag, true)

rsBytes, rsErr := os.ReadFile(rulesetFlag)
if rsErr != nil {
Expand Down
8 changes: 4 additions & 4 deletions motor/rule_applicator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2038,7 +2038,7 @@ custom:
id: "1234"
`
// load custom functions
pm, err := plugin.LoadFunctions("../plugin/sample/js")
pm, err := plugin.LoadFunctions("../plugin/sample/js", false)
assert.NoError(t, err)

// create a new document.
Expand Down Expand Up @@ -2084,7 +2084,7 @@ func TestApplyRules_TestRules_Custom_JS_Function_CustomDoc_CoreFunction(t *testi
notCustom: true"
`
// load custom functions
pm, err := plugin.LoadFunctions("../plugin/sample/js")
pm, err := plugin.LoadFunctions("../plugin/sample/js", false)
assert.NoError(t, err)

// create a new document.
Expand Down Expand Up @@ -2136,7 +2136,7 @@ paths:
patch: something
`
// load custom functions
pm, err := plugin.LoadFunctions("../plugin/sample/js")
pm, err := plugin.LoadFunctions("../plugin/sample/js", false)
assert.NoError(t, err)

// create a new document.
Expand Down Expand Up @@ -2182,7 +2182,7 @@ func TestApplyRules_TestRules_Custom_JS_Function_CustomDoc_CoreFunction_Function
notCustom: true"
`
// load custom functions
pm, err := plugin.LoadFunctions("../plugin/sample/js")
pm, err := plugin.LoadFunctions("../plugin/sample/js", false)
assert.NoError(t, err)

// create a new document.
Expand Down
12 changes: 7 additions & 5 deletions plugin/plugin_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

// LoadFunctions will load custom functions found in the supplied path
func LoadFunctions(path string) (*Manager, error) {
func LoadFunctions(path string, silence bool) (*Manager, error) {

dirEntries, err := os.ReadDir(path)
if err != nil {
Expand All @@ -29,8 +29,9 @@ func LoadFunctions(path string) (*Manager, error) {
fPath := filepath.Join(path, entry.Name())

// found something
pterm.Info.Printf("Located custom function plugin: %s\n", fPath)

if !silence {
pterm.Info.Printf("Located custom function plugin: %s\n", fPath)
}
// let's try and open it.
p, e := plugin.Open(fPath)
if e != nil {
Expand Down Expand Up @@ -65,8 +66,9 @@ func LoadFunctions(path string) (*Manager, error) {
function := javascript.NewJSRuleFunction(fName, string(p))

// found something
pterm.Info.Printf("Located custom javascript function: '%s'\n", function.GetSchema().Name)

if !silence {
pterm.Info.Printf("Located custom javascript function: '%s'\n", function.GetSchema().Name)
}
// check if the function is valid
sErr := function.CheckScript()

Expand Down
10 changes: 5 additions & 5 deletions plugin/plugin_loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ import (
)

func TestLoadFunctions_Nowhere(t *testing.T) {
pm, err := LoadFunctions("nowhere")
pm, err := LoadFunctions("nowhere", false)
assert.Nil(t, pm)
assert.Error(t, err)
}

func TestLoadFunctions(t *testing.T) {
pm, err := LoadFunctions("../model/test_files")
pm, err := LoadFunctions("../model/test_files", false)
assert.NotNil(t, pm)
assert.NoError(t, err)
assert.Equal(t, 0, pm.LoadedFunctionCount())
}

func TestLoadFunctions_JavaScript_OK(t *testing.T) {
pm, err := LoadFunctions("sample/js")
pm, err := LoadFunctions("sample/js", false)
assert.NotNil(t, pm)
assert.NoError(t, err)
assert.Equal(t, 5, pm.LoadedFunctionCount())
Expand All @@ -31,7 +31,7 @@ func TestLoadFunctions_JavaScript_OK(t *testing.T) {
}

func TestLoadFunctions_Sample(t *testing.T) {
pm, err := LoadFunctions("sample")
pm, err := LoadFunctions("sample", false)
if runtime.GOOS != "windows" { // windows does not support this feature, at all.
assert.NotNil(t, pm)
assert.NoError(t, err)
Expand All @@ -40,7 +40,7 @@ func TestLoadFunctions_Sample(t *testing.T) {
}

func TestLoadFunctions_TestCompile(t *testing.T) {
pm, err := LoadFunctions("sample")
pm, err := LoadFunctions("sample", false)
if runtime.GOOS != "windows" { // windows does not support this feature, at all.
assert.NotNil(t, pm)
assert.NoError(t, err)
Expand Down

0 comments on commit 5e80769

Please sign in to comment.