-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconfig.go
143 lines (124 loc) · 3 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package gex
import (
"io"
"io/ioutil"
"log"
"os"
"github.com/izumin5210/execx"
"github.com/pkg/errors"
"github.com/spf13/afero"
"github.com/izumin5210/gex/pkg/manager"
"github.com/izumin5210/gex/pkg/manager/dep"
"github.com/izumin5210/gex/pkg/manager/mod"
"github.com/izumin5210/gex/pkg/tool"
)
// Config specifies the configration for managing development tools.
type Config struct {
OutWriter io.Writer
ErrWriter io.Writer
InReader io.Reader
FS afero.Fs
Exec *execx.Executor
WorkingDir string
RootDir string
ManifestName string
BinDirName string
ManagerType manager.Type
Verbose bool
Logger *log.Logger
}
// Default contains default configuration.
var Default = createDefaultConfig()
func createDefaultConfig() *Config {
wd, _ := os.Getwd()
if wd == "" {
wd = "."
}
cfg := &Config{
OutWriter: os.Stdout,
ErrWriter: os.Stderr,
InReader: os.Stdin,
FS: afero.NewOsFs(),
Exec: execx.New(),
WorkingDir: wd,
ManifestName: "tools.go",
BinDirName: "bin",
Logger: log.New(ioutil.Discard, "", 0),
}
cfg.ManagerType, cfg.RootDir = manager.DetectType(cfg.WorkingDir, cfg.FS, cfg.Exec)
return cfg
}
// Create creates a new instance of tool.Repository to manage developemnt tools.
func (c *Config) Create() (tool.Repository, error) {
c.setDefaultsIfNeeded()
manager, executor, err := c.createManager()
if err != nil {
return nil, errors.WithStack(err)
}
return tool.NewRepository(executor, manager, c.ManagerType, &tool.Config{
FS: c.FS,
WorkingDir: c.WorkingDir,
RootDir: c.RootDir,
ManifestName: c.ManifestName,
BinDirName: c.BinDirName,
Verbose: c.Verbose,
Log: c.Logger,
}), nil
}
func (c *Config) setDefaultsIfNeeded() {
d := createDefaultConfig()
if c.OutWriter == nil {
c.OutWriter = d.OutWriter
}
if c.ErrWriter == nil {
c.ErrWriter = d.ErrWriter
}
if c.InReader == nil {
c.InReader = d.InReader
}
if c.FS == nil {
c.FS = d.FS
}
if c.Exec == nil {
c.Exec = d.Exec
}
if c.WorkingDir == "" {
c.WorkingDir = d.WorkingDir
}
if c.ManifestName == "" {
c.ManifestName = d.ManifestName
}
if c.BinDirName == "" {
c.BinDirName = d.BinDirName
}
if c.Logger == nil {
c.Logger = d.Logger
}
if c.ManagerType == manager.TypeUnknown {
c.ManagerType, c.RootDir = manager.DetectType(c.WorkingDir, c.FS, c.Exec)
}
if rootDir, err := manager.FindRoot(c.WorkingDir, c.FS, c.ManifestName); err == nil {
if len(rootDir) > len(c.RootDir) {
c.RootDir = rootDir
}
}
}
func (c *Config) createManager() (
manager.Interface,
manager.Executor,
error,
) {
executor := manager.NewExecutor(c.Exec, c.OutWriter, c.ErrWriter, c.InReader, c.WorkingDir, c.Logger)
var (
m manager.Interface
)
switch c.ManagerType {
case manager.TypeModules:
m = mod.NewManager(executor)
case manager.TypeDep:
m = dep.NewManager(executor, c.RootDir, c.WorkingDir)
default:
return nil, nil, errors.New("failed to detect a dependencies management tool")
}
return m, executor, nil
}