diagnostic

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 30, 2025 License: BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MustInstallFlags

func MustInstallFlags(fs *pflag.FlagSet)

MustInstallFlags installs the flags for the diagnostic package.

Types

type ANSIPrinter

type ANSIPrinter struct {
	TextPrinter
}

ANSIPrinter prints diagnostic messages in plain text format with ANSI color codes, if the underlying writer is a terminal. If it is not a terminal, this type behaves like TextPrinter.

func (*ANSIPrinter) Print

func (p *ANSIPrinter) Print(message *Message)

Print prints the given message in plain text format with ANSI color codes.

type Attachment

type Attachment interface {
	// contains filtered or unexported methods
}

Attachment represents an attachment that can be applied to a diagnostic message.

This follows the Go "options" pattern just to make it variadic and easy to set in a clean way.

func Column

func Column(column int) Attachment

Column returns an attachment that sets the column of the message.

func ColumnRange

func ColumnRange(column, columnEnd int) Attachment

ColumnRange returns an attachment that sets the column and end column of the message.

func File

func File(file string) Attachment

File returns an attachment that sets the file of the message.

func Line

func Line(line int) Attachment

Line returns an attachment that sets the line of the message.

func LineRange

func LineRange(line, lineEnd int) Attachment

LineRange returns an attachment that sets the line and end line of the message.

func Title

func Title(title string) Attachment

Title returns an attachment that sets the title of the message.

type Format

type Format string

Format represents the format type of the diagnostic output.

const (
	// FormatGitHub represents the GitHub format.
	FormatGitHub Format = "github"

	// FormatText represents the text format.
	FormatText Format = "text"

	// FormatJSON represents the JSON format.
	FormatJSON Format = "json"
)

func ParseFormat

func ParseFormat(s string) (Format, error)

ParseFormat parses the given string into a Format.

func (Format) Printer

func (f Format) Printer(w io.Writer) Printer

Printer represents a printer that can print diagnostic messages.

func (Format) Reporter

func (f Format) Reporter(w io.Writer) *Reporter

Reporter returns a new reporter that uses the given format.

func (*Format) UnmarshalText

func (f *Format) UnmarshalText(text []byte) error

UnmarshalText unmarshals the given text into a Format.

type GitHubPrinter

type GitHubPrinter struct {
	// W is the writer to write the JSON output to. If not set, it defaults to
	// os.Stdout.
	W io.Writer
}

GitHubPrinter prints diagnostic messages in the format expected by GitHub Actions. See: https://docs.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-error-message

func (*GitHubPrinter) Print

func (p *GitHubPrinter) Print(message *Message)

Print prints the given message in the format expected by GitHub Actions.

type JSONPrinter

type JSONPrinter struct {
	// W is the writer to write the JSON output to. If not set, it defaults to
	// os.Stdout.
	W io.Writer

	// Indent specifies whether the JSON output should be indented.
	Indent bool
}

JSONPrinter prints diagnostic messages in JSON format.

func (*JSONPrinter) Print

func (p *JSONPrinter) Print(message *Message)

Print prints the given message in JSON format.

type Message

type Message struct {
	// Severity is the severity of the message (required).
	Severity Severity `json:"severity"`

	// Body is the body of the message (required).
	Body string `json:"body"`

	// Title is the title of the message (optional).
	Title string `json:"title,omitempty"`

	// File is the file name where the message originated (optional).
	File string `json:"file,omitempty"`

	// Line is the line number where the message originated (optional).
	Line int `json:"line,omitempty"`

	// Column is the column number where the message originated (optional).
	Column int `json:"column,omitempty"`

	// LineEnd is the end line number where the message originated (optional).
	LineEnd int `json:"line-end,omitempty"`

	// ColumnEnd is the end column number where the message originated (optional).
	ColumnEnd int `json:"column-end,omitempty"`
}

Message represents a diagnostic message. The fields in this message map to the message fields as defined in https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/workflow-commands-for-github-actions

func Debugf

func Debugf(format string, args ...any) *Message

