/
gb.go
77 lines (68 loc) · 2.03 KB
/
gb.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright 2016 The nvim-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.
package fs
import (
"fmt"
"io/ioutil"
"path/filepath"
"strings"
"github.com/pkg/errors"
)
// IsGb check the current buffer directory whether gb directory structure.
// Return the gb project root path and boolean, and sets the context.GbProjectDir.
func IsGb(dir string) (string, bool) {
root, err := GbFindProjectRoot(dir)
if err != nil {
return "", false
}
// Check root directory whether "vendor", and overwrite root path to parent of vendor directory.
if filepath.Base(root) == "vendor" {
root = filepath.Dir(root)
}
// FindGbProjectRoot gets the GOPATH root if go directory structure.
// Recheck use vendor directory.
manifest := filepath.Join(root, "vendor", "manifest")
if IsNotExist(manifest) {
return "", false
}
return root, true
}
// GbFindProjectRoot works upwards from path seaching for the src/ directory
// which identifies the project root.
// Code taken directly from constabulary/gb.
// github.com/constabulary/gb/cmd/path.go
func GbFindProjectRoot(path string) (string, error) {
if path == "" {
return "", errors.New("project root is blank")
}
start := path
for path != filepath.Dir(path) {
root := filepath.Join(path, "src")
if IsNotExist(root) {
path = filepath.Dir(path)
continue
}
return path, nil
}
return "", fmt.Errorf(`could not find project root in "%s" or its parents`, start)
}
// GbProjectName return the gb project name.
func GbProjectName(projectRoot string) string {
return filepath.Base(projectRoot)
}
// GbPackages return the gb packages.
func GbPackages(root string) ([]string, error) {
dir := filepath.Join(root, "src")
paths, err := ioutil.ReadDir(dir)
if err != nil {
errors.Wrapf(err, "could not read %s dir", dir)
}
pkgs := make([]string, 0, len(paths))
for _, path := range paths {
if path.IsDir() && !strings.HasPrefix(path.Name(), "_") {
pkgs = append(pkgs, path.Name())
}
}
return pkgs, nil
}