Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

64 linhas
1.4 KiB

  1. // Copyright 2013 The Go Authors. All rights reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file or at
  5. // https://developers.google.com/open-source/licenses/bsd.
  6. package gosrc
  7. import (
  8. "go/build"
  9. "io/ioutil"
  10. "path/filepath"
  11. "strconv"
  12. "time"
  13. )
  14. var localPath string
  15. // SetLocalDevMode sets the package to local development mode. In this mode,
  16. // the GOPATH specified by path is used to find directories instead of version
  17. // control services.
  18. func SetLocalDevMode(path string) {
  19. localPath = path
  20. }
  21. func getLocal(importPath string) (*Directory, error) {
  22. ctx := build.Default
  23. if localPath != "" {
  24. ctx.GOPATH = localPath
  25. }
  26. bpkg, err := ctx.Import(importPath, ".", build.FindOnly)
  27. if err != nil {
  28. return nil, err
  29. }
  30. dir := filepath.Join(bpkg.SrcRoot, filepath.FromSlash(importPath))
  31. fis, err := ioutil.ReadDir(dir)
  32. if err != nil {
  33. return nil, err
  34. }
  35. var modTime time.Time
  36. var files []*File
  37. for _, fi := range fis {
  38. if fi.IsDir() || !isDocFile(fi.Name()) {
  39. continue
  40. }
  41. if fi.ModTime().After(modTime) {
  42. modTime = fi.ModTime()
  43. }
  44. b, err := ioutil.ReadFile(filepath.Join(dir, fi.Name()))
  45. if err != nil {
  46. return nil, err
  47. }
  48. files = append(files, &File{
  49. Name: fi.Name(),
  50. Data: b,
  51. })
  52. }
  53. return &Directory{
  54. ImportPath: importPath,
  55. Etag: strconv.FormatInt(modTime.Unix(), 16),
  56. Files: files,
  57. }, nil
  58. }