package bazel import ( "strings" ) // This function only handles fully formed labels, not label expressions, such as // '//...', '//pkg/edge/edgeadmin/...', etc. It does NOT perform any validation of // the input labels because it assumes the input is derived from the output of // Bazel directly. If you need to validate labels, do so before simplifying them. // // Will not simplify repository references for external repositories because // the function does not know the workspace label (i.e., you can only simplify // @edge_infra//pkg/edge to //pkg/edge if you know that you are currently in // the @edge_infra workspace). func SimplifyLabel(l string) string { // remove '//' before splitting on path tokens := strings.Split(strings.TrimPrefix(l, "//"), "/") // get last token in package path, which is the package name + the target. // e.g., pkg/edge/edgeadmin:cli -> cli:cli // then separate target name from it by splitting on ":" targetTokens := strings.Split(tokens[len(tokens)-1], ":") // length of 1 means we already got the simplified label as input, e.g., // pkg/edge/foo if len(targetTokens) == 1 { return strings.Join(tokens, "/") } // simplify target labels referring to target with same name as package // e.g., pkg/edge:edge -> pkg/edge if targetTokens[0] == targetTokens[1] { tokens[len(tokens)-1] = targetTokens[0] } return strings.Join(tokens, "/") } // PackageFromLabel returns the package name for the given label, e.g.: // // //cmd/f8n/foo:container_push -> //cmd/f8n/foo // // If an invalid label is passed in, an empty string is returned. func PackageFromLabel(l string) string { return strings.Split(l, ":")[0] }