codeinspector.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package codeinspector
  14. import (
  15. "fmt"
  16. "go/ast"
  17. "go/parser"
  18. "go/token"
  19. "io/ioutil"
  20. "strings"
  21. "unicode"
  22. go2idlparser "k8s.io/kubernetes/cmd/libs/go2idl/parser"
  23. "k8s.io/kubernetes/cmd/libs/go2idl/types"
  24. )
  25. // GetPublicFunctions lists all public functions (not methods) from a golang source file.
  26. func GetPublicFunctions(pkg, filePath string) ([]*types.Type, error) {
  27. builder := go2idlparser.New()
  28. data, err := ioutil.ReadFile(filePath)
  29. if err != nil {
  30. return nil, err
  31. }
  32. if err := builder.AddFile(pkg, filePath, data); err != nil {
  33. return nil, err
  34. }
  35. universe, err := builder.FindTypes()
  36. if err != nil {
  37. return nil, err
  38. }
  39. var functions []*types.Type
  40. // Create the AST by parsing src.
  41. fset := token.NewFileSet() // positions are relative to fset
  42. f, err := parser.ParseFile(fset, filePath, nil, 0)
  43. if err != nil {
  44. return nil, fmt.Errorf("failed parse file to list functions: %v", err)
  45. }
  46. // Inspect the AST and print all identifiers and literals.
  47. ast.Inspect(f, func(n ast.Node) bool {
  48. var s string
  49. switch x := n.(type) {
  50. case *ast.FuncDecl:
  51. s = x.Name.Name
  52. // It's a function (not method), and is public, record it.
  53. if x.Recv == nil && isPublic(s) {
  54. functions = append(functions, universe[pkg].Function(x.Name.Name))
  55. }
  56. }
  57. return true
  58. })
  59. return functions, nil
  60. }
  61. // isPublic checks if a given string is a public function name.
  62. func isPublic(myString string) bool {
  63. a := []rune(myString)
  64. a[0] = unicode.ToUpper(a[0])
  65. return myString == string(a)
  66. }
  67. // GetSourceCodeFiles lists golang source code files from directory, excluding sub-directory and tests files.
  68. func GetSourceCodeFiles(dir string) ([]string, error) {
  69. files, err := ioutil.ReadDir(dir)
  70. if err != nil {
  71. return nil, err
  72. }
  73. var filenames []string
  74. for _, file := range files {
  75. if strings.HasSuffix(file.Name(), ".go") && !strings.HasSuffix(file.Name(), "_test.go") {
  76. filenames = append(filenames, file.Name())
  77. }
  78. }
  79. return filenames, nil
  80. }