Skip to content

Commit 01e8510

Browse files
authored
Merge pull request #296 from vim-volt/lockdir-transaction
Re-implement broken transaction by empty lock directory
2 parents 55a6798 + be5d4d3 commit 01e8510

File tree

8 files changed

+251
-146
lines changed

8 files changed

+251
-146
lines changed

pathutil/pathutil.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,9 @@ func ConfigTOML() string {
193193
return filepath.Join(VoltPath(), "config.toml")
194194
}
195195

196-
// TrxLock returns fullpath of "$HOME/volt/trx.lock".
197-
func TrxLock() string {
198-
return filepath.Join(VoltPath(), "trx.lock")
196+
// TrxDir returns fullpath of "$HOME/volt/trx".
197+
func TrxDir() string {
198+
return filepath.Join(VoltPath(), "trx")
199199
}
200200

201201
// TempDir returns fullpath of "$HOME/tmp".

subcmd/build.go

+12-9
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"os"
77

8-
"github.com/vim-volt/volt/logger"
98
"github.com/vim-volt/volt/subcmd/builder"
109
"github.com/vim-volt/volt/transaction"
1110
)
@@ -53,7 +52,7 @@ Description
5352
return fs
5453
}
5554

56-
func (cmd *buildCmd) Run(args []string) *Error {
55+
func (cmd *buildCmd) Run(args []string) (result *Error) {
5756
// Parse args
5857
fs := cmd.FlagSet()
5958
fs.Parse(args)
@@ -62,18 +61,22 @@ func (cmd *buildCmd) Run(args []string) *Error {
6261
}
6362

6463
// Begin transaction
65-
err := transaction.Create()
64+
trx, err := transaction.Start()
6665
if err != nil {
67-
logger.Error()
68-
return &Error{Code: 11, Msg: "Failed to begin transaction: " + err.Error()}
66+
result = &Error{Code: 11, Msg: "Failed to begin transaction: " + err.Error()}
67+
return
6968
}
70-
defer transaction.Remove()
69+
defer func() {
70+
if err := trx.Done(); err != nil {
71+
result = &Error{Code: 13, Msg: "Failed to end transaction: " + err.Error()}
72+
}
73+
}()
7174

7275
err = builder.Build(cmd.full)
7376
if err != nil {
74-
logger.Error()
75-
return &Error{Code: 12, Msg: "Failed to build: " + err.Error()}
77+
result = &Error{Code: 12, Msg: "Failed to build: " + err.Error()}
78+
return
7679
}
7780

78-
return nil
81+
return
7982
}

subcmd/get.go

+18-10
Original file line numberDiff line numberDiff line change
@@ -192,26 +192,31 @@ func (cmd *getCmd) getReposPathList(args []string, lockJSON *lockjson.LockJSON)
192192
return reposPathList, nil
193193
}
194194