Debugf creates a new debug message with the given severity and body.

func Errorf

func Errorf(format string, args ...any) *Message

Errorf creates a new error message with the given severity and body.

func NewMessage

func NewMessage(severity Severity, format string, args ...any) *Message

NewMessage creates a new message with the given severity and body.

func Noticef

func Noticef(format string, args ...any) *Message

Noticef creates a new notice message with the given severity and body.

func Warningf

func Warningf(format string, args ...any) *Message

Warningf creates a new warning message with the given severity and body.

func (*Message) With

func (m *Message) With(attachments ...Attachment) *Message

With applies the attachments to the message.

type Printer

type Printer interface {
	// Print prints the given message.
	Print(message *Message)
}

Printer represents a Printer that can print diagnostic messages.

var DefaultPrinter Printer = &TextPrinter{W: os.Stdout}

DefaultPrinter is the default printer to use if no other printer is specified.

func NewPrinter

func NewPrinter(w io.Writer, format Format) Printer

NewPrinter creates a new printer that writes to the given writer using the given formats. If no formats are provided, the printer defaults to the text format.

type Reporter

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

Reporter represents an emitter that can emit diagnostic messages.

func NewReporter

func NewReporter(printer Printer) *Reporter

NewReporter creates a new reporter with the given printer.

func ReporterFromFlags

func ReporterFromFlags(fs *pflag.FlagSet) (*Reporter, error)

ReporterFromFlags returns the printer based on the flags. The reporter will always use stderr by default, since reports need to be unbuffered messages.

func ReporterFromFlagsWithWriter

func ReporterFromFlagsWithWriter(fs *pflag.FlagSet, w io.Writer) (*Reporter, error)

ReporterFromFlagsWithWriter returns the printer based on the flags with the given writer.

func (*Reporter) Debugf

func (r *Reporter) Debugf(format string, args ...any)

Debugf emits a debug message.

func (*Reporter) ErrorCount

func (r *Reporter) ErrorCount() int

ErrorCount returns the number of errors emitted.

func (*Reporter) Errorf

func (r *Reporter) Errorf(format string, args ...any)

Errorf emits an error message.

func (*Reporter) Fatalf

func (r *Reporter) Fatalf(format string, args ...any)

Fatalf emits a fatal error and exits the program.

func (*Reporter) NoticeCount

func (r *Reporter) NoticeCount() int

NoticeCount returns the number of notices emitted.

func (*Reporter) Noticef

func (r *Reporter) Noticef(format string, args ...any)

Noticef emits a notice message.

func (*Reporter) Report

func (r *Reporter) Report(message *Message)

Report emits the given message.

func (*Reporter) ReportFatal

func (r *Reporter) ReportFatal(message *Message)

ReportFatal emits a fatal error and exits the program.

func (*Reporter) ShowDebug

func (r *Reporter) ShowDebug(enable bool) *Reporter

ShowDebug sets whether debug messages should be emitted.

func (*Reporter) WarningCount

func (r *Reporter) WarningCount() int

WarningCount returns the number of warnings emitted.

func (*Reporter) Warningf

func (r *Reporter) Warningf(format string, args ...any)

Warningf emits a warning message.

type Severity

type Severity string

Severity represents the severity of a diagnostic message.

const (
	// SeverityError represents an error message.
	SeverityError Severity = "error"

	// SeverityWarning represents a warning message.
	SeverityWarning Severity = "warning"

	// SeverityNotice represents a notice message.
	SeverityNotice Severity = "notice"

	// SeverityDebug represents a debug message.
	SeverityDebug Severity = "debug"
)

type TextPrinter

type TextPrinter struct {
	// W is the writer to write the JSON output to. If not set, it defaults to
	// os.Stdout.
	W io.Writer
}

TextPrinter prints diagnostic messages in plain text format. This will ignore any source-location information from the message and only print the severity and body.

func (*TextPrinter) Print

func (p *TextPrinter) Print(message *Message)

Print prints the given message in plain text format.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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