Skip to content

Commit

Permalink
Migrate off using git-* shell outs to search Go files
Browse files Browse the repository at this point in the history
  • Loading branch information
cardil committed Jan 19, 2024
1 parent 8bfa3c5 commit 7b61cf6
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 183 deletions.
40 changes: 15 additions & 25 deletions staging/src/k8s.io/code-generator/pkg/codegen/helpers/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ import (
"fmt"
"github.com/spf13/pflag"
"k8s.io/code-generator/pkg/fs"
"k8s.io/code-generator/pkg/osbin/git"
"k8s.io/code-generator/pkg/fs/gosrc"
"k8s.io/code-generator/pkg/osbin/golang"
"k8s.io/klog/v2"
"os"
"path"
"regexp"
)

type genFn func(fs *pflag.FlagSet, args []string) error
Expand All @@ -45,22 +46,8 @@ func (gc genConf) suffix() string {
return gc.fileSuffix
}

type pathspecFn func(*Args) string

func globPathspec(globFmt string) pathspecFn {
return func(args *Args) string {
return fmt.Sprintf(globFmt, args.root)
}
}

func golangPathspec(args *Args) string {
return globPathspec(":(glob)%s/**/*.go")(args)
}

func zzGeneratedPathspec(name string) pathspecFn {
return globPathspec(
fmt.Sprintf(":(glob)%%s/**/zz_generated.%s.go", name),
)
func zzGeneratedPathspec(name string) string {
return fmt.Sprintf("zz_generated.%s.go", name)
}

func (g *Generator) generateGen(args *Args, gc genConf) error {
Expand Down Expand Up @@ -88,11 +75,10 @@ func (g *Generator) generateGen(args *Args, gc genConf) error {

func collectPackages(args *Args, conf genConf) ([]string, error) {
var inputPkgs = make(map[string]bool, 1)
if files, err := git.Grep(
conf.searchPattern,
golangPathspec(args),
git.WithList,
); err != nil {
matcher := gosrc.FileContains(regexp.MustCompile(
regexp.QuoteMeta(conf.searchPattern),
))
if files, err := gosrc.Find(args.root, matcher); err != nil {
return nil, err
} else {
klog.V(3).Infof("Found %d files with %s-gen tags",
Expand All @@ -119,13 +105,14 @@ func collectPackages(args *Args, conf genConf) ([]string, error) {
}

func deleteGenerated(args *Args, gc genConf) error {
pathspec := zzGeneratedPathspec(gc.suffix())(args)
if genFiles, err := git.Find(pathspec); err != nil {
pathspec := zzGeneratedPathspec(gc.suffix())
if genFiles, err := gosrc.Find(args.root, gosrc.FileEndsWith(pathspec)); err != nil {
return err
} else {
klog.V(2).Infof("Deleting %d existing %s helpers",
len(genFiles), gc.name)
for _, file := range genFiles {
for i := len(genFiles) - 1; i >= 0; i-- {
file := genFiles[i]
if _, oserr := os.Stat(file); oserr != nil && os.IsNotExist(oserr) {
continue
}
Expand Down Expand Up @@ -193,6 +180,9 @@ func resolvePackage(file string) (string, error) {
return foundPkg, nil
}

// asBinary runs the given function as if it were the given binary.
// It sets os.Args[0] to binName, and restores it when done. This is required
// so the generator can identify itself as it was executed directly.
func asBinary(binName string, fn func() error) error {
curr := os.Args[0]
defer func() {
Expand Down
103 changes: 103 additions & 0 deletions staging/src/k8s.io/code-generator/pkg/fs/gosrc/search.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
Copyright 2023 The Kubernetes Authors.
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.
*/

package gosrc

import (
"bufio"
"io/fs"
"os"
"path"
"path/filepath"
"regexp"
"strings"
)

// Find returns the list of files matching the given matchers. The search is
// performed recursively from the given root directory. The file is only
// considered if it matches all the given matchers.
func Find(root string, matchers ...FileMatcher) ([]string, error) {
var files []string
err := filepath.WalkDir(root, func(p string, d fs.DirEntry, err error) error {
if err != nil {
return filepath.SkipDir
}
n := path.Base(p)
if !(d.Type().IsRegular() &&
strings.HasSuffix(n, ".go") &&
!strings.HasSuffix(n, "_test.go")) {
return nil
}
for _, m := range matchers {
if !m.MatchFile(p) {
return nil
}
}
files = append(files, p)
return nil

})
return files, err
}

// FileEndsWith returns a FileMatcher that matches files with the given suffix.
func FileEndsWith(suffix string) FileMatcher {
return FileMatcherFunc(func(fp string) bool {
return strings.HasSuffix(fp, suffix)
})
}

// FileContains returns a FileMatcher that matches files containing the given
// pattern.
func FileContains(pattern *regexp.Regexp) FileMatcher {
return FileMatcherFunc(func(fp string) bool {
return match(pattern, fp)
})
}

// FileMatcher matches files.
type FileMatcher interface {
// MatchFile returns true if the given file path matches.
MatchFile(string) bool
}

// FileMatcherFunc is a function that matches files.
type FileMatcherFunc func(string) bool

// MatchFile implements FileMatcher.
func (f FileMatcherFunc) MatchFile(fp string) bool {
return f(fp)
}

func match(pattern *regexp.Regexp, fp string) bool {
// instead of reading the whole file into memory and matching against
// the whole file contents.
// The following code is similar to the following shell command:
// grep -q -e "$pattern" "$fp"
// The -q flag is used to suppress the output.
f, err := os.Open(fp)
if err != nil {
return false
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
if pattern.MatchString(scanner.Text()) {
return true
}
}
return false
}
20 changes: 10 additions & 10 deletions staging/src/k8s.io/code-generator/pkg/fs/rootdir.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/*
Copyright 2023 The Kubernetes Authors.
Copyright 2023 The Kubernetes Authors.
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
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
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.
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.
*/

package fs
Expand Down
70 changes: 0 additions & 70 deletions staging/src/k8s.io/code-generator/pkg/osbin/git/find.go

This file was deleted.

78 changes: 0 additions & 78 deletions staging/src/k8s.io/code-generator/pkg/osbin/git/grep.go

This file was deleted.

0 comments on commit 7b61cf6

Please sign in to comment.