-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #187 from ulucinar/tagger
Add the buildtagger tool
- Loading branch information
Showing
8 changed files
with
176 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
// Copyright 2024 Upbound Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// main package for the buildtagger application which is used in the | ||
// official provider repositories to generate build tags for the | ||
// provider families. Each family's source modules can be tagged using | ||
// the buildtagger tool. | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/pkg/errors" | ||
"gopkg.in/alecthomas/kingpin.v2" | ||
) | ||
|
||
var ( | ||
app = kingpin.New("buildtagger", "A tool for generating build tags (constraints) for the source modules of the official provider families.").DefaultEnvars() | ||
) | ||
|
||
var ( | ||
parentDir = app.Flag("parent-dir", "Parent directory which will be recursively walked to find the Go source files whose relative path to this parent matches the specified regular expression. The files found will be tagged using the specified build tag.").Default("./").String() | ||
regex = app.Flag("regex", `The regular expression against which a discovered Go source file's relative path or name will be matched. This expression must contain one and only one group whose value will be substituted in the given tag format string. An example is "(.+)/.+/.+\.go"`).Default(".*").String() | ||
tagFormat = app.Flag("tag-format", `A Printf format string to construct the build tag. An example is "(%s || all) && !ignore_autogenerated", where the "%s" format specifier can be replaced by a family resource provider group name.`+ | ||
`There should be a string format specifier for each of the capturing groups specified in the "regex".`).Default("!ignore_autogenerated").String() | ||
mode = app.Flag("mode", `If "file", the file name of the discovered Go source is matched against the given regular expression. If "dir", the relative path of the source file is matched.`).Default("dir").Enum("file", "dir") | ||
deleteTags = app.Flag("delete", `If set, the build tags are removed from the discovered Go sources, instead of being added.`).Default("false").Bool() | ||
) | ||
|
||
// addOrUpdateBuildTag traverses directories from the parent, | ||
// updating or adding build tags in Go files. If a build tag already exists, | ||
// it's replaced with the computed tags. | ||
func addOrUpdateBuildTag(parent, regex, tagFormat, mode string, deleteTags bool) error { | ||
re, err := regexp.Compile(regex) | ||
kingpin.FatalIfError(err, "Failed to compile the given regular expression: %s", regex) | ||
matched := false | ||
err = filepath.Walk(parent, func(path string, info os.FileInfo, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if !info.IsDir() { | ||
matchString, err := getMatchString(parent, path, mode) | ||
if err != nil { | ||
return err | ||
} | ||
matches := re.FindStringSubmatch(matchString) | ||
if len(matches) == 0 { | ||
return nil | ||
} | ||
matched = true | ||
args := make([]any, len(matches)-1) | ||
for i, a := range matches[1:] { | ||
args[i] = a | ||
} | ||
tag := fmt.Sprintf("//go:build "+strings.TrimSpace(tagFormat), args...) | ||
err = updateFileWithBuildTag(path, tag, deleteTags) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to update the source file") | ||
} | ||
} | ||
return nil | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
if matched { | ||
return nil | ||
} | ||
return errors.Errorf("no Go source files under %s matched the regular expression %q", parent, regex) | ||
} | ||
|
||
func getMatchString(parent, path, mode string) (string, error) { | ||
switch mode { | ||
case "file": | ||
// regex is matched against the filename | ||
return filepath.Base(path), nil | ||
case "dir": | ||
// regex is matched against the relative path | ||
matchString, err := filepath.Rel(parent, path) | ||
return matchString, errors.Wrapf(err, "failed to determine the relative path of %s wrt to %s", path, parent) | ||
default: | ||
return "", errors.Errorf("unknown match mode %q", mode) | ||
} | ||
} | ||
|
||
// updateFileWithBuildTag reads a Go file and updates or inserts the specified build tag. | ||
func updateFileWithBuildTag(filePath, buildTag string, deleteTag bool) error { | ||
content, err := os.ReadFile(filepath.Clean(filePath)) | ||
if err != nil { | ||
return errors.Wrapf(err, "failed to read the source file at path %s", filePath) | ||
} | ||
|
||
lines := strings.Split(string(content), "\n") | ||
if len(lines) < 1 { | ||
return nil | ||
} | ||
var updatedLines []string | ||
index, tagExists, emptyLineFollows := getLineStartIndex(lines, deleteTag) | ||
updatedLines = lines[index+1:] | ||
if !deleteTag { | ||
addedLines := [2]string{buildTag} | ||
trimIndex := 2 | ||
if emptyLineFollows && tagExists { | ||
trimIndex = 1 | ||
} | ||
updatedLines = append(addedLines[:trimIndex], updatedLines...) | ||
} | ||
// Write the updated content back to the file | ||
return errors.Wrapf(os.WriteFile(filePath, []byte(strings.Join(updatedLines, "\n")), 0600), "failed to write the source file at path %s", filePath) | ||
} | ||
|
||
func getLineStartIndex(lines []string, deleteTag bool) (int, bool, bool) { | ||
index := -1 | ||
tagExists := false | ||
if strings.HasPrefix(lines[0], "//go:build") { | ||
tagExists = true | ||
index++ | ||
} | ||
emptyLineFollows := len(lines) > 1 && strings.TrimSpace(lines[1]) == "" | ||
if deleteTag && emptyLineFollows && tagExists { | ||
index++ | ||
} | ||
return index, tagExists, emptyLineFollows | ||
} | ||
|
||
func main() { | ||
kingpin.MustParse(app.Parse(os.Args[1:])) | ||
kingpin.FatalIfError(addOrUpdateBuildTag(*parentDir, *regex, *tagFormat, *mode, *deleteTags), "Failed to run the buildtagger...") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
module github.com/upbound/uptest | ||
|
||
go 1.19 | ||
go 1.21 | ||
|
||
require ( | ||
cloud.google.com/go/storage v1.27.0 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters