lint

package
v0.13.1 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2023 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// TemplateFuncs are global functions available in templates.
	TemplateFuncs = template.FuncMap{
		"json": func(v any, args ...string) (string, error) {
			var (
				b   []byte
				err error
			)
			switch len(args) {
			case 0:
				b, err = json.Marshal(v)
			case 1:
				b, err = json.MarshalIndent(v, "", args[0])
			default:
				b, err = json.MarshalIndent(v, args[0], args[1])
			}
			return string(b), err
		},
	}
	// DefaultTemplate is the default template used by the CI job.
	DefaultTemplate = template.Must(template.New("report").
					Funcs(TemplateFuncs).
					Parse(`
{{- range $f := .Files }}
	{{- /* If there is an error but not diagnostics, print it. */}}
	{{- if and $f.Error (not $f.Reports) }}
		{{- printf "%s: %s\n" $f.Name $f.Error }}
	{{- else }}
		{{- range $r := $f.Reports }}
			{{- if $r.Text }}
				{{- printf "%s: %s:\n\n" $f.Name $r.Text }}
			{{- else if $r.Diagnostics }}
				{{- printf "Unnamed diagnostics for file %s:\n\n" $f.Name }}
			{{- end }}
			{{- range $d := $r.Diagnostics }}
				{{- printf "\tL%d: %s\n" ($f.Line $d.Pos) $d.Text }}
			{{- end }}
			{{- if $r.Diagnostics }}
				{{- print "\n" }}
			{{- end }}
		{{- end }}
	{{- end }}
{{- end -}}
`))
)

Functions

This section is empty.

Types

type ChangeDetector

type ChangeDetector interface {
	// DetectChanges splits the files of a migration directory into the "base" files (already merged) and new ones.
	DetectChanges(context.Context) ([]migrate.File, []migrate.File, error)
}

A ChangeDetector takes a migration directory and splits it into the "base" files (already merged) and new ones.

func LatestChanges

func LatestChanges(dir migrate.Dir, n int) ChangeDetector

LatestChanges implements the ChangeDetector interface by selecting the latest N files as new. It is useful for executing analysis on files in development before they are committed or on all files in a directory.

type ChangeLoader

type ChangeLoader interface {
	// LoadChanges converts each of the given migration files into one Changes.
	LoadChanges(context.Context, []migrate.File) (*Changes, error)
}

A ChangeLoader takes a set of migration files and will create multiple schema.Changes out of it.

type Changes

type Changes struct {
	From, To *schema.Realm    // Current and desired schema.
	Files    []*sqlcheck.File // Files for moving from current to desired state.
}

Changes holds schema changes information returned by the loader.

type DevLoader

type DevLoader struct {
	// Dev environment used as a sandbox instantiated to the starting point (e.g. base branch).
	Dev *sqlclient.Client
}

DevLoader implements the ChangesLoader interface using a dev-driver.

func (*DevLoader) LoadChanges

func (d *DevLoader) LoadChanges(ctx context.Context, base, files []migrate.File) (diff *Changes, err error)

LoadChanges implements the ChangesLoader interface.

type FileError

type FileError struct {
	File string
	Err  error // Atlas or database error.
	Pos  int   // Position error, if known.
}

FileError represents an error that occurred while processing a file.

func (FileError) Error

func (e FileError) Error() string

type FileReport

type FileReport struct {
	Name    string            `json:"Name,omitempty"`    // Name of the file.
	Text    string            `json:"Text,omitempty"`    // Contents of the file.
	Reports []sqlcheck.Report `json:"Reports,omitempty"` // List of reports.
	Error   string            `json:"Error,omitempty"`   // File specific error.
}

FileReport contains a summary of the analysis of a single file.

func NewFileReport

func NewFileReport(f migrate.File) *FileReport

NewFileReport returns a new FileReport.

func (*FileReport) Line

func (f *FileReport) Line(pos int) int

Line returns the line number from a position.

func (*FileReport) WriteReport

func (f *FileReport) WriteReport(r sqlcheck.Report)

WriteReport implements sqlcheck.ReportWriter.

type GitChangeDetector

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

GitChangeDetector implements the ChangeDetector interface by utilizing a git repository.

