input.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package input
  2. import (
  3. "fmt"
  4. "path"
  5. "path/filepath"
  6. "go-common/app/tool/gorpc/model"
  7. )
  8. // Returns all the Golang files for the given path. Ignores hidden files.
  9. func Files(srcPath string) ([]model.Path, error) {
  10. srcPath, err := filepath.Abs(srcPath)
  11. if err != nil {
  12. return nil, fmt.Errorf("filepath.Abs: %v\n", err)
  13. }
  14. if filepath.Ext(srcPath) == "" {
  15. return dirFiles(srcPath)
  16. }
  17. return file(srcPath)
  18. }
  19. func dirFiles(srcPath string) ([]model.Path, error) {
  20. ps, err := filepath.Glob(path.Join(srcPath, "*.go"))
  21. if err != nil {
  22. return nil, fmt.Errorf("filepath.Glob: %v\n", err)
  23. }
  24. var srcPaths []model.Path
  25. for _, p := range ps {
  26. src := model.Path(p)
  27. if isHiddenFile(p) || src.IsTestPath() {
  28. continue
  29. }
  30. srcPaths = append(srcPaths, src)
  31. }
  32. return srcPaths, nil
  33. }
  34. func file(srcPath string) ([]model.Path, error) {
  35. src := model.Path(srcPath)
  36. if filepath.Ext(srcPath) != ".go" || isHiddenFile(srcPath) || src.IsTestPath() {
  37. return nil, fmt.Errorf("no Go source files found at %v", srcPath)
  38. }
  39. return []model.Path{src}, nil
  40. }
  41. func isHiddenFile(path string) bool {
  42. return []rune(filepath.Base(path))[0] == '.'
  43. }