-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
122 lines (108 loc) · 3.49 KB
/
config.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
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
package parser
import (
"go/token"
"golang.org/x/tools/go/packages"
)
// Config holds some information about the
// parser behaviour.
//
// Some fields of the packages.Config struct may
// appear inside this Config struct, exposing
// them to client customization (others hidden)
type Config struct {
// Tests (from packages.Config) if set, the
// loader includes not just the packages matching
// a particular pattern but also any related test
// packages, including test-only variants of the
// package and the test executable.
//
// For example, when using the go command, loading
// "fmt" with Tests=true returns four packages, with
// IDs "fmt" (the standard package), "fmt [fmt.test]"
// (the package as compiled for the test), "fmt_test"
// (the test functions from source files in package fmt_test),
// and "fmt.test" (the test binary).
//
// In build systems with explicit names for tests,
// setting Tests may have no effect.
Tests bool
// Dir (from packages.Config) is the directory
// in which to run the build system's query tool
// that provides information about the packages.
//
// If Dir is empty, the tool is run in the current
// directory
Dir string
// Env (from packages.Config) is the environment
// to use when invoking the build system's query
// tool.
//
// If Env is nil, the current environment is used.
// As in os/exec's Cmd, only the last value in the
// slice for each environment key is used. To specify
// the setting of only a few variables, append to the
// current environment, as in:
// opt.Env = append(os.Environ(), "GOOS=plan9", "GOARCH=386")
Env []string
// Fset (from packages.Config) provides source
// position information for syntax trees and
// types.
//
// If Fset is nil, go-codegen will create a new
// fileset.
Fset *token.FileSet
// BuildFlags (from packages.Config) is a list of
// command-line flags to be passed through to the
// build system's query tool.
BuildFlags []string
// Focus sets the typename/filename/pkgname/etc that
// the parser should focus.
//
// Note that if you set the focus to be some pkg, the
// filenames, typenames, etc, will continue to be used.
// Only other pkgs will be ignored (the same for filenames,
// typenames, etc)
Focus *Focus
// Logger is an instance of the LoggerCLI, already created
// by the lib user.
//
// Note that by using this field, the value of the LogFlags
// field will be ignored
Logger LoggerCLI
// LogFlags controls the logging configuration of the parser.
// It can be set using the constants LogTrace, LogDebug, LogJSON,
// etc. If the Logger field is set, this field will be ignored.
//
// Note that you can use a bitwise-AND operator to combine
// multiple flags
LogFlags uint64
}
// packagesLoadConfig returns the configuration struct that is used to
// call the packages.Load function
func packagesLoadConfig(config Config) *packages.Config {
if config.Fset == nil {
config.Fset = token.NewFileSet()
}
return &packages.Config{
// Customizable configurations
Env: config.Env,
BuildFlags: config.BuildFlags,
Tests: config.Tests,
Dir: config.Dir,
Fset: config.Fset,
// Constant values, don't exposed
Mode: packagesConfigMode,
Logf: func(format string, args ...interface{}) {
config.Logger.Debug(format, args...)
},
// Not used
Context: nil,
ParseFile: nil,
Overlay: nil,
}
}
const packagesConfigMode = packages.NeedImports |
packages.NeedSyntax |
packages.NeedName |
packages.NeedTypes |
packages.NeedTypesInfo