Repository navigation
Expand file tree
/
Copy pathargs.go
More file actions
149 lines (116 loc) · 4.23 KB
/
Copy pathargs.go
File metadata and controls
149 lines (116 loc) · 4.23 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"errors"
"fmt"
"path/filepath"
"strings"
"github.com/alecthomas/kingpin"
"github.com/sourcegraph/lsif-go/internal/git"
protocol "github.com/sourcegraph/sourcegraph/lib/codeintel/lsif/protocol"
)
var app = kingpin.New(
"lsif-go",
"lsif-go is an LSIF indexer for Go.",
).Version(version + ", protocol version " + protocol.Version)
var (
outFile string
projectRoot string
moduleRoot string
repositoryRoot string
repositoryRemote string
moduleVersion string
verbosity int
noOutput bool
noAnimation bool
depBatchSize int
enableApiDocs bool
enableImplementations bool
)
func init() {
app.HelpFlag.Short('h')
app.VersionFlag.Short('V')
// Outfile options
app.Flag("output", "The output file.").Short('o').Default("dump.lsif").StringVar(&outFile)
// Path options (inferred by presence of go.mod; git)
app.Flag("project-root", "Specifies the directory to index.").Default(".").StringVar(&projectRoot)
app.Flag("module-root", "Specifies the directory containing the go.mod file.").Default(defaultModuleRoot.Value()).StringVar(&moduleRoot)
app.Flag("repository-root", "Specifies the top-level directory of the git repository.").Default(defaultRepositoryRoot.Value()).StringVar(&repositoryRoot)
// Repository remote and tag options (inferred by git)
app.Flag("repository-remote", "Specifies the canonical name of the repository remote.").Default(defaultRepositoryRemote.Value()).StringVar(&repositoryRemote)
app.Flag("module-version", "Specifies the version of the module defined by module-root.").Default(defaultModuleVersion.Value()).StringVar(&moduleVersion)
// Verbosity options
app.Flag("quiet", "Do not output to stdout or stderr.").Short('q').Default("false").BoolVar(&noOutput)
app.Flag("verbose", "Output debug logs.").Short('v').CounterVar(&verbosity)
app.Flag("no-animation", "Do not animate output.").Default("false").BoolVar(&noAnimation)
app.Flag("dep-batch-size", "How many dependencies to load at once to limit memory usage (e.g. 100). 0 means load all at once.").Default("0").IntVar(&depBatchSize)
// Feature flags
app.Flag("enable-api-docs", "Enable Sourcegraph API Doc generation").Default("true").BoolVar(&enableApiDocs)
app.Flag("enable-implementations", "Enable textDocument/implementation generation").Default("true").BoolVar(&enableImplementations)
}
func parseArgs(args []string) (err error) {
if _, err := app.Parse(args); err != nil {
return fmt.Errorf("failed to parse args: %v", err)
}
sanitizers := []func() error{sanitizeProjectRoot, sanitizeModuleRoot, sanitizeRepositoryRoot}
validators := []func() error{validatePaths}
for _, f := range append(sanitizers, validators...) {
if err := f(); err != nil {
return fmt.Errorf("failed to parse args: %v", err)
}
}
return nil
}
//
// Sanitizers
func sanitizeProjectRoot() (err error) {
projectRoot, err = filepath.Abs(projectRoot)
if err != nil {
return fmt.Errorf("get abspath of project root: %v", err)
}
return nil
}
func sanitizeModuleRoot() (err error) {
moduleRoot, err = filepath.Abs(moduleRoot)
if err != nil {
return fmt.Errorf("get abspath of module root: %v", err)
}
return nil
}
func sanitizeRepositoryRoot() (err error) {
repositoryRoot, err = filepath.Abs(repositoryRoot)
if err != nil {
return fmt.Errorf("get abspath of repository root: %v", err)
}
return nil
}
//
// Validators
func validatePaths() error {
if !strings.HasPrefix(projectRoot, repositoryRoot) {
return errors.New("project root is outside the repository")
}
if !strings.HasPrefix(moduleRoot, repositoryRoot) {
return errors.New("module root is outside the repository")
}
return nil
}
//
// Defaults
var defaultModuleRoot = newCachedString(func() string {
return searchForGoMod(wd.Value(), toplevel.Value())
})
var defaultRepositoryRoot = newCachedString(func() string {
return rel(toplevel.Value())
})
var defaultRepositoryRemote = newCachedString(func() string {
if repo, err := git.InferRepo(defaultModuleRoot.Value()); err == nil {
return repo
}
return ""
})
var defaultModuleVersion = newCachedString(func() string {
if version, err := git.InferModuleVersion(defaultModuleRoot.Value()); err == nil {
return version
}
return ""
})