func NewGitChangeDetector

func NewGitChangeDetector(dir migrate.Dir, opts ...GitChangeDetectorOption) (*GitChangeDetector, error)

NewGitChangeDetector configures a new GitChangeDetector.

func (*GitChangeDetector) DetectChanges

func (d *GitChangeDetector) DetectChanges(ctx context.Context) ([]migrate.File, []migrate.File, error)

DetectChanges implements the ChangeDetector interface.

type GitChangeDetectorOption

type GitChangeDetectorOption func(*GitChangeDetector) error

GitChangeDetectorOption allows configuring GitChangeDetector with functional arguments.

func WithBase

func WithBase(base string) GitChangeDetectorOption

WithBase configures the git base branch name for a GitChangeDetector.

func WithMigrationsPath

func WithMigrationsPath(path string) GitChangeDetectorOption

WithMigrationsPath configures the path for the migration directory.

func WithWorkDir

func WithWorkDir(work string) GitChangeDetectorOption

WithWorkDir configures the git working directory for a GitChangeDetector.

type ReportWriter

type ReportWriter interface {
	WriteReport(*SummaryReport) error
}

ReportWriter is a type of report writer that writes a summary of analysis reports.

type Runner

type Runner struct {
	// DevClient configures the "dev driver" to calculate
	// migration changes by the driver.
	Dev *sqlclient.Client

	// RunChangeDetector configures the ChangeDetector to
	// be used by the runner.
	ChangeDetector ChangeDetector

	// Dir is used for scanning and validating the migration directory.
	Dir migrate.Dir

	// Analyzers defines the analysis to be run in the CI job.
	Analyzers []sqlcheck.Analyzer

	// ReportWriter writes the summary report.
	ReportWriter ReportWriter
	// contains filtered or unexported fields
}

Runner is used to execute CI jobs.

func (*Runner) Run

func (r *Runner) Run(ctx context.Context) error

Run executes the CI job.

type SilentError

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

SilentError is returned in case the wrapped error is already printed by the runner and should not be printed by its caller

type SummaryReport

type SummaryReport struct {
	// Env holds the environment information.
	Env struct {
		Driver string         `json:"Driver,omitempty"` // Driver name.
		URL    *sqlclient.URL `json:"URL,omitempty"`    // URL to dev database.
		Dir    string         `json:"Dir,omitempty"`    // Path to migration directory.
	}

	// Steps of the analysis. Added in verbose mode.
	Steps []struct {
		Name   string      `json:"Name,omitempty"`   // Step name.
		Text   string      `json:"Text,omitempty"`   // Step description.
		Error  string      `json:"Error,omitempty"`  // Error that cause the execution to halt.
		Result *FileReport `json:"Result,omitempty"` // Result of the step. For example, a diagnostic.
	}

	// Schema versions found by the runner.
	Schema struct {
		Current string `json:"Current,omitempty"` // Current schema.
		Desired string `json:"Desired,omitempty"` // Desired schema.
	}

	// Files reports. Non-empty in case there are findings.
	Files []*FileReport `json:"Files,omitempty"`
}

A SummaryReport contains a summary of the analysis of all files. It is used as an input to templates to report the CI results.

func NewSummaryReport

func NewSummaryReport(c *sqlclient.Client, dir migrate.Dir) *SummaryReport

NewSummaryReport returns a new SummaryReport.

func (*SummaryReport) StepError

func (f *SummaryReport) StepError(name, text string, err error) error

StepError appends step error to the summary.

func (*SummaryReport) StepResult

func (f *SummaryReport) StepResult(name, text string, result *FileReport)

StepResult appends step result to the summary.

func (*SummaryReport) WriteSchema

func (f *SummaryReport) WriteSchema(c *sqlclient.Client, diff *Changes)

WriteSchema writes the current and desired schema to the summary.

type TemplateWriter

type TemplateWriter struct {
	T *template.Template
	W io.Writer
}

A TemplateWriter is a type of writer that writes output according to a template.

func (*TemplateWriter) WriteReport

func (w *TemplateWriter) WriteReport(r *SummaryReport) error

WriteReport implements ReportWriter.

Jump to

Keyboard shortcuts

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