Repository navigation
Expand file tree
/
Copy pathargs.go
More file actions
134 lines (104 loc) · 3.3 KB
/
Copy pathargs.go
File metadata and controls
134 lines (104 loc) · 3.3 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
package main
import (
"errors"
"fmt"
"path/filepath"
"strings"
"github.com/alecthomas/kingpin"
"github.com/sourcegraph/lsif-go/internal/git"
protocol "github.com/sourcegraph/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
moduleVersion string
verbosity int
noOutput bool
noAnimation 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)
// Module version options (inferred by git)
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)
}
func parseArgs(args []string) (err error) {
if _, err := app.Parse(args); err != nil {
return err
}
sanitizers := []func() error{sanitizeProjectRoot, sanitizeModuleRoot, sanitizeRepositoryRoot}
validators := []func() error{validatePaths}
for _, f := range append(sanitizers, validators...) {
if err := f(); err != nil {
return 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 = filep
57AF
ath.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 defaultProjectRoot = newCachedString(func() string {
return rel(wd.Value())
})
var defaultModuleRoot = newCachedString(func() string {
return searchForGoMod(wd.Value(), toplevel.Value())
})
var defaultRepositoryRoot = newCachedString(func() string {
return rel(toplevel.Value())
})
var defaultModuleVersion = newCachedString(func() string {
if version, err := git.InferModuleVersion(defaultModuleRoot.Value()); err == nil {
return version
}
return ""
})