Skip to content

Commit 68f5d4d

Browse files
committed
refactor: remove unnecessary Read() of config,lock.json
1 parent 1502487 commit 68f5d4d

12 files changed

+83
-145
lines changed

gateway/build.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func (cmd *buildCmd) Run(cmdctx *CmdContext) *Error {
6969
}
7070
defer transaction.Remove()
7171

72-
err = builder.Build(cmd.full)
72+
err = builder.Build(cmd.full, cmdctx.Config, cmdctx.LockJSON)
7373
if err != nil {
7474
logger.Error()
7575
return &Error{Code: 12, Msg: "Failed to build: " + err.Error()}

gateway/builder/builder.go

+6-10
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,26 @@
11
package builder
22

33
import (
4-
"github.com/pkg/errors"
54
"os"
65

6+
"github.com/pkg/errors"
7+
78
"github.com/vim-volt/volt/config"
89
"github.com/vim-volt/volt/gateway/buildinfo"
10+
"github.com/vim-volt/volt/lockjson"
911
"github.com/vim-volt/volt/logger"
1012
"github.com/vim-volt/volt/pathutil"
1113
)
1214

1315
// Builder creates/updates ~/.vim/pack/volt directory
1416
type Builder interface {
15-
Build(buildInfo *buildinfo.BuildInfo, buildReposMap map[pathutil.ReposPath]*buildinfo.Repos) error
17+
Build(buildInfo *buildinfo.BuildInfo, buildReposMap map[pathutil.ReposPath]*buildinfo.Repos, lockJSON *lockjson.LockJSON) error
1618
}
1719

1820
const currentBuildInfoVersion = 2
1921

2022
// Build creates/updates ~/.vim/pack/volt directory
21-
func Build(full bool) error {
22-
// Read config.toml
23-
cfg, err := config.Read()
24-
if err != nil {
25-
return errors.Wrap(err, "could not read config.toml")
26-
}
27-
23+
func Build(full bool, cfg *config.Config, lockJSON *lockjson.LockJSON) error {
2824
// Get builder
2925
blder, err := getBuilder(cfg.Build.Strategy)
3026
if err != nil {
@@ -75,7 +71,7 @@ func Build(full bool) error {
7571
}
7672
}
7773

78-
return blder.Build(buildInfo, buildReposMap)
74+
return blder.Build(buildInfo, buildReposMap, lockJSON)
7975
}
8076

8177
func getBuilder(strategy string) (Builder, error) {

gateway/builder/copy.go

+1-7
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,13 @@ type copyBuilder struct {
2525
BaseBuilder
2626
}
2727

28-
func (builder *copyBuilder) Build(buildInfo *buildinfo.BuildInfo, buildReposMap map[pathutil.ReposPath]*buildinfo.Repos) error {
28+
func (builder *copyBuilder) Build(buildInfo *buildinfo.BuildInfo, buildReposMap map[pathutil.ReposPath]*buildinfo.Repos, lockJSON *lockjson.LockJSON) error {
2929
// Exit if vim executable was not found in PATH
3030
vimExePath, err := pathutil.VimExecutable()
3131
if err != nil {
3232
return err
3333
}
3434

35-
// Read lock.json
36-
lockJSON, err := lockjson.Read()
37-
if err != nil {
38-
return errors.New("could not read lock.json: " + err.Error())
39-
}
40-
4135
// Get current profile's repos list
4236
reposList, err := lockJSON.GetCurrentReposList()
4337
if err != nil {

gateway/builder/symlink.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,13 @@ type symlinkBuilder struct {
2424
}
2525

2626
// TODO: rollback when return err (!= nil)
27-
func (builder *symlinkBuilder) Build(buildInfo *buildinfo.BuildInfo, buildReposMap map[pathutil.ReposPath]*buildinfo.Repos) error {
27+
func (builder *symlinkBuilder) Build(buildInfo *buildinfo.BuildInfo, buildReposMap map[pathutil.ReposPath]*buildinfo.Repos, lockJSON *lockjson.LockJSON) error {
2828
// Exit if vim executable was not found in PATH
2929
if _, err := pathutil.VimExecutable(); err != nil {
3030
return err
3131
}
3232

3333
// Get current profile's repos list
34-
lockJSON, err := lockjson.Read()
35-
if err != nil {
36-
return errors.Wrap(err, "could not read lock.json")
37-
}
3834
reposList, err := lockJSON.GetCurrentReposList()
3935
if err != nil {
4036
return err

gateway/get.go

+3-9
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func (cmd *getCmd) Run(cmdctx *CmdContext) *Error {
134134
return &Error{Code: 13, Msg: "No repositories are specified"}
135135
}
136136

137-
err = cmd.doGet(reposPathList, cmdctx.LockJSON)
137+
err = cmd.doGet(reposPathList, cmdctx.Config, cmdctx.LockJSON)
138138
if err != nil {
139139
return &Error{Code: 20, Msg: err.Error()}
140140
}
@@ -181,7 +181,7 @@ func (cmd *getCmd) getReposPathList(args []string, lockJSON *lockjson.LockJSON)
181181
return reposPathList, nil
182182
}
183183

184-
func (cmd *getCmd) doGet(reposPathList []pathutil.ReposPath, lockJSON *lockjson.LockJSON) error {
184+
func (cmd *getCmd) doGet(reposPathList []pathutil.ReposPath, cfg *config.Config, lockJSON *lockjson.LockJSON) error {
185185
// Find matching profile
186186
profile, err := lockJSON.Profiles.FindByName(lockJSON.CurrentProfileName)
187187
if err != nil {
@@ -197,12 +197,6 @@ func (cmd *getCmd) doGet(reposPathList []pathutil.ReposPath, lockJSON *lockjson.
197197
}
198198
defer transaction.Remove()
199199

200-
// Read config.toml
201-
cfg, err := config.Read()
202-
if err != nil {
203-
return errors.Wrap(err, "could not read config.toml")
204-
}
205-
206200
done := make(chan getParallelResult, len(reposPathList))
207201
getCount := 0
208202
// Invoke installing / upgrading tasks
@@ -249,7 +243,7 @@ func (cmd *getCmd) doGet(reposPathList []pathutil.ReposPath, lockJSON *lockjson.
249243
}
250244

251245
// Build ~/.vim/pack/volt dir
252-
err = builder.Build(false)
246+
err = builder.Build(false, cfg, lockJSON)
253247
if err != nil {
254248
return errors.Wrap(err, "could not build "+pathutil.VimVoltDir())
255249
}

gateway/migrate.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (cmd *migrateCmd) Run(cmdctx *CmdContext) *Error {
6464
return &Error{Code: 10, Msg: "Failed to parse args: " + err.Error()}
6565
}
6666

67-
if err := op.Migrate(); err != nil {
67+
if err := op.Migrate(cmdctx.Config, cmdctx.LockJSON); err != nil {
6868
return &Error{Code: 11, Msg: "Failed to migrate: " + err.Error()}
6969
}
7070

gateway/migrate/lockjson.go

+3-8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package migrate
33
import (
44
"github.com/pkg/errors"
55

6+
"github.com/vim-volt/volt/config"
67
"github.com/vim-volt/volt/lockjson"
78
"github.com/vim-volt/volt/transaction"
89
)
@@ -31,15 +32,9 @@ Description
3132
To suppress this, running this command simply reads and writes migrated structure to lock.json.`
3233
}
3334

34-
func (*lockjsonMigrater) Migrate() error {
35-
// Read lock.json
36-
lockJSON, err := lockjson.ReadNoMigrationMsg()
37-
if err != nil {
38-
return errors.Wrap(err, "could not read lock.json")
39-
}
40-
35+
func (*lockjsonMigrater) Migrate(cfg *config.Config, lockJSON *lockjson.LockJSON) error {
4136
// Begin transaction
42-
err = transaction.Create()
37+
err := transaction.Create()
4338
if err != nil {
4439
return err
4540
}

gateway/migrate/migrater.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package migrate
22

33
import (
4-
"github.com/pkg/errors"
54
"sort"
5+
6+
"github.com/pkg/errors"
7+
"github.com/vim-volt/volt/config"
8+
"github.com/vim-volt/volt/lockjson"
69
)
710

811
// Migrater migrates many kinds of data.
912
type Migrater interface {
10-
Migrate() error
13+
Migrate(cfg *config.Config, lockJSON *lockjson.LockJSON) error
1114
Name() string
1215
Description(brief bool) string
1316
}

gateway/migrate/plugconf-config-func.go

+7-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package migrate
22

33
import (
4-
"github.com/pkg/errors"
54
"io/ioutil"
65
"os"
76
"path/filepath"
87
"strings"
98

9+
"github.com/pkg/errors"
10+
11+
"github.com/vim-volt/volt/config"
1012
"github.com/vim-volt/volt/gateway/builder"
1113
"github.com/vim-volt/volt/lockjson"
1214
"github.com/vim-volt/volt/logger"
@@ -39,13 +41,7 @@ Description
3941
All plugconf files are replaced with new contents.`
4042
}
4143

42-
func (*plugconfConfigMigrater) Migrate() error {
43-
// Read lock.json
44-
lockJSON, err := lockjson.ReadNoMigrationMsg()
45-
if err != nil {
46-
return errors.Wrap(err, "could not read lock.json")
47-
}
48-
44+
func (*plugconfConfigMigrater) Migrate(cfg *config.Config, lockJSON *lockjson.LockJSON) error {
4945
results, parseErr := plugconf.ParseMultiPlugconf(lockJSON.Repos)
5046
if parseErr.HasErrs() {
5147
logger.Error("Please fix the following errors before migration:")
@@ -82,21 +78,21 @@ func (*plugconfConfigMigrater) Migrate() error {
8278
// After checking errors, write the content to files
8379
for _, info := range infoList {
8480
os.MkdirAll(filepath.Dir(info.path), 0755)
85-
err = ioutil.WriteFile(info.path, info.content, 0644)
81+
err := ioutil.WriteFile(info.path, info.content, 0644)
8682
if err != nil {
8783
return err
8884
}
8985
}
9086

9187
// Begin transaction
92-
err = transaction.Create()
88+
err := transaction.Create()
9389
if err != nil {
9490
return err
9591
}
9692
defer transaction.Remove()
9793

9894
// Build ~/.vim/pack/volt dir
99-
err = builder.Build(false)
95+
err = builder.Build(false, cfg, lockJSON)
10096
if err != nil {
10197
return errors.Wrap(err, "could not build "+pathutil.VimVoltDir())
10298
}

0 commit comments

Comments
 (0)