Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
nothub committed Feb 11, 2023
1 parent 23d17a5 commit 4914e40
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 221 deletions.
8 changes: 4 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ var rootCmd = &cobra.Command{
if len(args) > 1 {
version = args[1]
}
index, archivePath := handleArgs(input, version, opts.ServerDir, opts.Host)
index, zipPath := handleArgs(input, version, opts.ServerDir, opts.Host)

for _, file := range index.Files {
ok, err := util.PathIsSubpath(file.Path, opts.ServerDir)
Expand Down Expand Up @@ -187,16 +187,16 @@ var rootCmd = &cobra.Command{

// overrides
fmt.Println("Extracting overrides...")
err := mrpack.ExtractOverrides(archivePath, opts.ServerDir)
err := mrpack.ExtractOverrides(zipPath, opts.ServerDir)
if err != nil {
log.Fatalln(err)
}

packInfo, err := update.GenerateModPackInfo(index)
packInfo, err := update.BuildPackState(zipPath)
if err != nil {
fmt.Println(err)
}
err = packInfo.Write(path.Join(opts.ServerDir, "modpack.json"))
err = packInfo.Save(path.Join(opts.ServerDir, "modpack.json"))
if err != nil {
fmt.Println(err)
}
Expand Down
16 changes: 8 additions & 8 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ var updateCmd = &cobra.Command{
if len(args) > 1 {
version = args[1]
}
index, archivePath := handleArgs(input, version, opts.ServerDir, opts.Host)
index, zipPath := handleArgs(input, version, opts.ServerDir, opts.Host)

newPackInfo, err := update.GenerateModPackInfo(index)
newPackInfo, err := update.BuildPackState(zipPath)
if err != nil {
log.Fatalln(err)
}
Expand All @@ -48,20 +48,20 @@ var updateCmd = &cobra.Command{

fmt.Println("Updating:", index.Name)

err = newPackInfo.Write(path.Join(opts.ServerDir, "modpack.json.update"))
err = newPackInfo.Save(path.Join(opts.ServerDir, "modpack.json.update"))
if err != nil {
log.Fatalln(err)
}
oldPackInfo, err := update.ReadModPackInfo(path.Join(opts.ServerDir, "modpack.json"))
oldPackInfo, err := update.LoadPackState(path.Join(opts.ServerDir, "modpack.json"))
if err != nil {
log.Fatalln(err)
}
deleteFileInfo, updateFileInfo, err := update.CompareModPackInfo(*oldPackInfo, *newPackInfo)
deletions, updates, err := update.CompareModPackInfo(*oldPackInfo, *newPackInfo)
if err != nil {
return
}
deletionActions := update.GetDeletionActions(deleteFileInfo, opts.ServerDir)
updateActions := update.GetUpdateActions(updateFileInfo, opts.ServerDir)
deletionActions := update.GetDeletionActions(deletions, opts.ServerDir)
updateActions := update.GetUpdateActions(updates, opts.ServerDir)

fmt.Printf("Would you like to update: [y/N]")
var userInput string
Expand All @@ -79,7 +79,7 @@ var updateCmd = &cobra.Command{
log.Fatalln(err)
}

err = update.ModPackUpdateDo(updateActions, updateFileInfo.Hashes, opts.ServerDir, archivePath, opts.DownloadThreads, opts.RetryTimes)
err = update.ModPackUpdateDo(updateActions, updates.Hashes, opts.ServerDir, zipPath, opts.DownloadThreads, opts.RetryTimes)
if err != nil {
log.Fatalln(err)
}
Expand Down
10 changes: 5 additions & 5 deletions modrinth/mrpack/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,19 @@ type Dependencies struct {
}

func ReadIndex(zipFile string) (*Index, error) {
zipReader, err := zip.OpenReader(zipFile)
r, err := zip.OpenReader(zipFile)
if err != nil {
return nil, err
}
defer func(zipReader *zip.ReadCloser) {
err := zipReader.Close()
defer func(r *zip.ReadCloser) {
err := r.Close()
if err != nil {
fmt.Println(err)
}
}(zipReader)
}(r)

var indexFile *zip.File
for _, file := range zipReader.File {
for _, file := range r.File {
if file.Name == "modrinth.index.json" {
indexFile = file
break
Expand Down
84 changes: 40 additions & 44 deletions modrinth/mrpack/overrides.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"strings"
)

func extractedPath(file *zip.File) (bool, string) {
func extPath(file *zip.File) (bool, string) {
if strings.HasPrefix(file.Name, "overrides/") {
return true, strings.TrimPrefix(file.Name, "overrides/")
}
Expand All @@ -25,54 +25,15 @@ func extractedPath(file *zip.File) (bool, string) {
return false, ""
}

func GetOverrideHashes(zipFile string) (*map[string]string, error) {
hashes := make(map[string]string)

err := util.IterZip(zipFile, func(file *zip.File) error {
ok, p := extractedPath(file)
if !ok {
// skip non-server overrides
return nil
}

reader, err := file.Open()
if err != nil {
return err
}

h, err := chksum.Create(reader, crypto.SHA512.New(), encoding.Hex)
if err != nil {
return err
}

err = reader.Close()
if err != nil {
log.Println(err.Error())
}

hashes[p] = h

return nil
})
if err != nil {
log.Fatalln(err.Error())
}

return &hashes, nil
}

func ExtractOverrides(zipFile string, target string) error {
err := util.IterZip(zipFile, func(file *zip.File) error {
filePath := file.Name
if strings.HasPrefix(filePath, "overrides/") {
filePath = strings.TrimPrefix(filePath, "overrides/")
} else if strings.HasPrefix(filePath, "server-overrides/") {
filePath = strings.TrimPrefix(filePath, "server-overrides/")
} else {
ok, p := extPath(file)
if !ok {
// skip non-server override files
return nil
}

targetPath := path.Join(target, filePath)
targetPath := path.Join(target, p)
ok, err := util.PathIsSubpath(targetPath, target)
if err != nil {
log.Println(err.Error())
Expand Down Expand Up @@ -119,3 +80,38 @@ func ExtractOverrides(zipFile string, target string) error {

return nil
}

func OverrideHashes(zipFile string) map[string]string {
hashes := make(map[string]string)

err := util.IterZip(zipFile, func(file *zip.File) error {
ok, p := extPath(file)
if !ok {
// skip non-server override files
return nil
}

r, err := file.Open()
if err != nil {
return err
}

h, err := chksum.Create(r, crypto.SHA512.New(), encoding.Hex)
if err != nil {
return err
}
hashes[p] = h

err = r.Close()
if err != nil {
log.Println(err.Error())
}

return nil
})
if err != nil {
log.Fatalln(err.Error())
}

return hashes
}
55 changes: 0 additions & 55 deletions update/model.go

This file was deleted.

105 changes: 0 additions & 105 deletions update/packinfo.go

This file was deleted.

Loading

0 comments on commit 4914e40

Please sign in to comment.