lint

package
v0.3.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 17, 2020 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// RulePrefix is a prefix of rule name
	RulePrefix = "rule."

	// DefaultFormat is a default format string
	DefaultFormat = "[{{.Level}}]  {{.Rule}}  {{.Message}}"

	// LevelError represents the error level reported by lint
	LevelError = "ERROR"
	// LevelWarning represents the warning level reported by lint
	LevelWarning = "WARN"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Report ReportConfig `hcl:"report,block"`
}

Config represents the configuration of the linter itself

type Debug

type Debug struct {
	Name  string         `hcl:"name,label"`
	Value *hcl.Attribute `hcl:"value"`
}

Debug is WIP

type File

type File struct {
	Path string
	Data []byte

	// Meta field means the annotation
	Meta string

	// File struct has Policy data
	// because policy applied to the file should be determined by each file
	Policy loader.Policy

	Diagnostics hcl.Diagnostics
}

File represents the files to be linted It's converted from the arguments

type Item

type Item struct {
	Name    string
	Message string
	Status  Status
	Level   string
}

Item represents the result of a rule

type Items

type Items []Item

Items is the collenction of Item object

type Linter

type Linter struct {
	// contains filtered or unexported fields
}

Linter is a linter structure

func NewLinter

func NewLinter(args []string, additionals ...string) (*Linter, error)

NewLinter creates Linter object based on Lint Policy

func (*Linter) Files

func (l *Linter) Files() []File

Files returns []File object converted from given arguments

func (*Linter) Print

func (l *Linter) Print(result Result)

Print prints the result of Lint based on Result reported by Run

func (*Linter) PrintSummary

func (l *Linter) PrintSummary(results ...Result)

PrintSummary prints the summary of all results of the entire Lint

func (*Linter) Run

func (l *Linter) Run(file File) (Result, error)

Run runs the linter against a file of an argument

func (*Linter) Status

func (l *Linter) Status(results ...Result) Status

Status indicates execution result of Lint by the status code

func (*Linter) Validate

func (l *Linter) Validate() error

Validate validates linter configuration

type Output

type Output struct {
	Name  string         `hcl:"name,label"`
	Value *hcl.Attribute `hcl:"value"`
}

Output is WIP

type Policy

type Policy struct {
	Config  *Config  `hcl:"config,block"`
	Rules   Rules    `hcl:"rule,block"`
	Outputs []Output `hcl:"output,block"`

	Debugs []Debug `hcl:"debug,block"`

	Remain hcl.Body `hcl:",remain"`
}

Policy represents the rule set against config files of arguments

func (*Policy) Validate

func (p *Policy) Validate() error

Validate validates policy syntax

type Precondition

type Precondition struct {
	Cases []bool `hcl:"cases"`
}

Precondition represents a condition that determines whether the rule should be executed

type Report

type Report struct {
	// Level takes ERROR or WARN
	// In case of ERROR, the report message of the failed rule is shown and the linter returns false
	// In case of WARN, the report message of the failed rule is shown and the linter returns true
	Level string `hcl:"level"`

	// Message is shown when the rule is failed
	Message string `hcl:"message"`
}

Report represents the rule of reporting style

type ReportConfig

type ReportConfig struct {
	Format string `hcl:"format,optional"`
	Style  string `hcl:"style,optional"`
	Color  bool   `hcl:"color,optional"`
}

ReportConfig represents the configuration of the report itself

type ReportLength

type ReportLength struct {
	// Max length of RulePrefix + {{.Rule}}
	MaxRuleName int

	// Max length of {{.Level}}
	MaxLevel int

	// Max length of {{.Message}}
	MaxMessage int
}

ReportLength is the information of the max length of each format strings

type Result

type Result struct {
	Path  string
	Items Items
	OK    bool

	// RulesNotFound is a flag for rules not found
	RulesNotFound bool
	// Metadata is something notes related to Result
	Metadata string
}

Result represents the execution result of Lint It's represented against one argument The result of each rules for the argument is stored in Items

type Rule

type Rule struct {
	Name string `hcl:"name,label"`

	Description  string        `hcl:"description"`
	Dependencies []string      `hcl:"depends_on,optional"`
	Precondition *Precondition `hcl:"precondition,block"`
	Conditions   []bool        `hcl:"conditions"`
	Report       Report        `hcl:"report,block"`

	Debugs []string `hcl:"debug,optional"`
}

Rule represents the linting rule

func (*Rule) BuildMessage

func (r *Rule) BuildMessage(cfg ReportConfig, length ReportLength) (string, error)

BuildMessage formats the results reported by linter.

func (*Rule) SkipCase

func (r *Rule) SkipCase() bool

SkipCase returns true if cases of a precondition block includes one or more failed cases

func (*Rule) TrueCase

func (r *Rule) TrueCase() bool

TrueCase returns true if all conditions in a rule are true.

func (*Rule) Validate

func (r *Rule) Validate() error

Validate validates rule syntax

type Rules

type Rules []Rule

Rules is the collenction of Rule object

func (*Rules) Sort

func (r *Rules) Sort()

Sort sorts the rules based on its own dependencies

For example, in the case that these rules are defined like below, the order which the rules are executed should be as follows:

rule.a --- rule.b --- rule.c
                   `- rule.d

rule "a" {
  ...
}
rule "b" {
  depends_on = ["rule.a"]
}
rule "c" {
  depends_on = ["rule.b"]
}
rule "d" {
  depends_on = ["rule.a"]
}

This implementation is based on the algorithm of topological sort

type Status

type Status int

Status represents the status code of Lint

const (
	// Success is the success code of Lint
	Success Status = iota
	// Failure is the failure code of Lint
	Failure
)

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL