Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
nothub committed Feb 12, 2023
1 parent f63a299 commit 2d2ff43
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 39 deletions.
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ var rootCmd = &cobra.Command{
log.Fatalln(err)
}

packState, err := update.BuildPackState(zipPath)
packState, err := update.BuildPackState(index, zipPath)
if err != nil {
log.Fatalln(err)
}
Expand Down
12 changes: 2 additions & 10 deletions update/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ const (
//
// Hash must be sha512 and hex encoded.
func GetStrategy(hash string, path string) strategy {
// TODO: replace with ShouldBackup
if !util.PathIsFile(path) {
return NoOp
}
Expand All @@ -44,16 +43,9 @@ func GetStrategy(hash string, path string) strategy {
}
}

// ShouldBackup indicates if the file exists but the hash value does not match.
func ShouldBackup(path string, hash string) bool {
if !util.PathIsFile(path) {
return false
}
match, _ := chksum.VerifyFile(path, hash, crypto.SHA512.New(), encoding.Hex)
if match {
return false
} else {
return true
}
return GetStrategy(hash, path) == Backup
}

func Do(newFiles []File, serverDir string, zipPath string, threads int, retries int) error {
Expand Down
36 changes: 14 additions & 22 deletions update/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,28 @@ import (
"github.com/nothub/mrpack-install/update/backup"
"github.com/nothub/mrpack-install/util"
"log"
"os"
"path/filepath"
"reflect"
)

func Cmd(opts *cmd.UpdateOpts, index *mrpack.Index, zipPath string) {
fmt.Println("Updating:", index.Name)
fmt.Printf("Updating %s with %s", opts.ServerDir, zipPath)

newState, err := BuildPackState(zipPath)
oldState, err := LoadPackState(opts.ServerDir)
if err != nil {
log.Fatalln(err)
}
for filePath := range newState.Files {
util.AssertPathSafe(filePath, opts.ServerDir)
}

oldState, err := LoadPackState(opts.ServerDir)
newState, err := BuildPackState(index, zipPath)
if err != nil {
log.Fatalln(err)
}
for filePath := range newState.Files {
util.AssertPathSafe(filePath, opts.ServerDir)
}

if !reflect.DeepEqual(oldState.Deps, newState.Deps) {
// TODO: server update
// TODO: better message
log.Fatalln("mismatched versions, please upgrade manually")
}

Expand All @@ -45,28 +44,21 @@ func Cmd(opts *cmd.UpdateOpts, index *mrpack.Index, zipPath string) {
}
}

// handle old files
for path := range oldState.Files {
switch GetStrategy(oldState.Files[path], filepath.Join(opts.ServerDir, path)) {
case Delete:
err := os.Remove(filepath.Join(opts.ServerDir, path))
if err != nil {
log.Fatalln(err.Error())
}
case Backup:
for path, hashes := range oldState.Files {
if ShouldBackup(path, hashes.Sha512) {
err := backup.Create(path, opts.ServerDir)
if err != nil {
log.Fatalln(err.Error())
}
}
}

// new files
var newFiles []File
for path := range newState.Files {
var f File
// TODO: correctly handle new files
var newFiles []mrpack.File
for path, hashes := range newState.Files {
var f mrpack.File
f.Path = path
switch GetStrategy(newState.Files[path], filepath.Join(opts.ServerDir, path)) {
switch GetStrategy(hashes.Sha512, filepath.Join(opts.ServerDir, path)) {
case Delete:
delete(newState.Files, path)
case Backup:
Expand Down
7 changes: 1 addition & 6 deletions update/packstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,7 @@ func LoadPackState(serverDir string) (*PackState, error) {
return &state, nil
}

func BuildPackState(zipPath string) (*PackState, error) {
index, err := mrpack.ReadIndex(zipPath)
if err != nil {
return nil, err
}

func BuildPackState(index *mrpack.Index, zipPath string) (*PackState, error) {
var state PackState
state.Name = index.Name
state.Version = index.Version
Expand Down

0 comments on commit 2d2ff43

Please sign in to comment.