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