...

Package labels

import "github.com/bazelbuild/buildtools/labels"
Overview
Index

Overview ▾

Package labels contains helper functions for working with labels.

func Equal

func Equal(label1, label2, pkg string) bool

Equal returns true if label1 and label2 are equal. The function takes care of the optional ":" prefix and differences between long-form labels and local labels (relative to pkg).

func Shorten

func Shorten(input, pkg string) string

Shorten rewrites labels to use the canonical form (the form recommended by build-style). "//foo/bar:bar" => "//foo/bar", or ":bar" if the label belongs to pkg

type Label

Label represents a Bazel target label.

type Label struct {
    Repository string // Repository of the target, can be empty if the target belongs to the current repository
    Package    string // Package of a target, can be empty for top packages
    Target     string // Name of the target, should be always non-empty
}

func Parse

func Parse(target string) Label

Parse parses an absolute Bazel label (eg. //devtools/buildozer:rule) and returns the corresponding Label object.

func ParseRelative

func ParseRelative(input, pkg string) Label

ParseRelative parses a label `input` which may be absolute or relative. If it's relative then it's considered to belong to `pkg`

func (Label) Format

func (l Label) Format() string

Format returns a string representation of a label. It's always absolute but the target name is omitted if it's equal to the package directory, e.g. "//package/foo:foo" is formatted as "//package/foo".

func (Label) FormatRelative

func (l Label) FormatRelative(pkg string) string

FormatRelative returns a string representation of a label relative to `pkg` (relative label if it represents a target in the same package, absolute otherwise)