...

Package config

import "github.com/bazelbuild/buildtools/buildifier/config"
Overview
Index
Examples

Overview ▾

Package config provides configuration objects for buildifier

func FindConfigPath

func FindConfigPath(rootDir string) string

FindConfigPath locates the nearest buildifier configuration file. First tries the value of the BUILDIFIER_CONFIG environment variable. If no environment variable is defined, The configuration file will be resolved starting from the process cwd and searching up the file tree until a config file is (or isn't) found.

func ValidateFormat

func ValidateFormat(format, mode *string) error

ValidateFormat validates the value of --format

func ValidateInputType

func ValidateInputType(inputType *string) error

ValidateInputType validates the value of --type

func ValidateModes

func ValidateModes(mode, lint *string, dflag *bool, additionalModes ...string) error

ValidateModes validates flags --mode, --lint, and -d

func ValidateWarnings

func ValidateWarnings(warnings *string, allWarnings, defaultWarnings *[]string) ([]string, error)

ValidateWarnings validates the value of the --warnings flag

type ArrayFlags

ArrayFlags is a string slice that satisfies the flag.Value interface

type ArrayFlags []string

func (*ArrayFlags) Set

func (i *ArrayFlags) Set(value string) error

Set implements part of the flag.Value interface

func (*ArrayFlags) String

func (i *ArrayFlags) String() string

String implements part of the flag.Value interface

type Config

Config is used to configure buildifier

type Config struct {
    // InputType determines the input file type: build (for BUILD files), bzl
    // (for .bzl files), workspace (for WORKSPACE files), default (for generic
    // Starlark files), module (for MODULE.bazel files)
    // or auto (default, based on the filename)
    InputType string `json:"type,omitempty"`
    // Format sets the diagnostics format: text or json (default text)
    Format string `json:"format,omitempty"`
    // Mode determines the formatting mode: check, diff, or fix (default fix)
    Mode string `json:"mode,omitempty"`
    // DiffMode is an alias for
    DiffMode bool `json:"diffMode,omitempty"`
    // Lint determines the lint mode: off, warn, or fix (default off)
    Lint string `json:"lint,omitempty"`
    // Warnings is a comma-separated list of warning identifiers used in the lint mode or "all"
    Warnings string `json:"warnings,omitempty"`
    // WarningsList is a list of warnings (alternative to comma-separated warnings string)
    WarningsList []string `json:"warningsList,omitempty"`
    // Recursive instructs buildifier to find starlark files recursively
    Recursive bool `json:"recursive,omitempty"`
    // Verbose instructs buildifier to output verbose diagnostics
    Verbose bool `json:"verbose,omitempty"`
    // DiffCommand is the command to run when the formatting mode is diff
    // (default uses the BUILDIFIER_DIFF, BUILDIFIER_MULTIDIFF, and DISPLAY
    // environment variables to create the diff command)
    DiffCommand string `json:"diffCommand,omitempty"`
    // MultiDiff means the command specified by the -diff_command flag can diff
    // multiple files in the style of tkdiff (default false)
    MultiDiff bool `json:"multiDiff,omitempty"`
    // TablesPath is the path to JSON file with custom table definitions that
    // will replace the built-in tables
    TablesPath string `json:"tables,omitempty"`
    // AddTablesPath path to JSON file with custom table definitions which will be merged with the built-in tables
    AddTablesPath string `json:"addTables,omitempty"`
    // WorkspaceRelativePath - assume BUILD file has this path relative to the workspace directory
    WorkspaceRelativePath string `json:"path,omitempty"`
    // DisableRewrites configures the list of buildifier rewrites to disable
    DisableRewrites ArrayFlags `json:"buildifier_disable,omitempty"`
    // AllowSort specifies additional sort contexts to treat as safe
    AllowSort ArrayFlags `json:"allowsort,omitempty"`

    // Help is true if the -h flag is set
    Help bool `json:"-"`
    // Version is true if the -v flag is set
    Version bool `json:"-"`
    // ConfigPath is the path to this config
    ConfigPath string `json:"-"`
    // LintWarnings is the final validated list of Lint/Fix warnings
    LintWarnings []string `json:"-"`
}

func Example

func Example() *Config

Example creates a sample configuration file for the -config=example flag.

Example

Code:

c := Example()
fmt.Print(c.String())

Output:

{
  "type": "auto",
  "mode": "fix",
  "lint": "fix",
  "warningsList": [
    "attr-applicable_licenses",
    "attr-cfg",
    "attr-license",
    "attr-licenses",
    "attr-non-empty",
    "attr-output-default",
    "attr-single-file",
    "build-args-kwargs",
    "bzl-visibility",
    "confusing-name",
    "constant-glob",
    "ctx-actions",
    "ctx-args",
    "deprecated-function",
    "depset-items",
    "depset-iteration",
    "depset-union",
    "dict-concatenation",
    "dict-method-named-arg",
    "duplicated-name",
    "filetype",
    "function-docstring",
    "function-docstring-args",
    "function-docstring-header",
    "function-docstring-return",
    "git-repository",
    "http-archive",
    "integer-division",
    "keyword-positional-params",
    "list-append",
    "load",
    "module-docstring",
    "name-conventions",
    "native-android",
    "native-build",
    "native-cc",
    "native-java",
    "native-package",
    "native-proto",
    "native-py",
    "no-effect",
    "output-group",
    "overly-nested-depset",
    "package-name",
    "package-on-top",
    "positional-args",
    "print",
    "provider-params",
    "redefined-variable",
    "repository-name",
    "return-value",
    "rule-impl-return",
    "skylark-comment",
    "skylark-docstring",
    "string-iteration",
    "uninitialized",
    "unnamed-macro",
    "unreachable",
    "unsorted-dict-items",
    "unused-variable"
  ]
}

func New

func New() *Config

New constructs a Config with default values.

Example

Code:

c := New()
fmt.Print(c.String())

Output:

{
  "type": "auto"
}

func (*Config) FlagSet

func (c *Config) FlagSet(name string, errorHandling flag.ErrorHandling) *flag.FlagSet

FlagSet returns a flag.FlagSet that can be used to override the config.

func (*Config) LoadFile

func (c *Config) LoadFile() error

LoadFile unmarshals JSON file from the ConfigPath field.

func (*Config) LoadReader

func (c *Config) LoadReader(in io.Reader) error

LoadReader unmarshals JSON data from the given reader.

func (*Config) String

func (c *Config) String() string

String renders the config as a formatted JSON string and satisfies the Stringer interface.

func (*Config) Validate

func (c *Config) Validate(args []string) error

Validate checks that the input type, format, and lint modes are correctly set. It computes the final set of warnings used for linting. The tables package is configured as a side-effect.