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 f8a6f69 commit f63a299
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 163 deletions.
11 changes: 6 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ var rootCmd = &cobra.Command{
if !util.PathIsFile(path.Join(opts.ServerDir, opts.ServerFile)) {
fmt.Println("Server file not present, downloading...\n(Point --server-dir and --server-file flags to an existing server file to skip this step.)")

inst := server.InstallerFromDeps(&index.Dependencies)
inst := server.InstallerFromDeps(&index.Deps)
err := inst.Install(opts.ServerDir, opts.ServerFile)
if err != nil {
log.Fatalln(err)
Expand Down Expand Up @@ -194,18 +194,19 @@ var rootCmd = &cobra.Command{
log.Fatalln(err)
}

packInfo, err := update.BuildPackState(zipPath)
packState, err := update.BuildPackState(zipPath)
if err != nil {
fmt.Println(err)
log.Fatalln(err)
}
err = packInfo.Save(path.Join(opts.ServerDir, "modpack.json"))
err = packState.Save(opts.ServerDir)
if err != nil {
fmt.Println(err)
log.Fatalln(err)
}

if modsUnclean {
fmt.Println("There have been problems downloading mods, you probably have to fix some dependency problems manually!")
}

fmt.Println("Done :) Have a nice day ✌️")
},
}
Expand Down
94 changes: 2 additions & 92 deletions cmd/update.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package cmd

import (
"fmt"
"github.com/nothub/mrpack-install/update"
"github.com/nothub/mrpack-install/update/backup"
"github.com/nothub/mrpack-install/util"
"github.com/spf13/cobra"
"log"
"os"
"path"
)

func init() {
Expand Down Expand Up @@ -54,95 +50,9 @@ var updateCmd = &cobra.Command{
if len(args) > 1 {
version = args[1]
}
index, zipPath := handleArgs(input, version, opts.ServerDir, opts.Host)
fmt.Println("Updating:", index.Name)

newPackInfo, err := update.BuildPackState(zipPath)
if err != nil {
log.Fatalln(err)
}

for filePath := range newPackInfo.Hashes {
util.AssertPathSafe(filePath, opts.ServerDir)
}

err = newPackInfo.Save(path.Join(opts.ServerDir, "modpack.json.update"))
if err != nil {
log.Fatalln(err)
}
oldPackInfo, err := update.LoadPackState(path.Join(opts.ServerDir, "modpack.json"))
if err != nil {
log.Fatalln(err)
}

// TODO: clean this up (phase 1: collect all required actions phase 2: execute backups) (ignore all deletions here and just overwrite later on?)
deletions, updates, err := update.CompareModPackInfo(*oldPackInfo, *newPackInfo)
if err != nil {
return
}
deletionActions := update.GetDeletionActions(deletions, opts.ServerDir)
updateActions := update.GetUpdateActions(updates, opts.ServerDir)

reportChanges(deletionActions, updateActions)
if !askContinue() {
fmt.Println("Update process canceled.")
return
}

err = update.HandleOldFiles(deletionActions, opts.ServerDir)
if err != nil {
log.Fatalln(err)
}

err = update.Do(updateActions, updates.Hashes, opts.ServerDir, zipPath, opts.DownloadThreads, opts.RetryTimes)
if err != nil {
log.Fatalln(err)
}

util.RemoveEmptyDirs(opts.ServerDir)

err = os.Rename(path.Join(opts.ServerDir, "modpack.json.update"), path.Join(opts.ServerDir, "modpack.json"))
if err != nil {
log.Fatalln(err)
}
index, zipPath := handleArgs(input, version, opts.ServerDir, opts.Host)

fmt.Println("Done :) Have a nice day ✌️")
update.Cmd(opts, index, zipPath)
},
}

func reportChanges(deletions update.Actions, updates update.Actions) {
var changes update.Actions
for filePath, strategy := range deletions {
changes[filePath] = strategy
}
for filePath, strategy := range updates {
changes[filePath] = strategy
}
// TODO: include overrides in change report

fmt.Printf("The following %v changes will be applied:\n", len(changes))
for filePath, strategy := range changes {
switch strategy {
case update.Delete:
log.Printf("Delete and replace: %s\n", filePath)
case update.Backup:
log.Printf("Backup and replace: %s\n", filePath)
case update.NoOp:
log.Printf("Create new file: %s\n", filePath)
}
}
}

func askContinue() bool {
fmt.Printf("Would you like to continue? [y/n]")
var input string
_, err := fmt.Scanln(&input)
if err != nil {
log.Fatalln(err)
}
if input == "y" {
return true
}
fmt.Println("Stopping process.")
return false
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.19

require (
github.com/google/uuid v1.3.0
github.com/nothub/hashutils v0.3.0
github.com/nothub/hashutils v0.4.0
github.com/spf13/cobra v1.6.1
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc=
github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/nothub/hashutils v0.3.0 h1:GCgn/vN6mjpZFd1U5s/lbYDmqXYPFqmGb+p+AdjCk8A=
github.com/nothub/hashutils v0.3.0/go.mod h1:nAmq0uaO7NHP5obrR5EpPhUEM8W8/uJbczWCQlzmCS4=
github.com/nothub/hashutils v0.4.0 h1:/lQiOrqXreZ8HRvNdhVZxyhVlhb7Pk3lXhicropKzow=
github.com/nothub/hashutils v0.4.0/go.mod h1:nAmq0uaO7NHP5obrR5EpPhUEM8W8/uJbczWCQlzmCS4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.6.1 h1:o94oiPyS4KD1mPy2fmcYYHHfCxLqYjJOhGsCHFZtEzA=
github.com/spf13/cobra v1.6.1/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUqzrY=
Expand Down
1 change: 1 addition & 0 deletions modrinth/api/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ type File struct {
Size int `json:"size"`
}

// Hashes are hex encoded.
type Hashes struct {
Sha1 string `json:"sha1"`
Sha512 string `json:"sha512"`
Expand Down
27 changes: 19 additions & 8 deletions modrinth/mrpack/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,24 @@ import (
import modrinth "github.com/nothub/mrpack-install/modrinth/api"

type Index struct {
FormatVersion int `json:"formatVersion"`
Game Game `json:"game"`
VersionId string `json:"versionId"`
Name string `json:"name"`
Summary string `json:"summary"`
Files []File `json:"files"`
Dependencies Dependencies `json:"dependencies"`
Format int `json:"formatVersion"`
Game Game `json:"game"`
Version string `json:"versionId"`
Name string `json:"name"`
Summary string `json:"summary"`
Files []File `json:"files"`
Deps Deps `json:"dependencies"`
}

func (index *Index) ServerDls() []File {
var dls []File
for _, f := range index.Files {
if f.Env.Server == modrinth.UnsupportedEnvSupport {
continue
}
dls = append(dls, f)
}
return dls
}

type Game string
Expand All @@ -39,7 +50,7 @@ type Env struct {
Server modrinth.EnvSupport `json:"server"`
}

type Dependencies struct {
type Deps struct {
Minecraft string `json:"minecraft"`
Fabric string `json:"fabric-loader"`
Quilt string `json:"quilt-loader"`
Expand Down
2 changes: 1 addition & 1 deletion server/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type Installer interface {
Install(serverDir string, serverFile string) error
}

func InstallerFromDeps(deps *mrpack.Dependencies) Installer {
func InstallerFromDeps(deps *mrpack.Deps) Installer {
var flavor Flavor
if deps.Fabric != "" {
flavor = Fabric
Expand Down
69 changes: 15 additions & 54 deletions update/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,22 @@ import (
"github.com/nothub/hashutils/chksum"
"github.com/nothub/hashutils/encoding"
"github.com/nothub/mrpack-install/requester"
"github.com/nothub/mrpack-install/update/backup"
"github.com/nothub/mrpack-install/util"
"io"
"log"
"os"
"path/filepath"
"strings"
)

type Strategy uint8
type strategy uint8

const (
Delete Strategy = iota
Delete strategy = iota
Backup
NoOp
)

// GetFileStrategy selects one of 3 strategies:
// GetStrategy selects one of 3 strategies for handling old files:
//
// 1. NoOp - File does not exist
//
Expand All @@ -33,7 +31,8 @@ const (
// 3. Backup - File exists but hash values do not match
//
// Hash must be sha512 and hex encoded.
func GetFileStrategy(hash string, path string) Strategy {
func GetStrategy(hash string, path string) strategy {
// TODO: replace with ShouldBackup
if !util.PathIsFile(path) {
return NoOp
}
Expand All @@ -45,62 +44,24 @@ func GetFileStrategy(hash string, path string) Strategy {
}
}

type Actions map[string]Strategy

func GetDeletionActions(deletions *PackState, serverDir string) Actions {
actions := make(Actions)
for filePath := range deletions.Hashes {
switch GetFileStrategy(deletions.Hashes[filePath], filepath.Join(serverDir, filePath)) {
case Delete:
actions[filePath] = Delete
case Backup:
actions[filePath] = Backup
}
func ShouldBackup(path string, hash string) bool {
if !util.PathIsFile(path) {
return false
}
return actions
}

func GetUpdateActions(updates *PackState, serverDir string) Actions {
actions := make(Actions)
for filePath := range updates.Hashes {
switch GetFileStrategy(updates.Hashes[filePath], filepath.Join(serverDir, filePath)) {
case Delete:
delete(updates.Hashes, filePath)
case Backup:
err := backup.Create(filePath, serverDir)
if err != nil {
log.Fatalln(err.Error())
}
}
match, _ := chksum.VerifyFile(path, hash, crypto.SHA512.New(), encoding.Hex)
if match {
return false
} else {
return true
}
return actions
}

func HandleOldFiles(deletions Actions, serverDir string) error {
for filePath, strategy := range deletions {
switch strategy {
case Delete:
log.Println("Delete", filePath)
err := os.Remove(filepath.Join(serverDir, filePath))
if err != nil {
return err
}
case Backup:
err := backup.Create(filePath, serverDir)
if err != nil {
return err
}
}
}
return nil
}
func Do(newFiles []File, serverDir string, zipPath string, threads int, retries int) error {

func Do(updates Actions, hashes map[string]string, serverDir string, zipPath string, threads int, retries int) error {
var downloads []*requester.Download
downloadPools := requester.NewDownloadPools(requester.DefaultHttpClient, downloads, threads, retries)

// TODO: combine "updates Actions" and "hashes map[string]string" to a struct that also includes download links
for filePath := range updates {
for filePath := range newFiles {
downloadPools.Downloads = append(downloadPools.Downloads, requester.NewDownload(hashes[filePath].DownloadLink, map[string]string{"sha1": hashes[filePath]}, filepath.Base(filePath), filepath.Join(serverDir, filepath.Dir(filePath))))
}
downloadPools.Do()
Expand Down
Loading

0 comments on commit f63a299

Please sign in to comment.