195-
func (cmd *getCmd) doGet(reposPathList []pathutil.ReposPath, lockJSON *lockjson.LockJSON) error {
195+
func (cmd *getCmd) doGet(reposPathList []pathutil.ReposPath, lockJSON *lockjson.LockJSON) (err error) {
196196
// Find matching profile
197197
profile, err := lockJSON.Profiles.FindByName(lockJSON.CurrentProfileName)
198198
if err != nil {
199199
// this must not be occurred because lockjson.Read()
200200
// validates if the matching profile exists
201-
return err
201+
return
202202
}
203203

204204
// Begin transaction
205-
err = transaction.Create()
205+
trx, err := transaction.Start()
206206
if err != nil {
207-
return err
207+
return
208208
}
209-
defer transaction.Remove()
209+
defer func() {
210+
if e := trx.Done(); e != nil {
211+
err = e
212+
}
213+
}()
210214

211215
// Read config.toml
212216
cfg, err := config.Read()
213217
if err != nil {
214-
return errors.Wrap(err, "could not read config.toml")
218+
err = errors.Wrap(err, "could not read config.toml")
219+
return
215220
}
216221

217222
done := make(chan getParallelResult, len(reposPathList))
@@ -252,24 +257,27 @@ func (cmd *getCmd) doGet(reposPathList []pathutil.ReposPath, lockJSON *lockjson.
252257
// Write to lock.json
253258
err = lockJSON.Write()
254259
if err != nil {
255-
return errors.Wrap(err, "could not write to lock.json")
260+
err = errors.Wrap(err, "could not write to lock.json")
261+
return
256262
}
257263
}
258264

259265
// Build ~/.vim/pack/volt dir
260266
err = builder.Build(false)
261267
if err != nil {
262-
return errors.Wrap(err, "could not build "+pathutil.VimVoltDir())
268+
err = errors.Wrap(err, "could not build "+pathutil.VimVoltDir())
269+
return
263270
}
264271

265272
// Show results
266273
for i := range statusList {
267274
fmt.Println(statusList[i])
268275
}
269276
if failed {
270-
return errors.New("failed to install some plugins")
277+
err = errors.New("failed to install some plugins")
278+
return
271279
}
272-
return nil
280+
return
273281
}
274282

275283
func (*getCmd) formatStatus(r *getParallelResult) string {

subcmd/migrate/lockjson.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,28 @@ Description
3131
To suppress this, running this command simply reads and writes migrated structure to lock.json.`
3232
}
3333

34-
func (*lockjsonMigrater) Migrate() error {
34+
func (*lockjsonMigrater) Migrate() (err error) {
3535
// Read lock.json
3636
lockJSON, err := lockjson.ReadNoMigrationMsg()
3737
if err != nil {
3838
return errors.Wrap(err, "could not read lock.json")
3939
}
4040

4141
// Begin transaction
42-
err = transaction.Create()
42+
trx, err := transaction.Start()
4343
if err != nil {
44-
return err
44+
return
4545
}
46-
defer transaction.Remove()
46+
defer func() {
47+
if e := trx.Done(); e != nil {
48+
err = e
49+
}
50+
}()
4751

4852
// Write to lock.json
4953
err = lockJSON.Write()
5054
if err != nil {
5155
return errors.Wrap(err, "could not write to lock.json")
5256
}
53-
return nil
57+
return
5458
}

subcmd/migrate/plugconf-config-func.go

+18-11
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,24 @@ Description
3939
All plugconf files are replaced with new contents.`
4040
}
4141

42-
func (*plugconfConfigMigrater) Migrate() error {
42+
func (*plugconfConfigMigrater) Migrate() (err error) {
4343
// Read lock.json
4444
lockJSON, err := lockjson.ReadNoMigrationMsg()
4545
if err != nil {
46-
return errors.Wrap(err, "could not read lock.json")
46+
err = errors.Wrap(err, "could not read lock.json")
47+
return
4748
}
4849

4950
results, parseErr := plugconf.ParseMultiPlugconf(lockJSON.Repos)
5051
if parseErr.HasErrs() {
5152
logger.Error("Please fix the following errors before migration:")
52-
for _, err := range parseErr.Errors().Errors {
53-
for _, line := range strings.Split(err.Error(), "\n") {
53+
for _, e := range parseErr.Errors().Errors {
54+
for _, line := range strings.Split(e.Error(), "\n") {
5455
logger.Errorf(" %s", line)
5556
}
5657
}
57-
return nil
58+
err = nil
59+
return
5860
}
5961

6062
type plugInfo struct {
@@ -84,22 +86,27 @@ func (*plugconfConfigMigrater) Migrate() error {
8486
os.MkdirAll(filepath.Dir(info.path), 0755)
8587
err = ioutil.WriteFile(info.path, info.content, 0644)
8688
if err != nil {
87-
return err
89+
return
8890
}
8991
}
9092

9193
// Begin transaction
92-
err = transaction.Create()
94+
trx, err := transaction.Start()
9395
if err != nil {
94-
return err
96+
return
9597
}
96-
defer transaction.Remove()
98+
defer func() {
99+
if e := trx.Done(); e != nil {
100+
err = e
101+
}
102+
}()
97103

98104
// Build ~/.vim/pack/volt dir
99105
err = builder.Build(false)
100106
if err != nil {
101-
return errors.Wrap(err, "could not build "+pathutil.VimVoltDir())
107+
err = errors.Wrap(err, "could not build "+pathutil.VimVoltDir())
108+
return
102109
}
103110

104-
return nil
111+
return
105112
}

0 commit comments

Comments
 (0)