forked from golang/gddo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get.go
57 lines (47 loc) · 1.19 KB
/
get.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
// 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 doc fetches Go package documentation from version control services.
package doc
import (
"context"
"go/doc"
"net/http"
"strings"
"github.com/golang/gddo/gosrc"
)
func Get(ctx context.Context, client *http.Client, importPath string, etag string) (*Package, error) {
const versionPrefix = PackageVersion + "-"
if strings.HasPrefix(etag, versionPrefix) {
etag = etag[len(versionPrefix):]
} else {
etag = ""
}
dir, err := gosrc.Get(ctx, client, importPath, etag)
if err != nil {
return nil, err
}
pdoc, err := newPackage(dir)
if err != nil {
return pdoc, err
}
if pdoc.Synopsis == "" &&
pdoc.Doc == "" &&
!pdoc.IsCmd &&
pdoc.Name != "" &&
dir.ImportPath == dir.ProjectRoot &&
len(pdoc.Errors) == 0 {
project, err := gosrc.GetProject(ctx, client, dir.ResolvedPath)
switch {
case err == nil:
pdoc.Synopsis = doc.Synopsis(project.Description)
case gosrc.IsNotFound(err):
// ok
default:
return nil, err
}
}
return pdoc, nil
}