Skip to content

Commit

Permalink
Merge pull request #296 from vim-volt/lockdir-transaction
Browse files Browse the repository at this point in the history
Re-implement broken transaction by empty lock directory
  • Loading branch information
tyru authored May 26, 2019
2 parents 55a6798 + be5d4d3 commit 01e8510
Show file tree
Hide file tree
Showing 8 changed files with 251 additions and 146 deletions.
6 changes: 3 additions & 3 deletions pathutil/pathutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,9 @@ func ConfigTOML() string {
return filepath.Join(VoltPath(), "config.toml")
}

// TrxLock returns fullpath of "$HOME/volt/trx.lock".
func TrxLock() string {
return filepath.Join(VoltPath(), "trx.lock")
// TrxDir returns fullpath of "$HOME/volt/trx".
func TrxDir() string {
return filepath.Join(VoltPath(), "trx")
}

// TempDir returns fullpath of "$HOME/tmp".
Expand Down
21 changes: 12 additions & 9 deletions subcmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"os"

"github.com/vim-volt/volt/logger"
"github.com/vim-volt/volt/subcmd/builder"
"github.com/vim-volt/volt/transaction"
)
Expand Down Expand Up @@ -53,7 +52,7 @@ Description
return fs
}

func (cmd *buildCmd) Run(args []string) *Error {
func (cmd *buildCmd) Run(args []string) (result *Error) {
// Parse args
fs := cmd.FlagSet()
fs.Parse(args)
Expand All @@ -62,18 +61,22 @@ func (cmd *buildCmd) Run(args []string) *Error {
}

// Begin transaction
err := transaction.Create()
trx, err := transaction.Start()
if err != nil {
logger.Error()
return &Error{Code: 11, Msg: "Failed to begin transaction: " + err.Error()}
result = &Error{Code: 11, Msg: "Failed to begin transaction: " + err.Error()}
return
}
defer transaction.Remove()
defer func() {
if err := trx.Done(); err != nil {
result = &Error{Code: 13, Msg: "Failed to end transaction: " + err.Error()}
}
}()

err = builder.Build(cmd.full)
if err != nil {
logger.Error()
return &Error{Code: 12, Msg: "Failed to build: " + err.Error()}
result = &Error{Code: 12, Msg: "Failed to build: " + err.Error()}
return
}

return nil
return
}
28 changes: 18 additions & 10 deletions subcmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,26 +192,31 @@ func (cmd *getCmd) getReposPathList(args []string, lockJSON *lockjson.LockJSON)
return reposPathList, nil
}

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

// Begin transaction
err = transaction.Create()
trx, err := transaction.Start()
if err != nil {
return err
return
}
defer transaction.Remove()
defer func() {
if e := trx.Done(); e != nil {
err = e
}
}()

// Read config.toml
cfg, err := config.Read()
if err != nil {
return errors.Wrap(err, "could not read config.toml")
err = errors.Wrap(err, "could not read config.toml")
return
}

done := make(chan getParallelResult, len(reposPathList))
Expand Down Expand Up @@ -252,24 +257,27 @@ func (cmd *getCmd) doGet(reposPathList []pathutil.ReposPath, lockJSON *lockjson.
// Write to lock.json
err = lockJSON.Write()
if err != nil {
return errors.Wrap(err, "could not write to lock.json")
err = errors.Wrap(err, "could not write to lock.json")
return
}
}

// Build ~/.vim/pack/volt dir
err = builder.Build(false)
if err != nil {
return errors.Wrap(err, "could not build "+pathutil.VimVoltDir())
err = errors.Wrap(err, "could not build "+pathutil.VimVoltDir())
return
}

// Show results
for i := range statusList {
fmt.Println(statusList[i])
}
if failed {
return errors.New("failed to install some plugins")
err = errors.New("failed to install some plugins")
return
}
return nil
return
}

func (*getCmd) formatStatus(r *getParallelResult) string {
Expand Down
14 changes: 9 additions & 5 deletions subcmd/migrate/lockjson.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,28 @@ Description
To suppress this, running this command simply reads and writes migrated structure to lock.json.`
}

func (*lockjsonMigrater) Migrate() error {
func (*lockjsonMigrater) Migrate() (err error) {
// Read lock.json
lockJSON, err := lockjson.ReadNoMigrationMsg()
if err != nil {
return errors.Wrap(err, "could not read lock.json")
}

// Begin transaction
err = transaction.Create()
trx, err := transaction.Start()
if err != nil {
return err
return
}
defer transaction.Remove()
defer func() {
if e := trx.Done(); e != nil {
err = e
}
}()

// Write to lock.json
err = lockJSON.Write()
if err != nil {
return errors.Wrap(err, "could not write to lock.json")
}
return nil
return
}
29 changes: 18 additions & 11 deletions subcmd/migrate/plugconf-config-func.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,24 @@ Description
All plugconf files are replaced with new contents.`
}

func (*plugconfConfigMigrater) Migrate() error {
func (*plugconfConfigMigrater) Migrate() (err error) {
// Read lock.json
lockJSON, err := lockjson.ReadNoMigrationMsg()
if err != nil {
return errors.Wrap(err, "could not read lock.json")
err = errors.Wrap(err, "could not read lock.json")
return
}

results, parseErr := plugconf.ParseMultiPlugconf(lockJSON.Repos)
if parseErr.HasErrs() {
logger.Error("Please fix the following errors before migration:")
for _, err := range parseErr.Errors().Errors {
for _, line := range strings.Split(err.Error(), "\n") {
for _, e := range parseErr.Errors().Errors {
for _, line := range strings.Split(e.Error(), "\n") {
logger.Errorf(" %s", line)
}
}
return nil
err = nil
return
}

type plugInfo struct {
Expand Down Expand Up @@ -84,22 +86,27 @@ func (*plugconfConfigMigrater) Migrate() error {
os.MkdirAll(filepath.Dir(info.path), 0755)
err = ioutil.WriteFile(info.path, info.content, 0644)
if err != nil {
return err
return
}
}

// Begin transaction
err = transaction.Create()
trx, err := transaction.Start()
if err != nil {
return err
return
}
defer transaction.Remove()
defer func() {
if e := trx.Done(); e != nil {
err = e
}
}()

// Build ~/.vim/pack/volt dir
err = builder.Build(false)
if err != nil {
return errors.Wrap(err, "could not build "+pathutil.VimVoltDir())
err = errors.Wrap(err, "could not build "+pathutil.VimVoltDir())
return
}

return nil
return
}
Loading

0 comments on commit 01e8510

Please sign in to comment.