Remove pack-root, fix serve command (fixes #39)

index.toml is now the pack root - if you want to replicate
what the pack-root option would have provided, just move
your index.toml (and update the path in pack.toml)
This commit is contained in:
comp500 2021-06-13 19:15:33 +01:00
parent 8cbe7d4c61
commit 76460c5414
3 changed files with 22 additions and 17 deletions

View File

@ -79,5 +79,4 @@ For use on servers, add the `-g` flag to disable the GUI and `-s server` to down
- `mods-folder` The folder to save mod metadata files into, for the install commands (relative to the pack root) - `mods-folder` The folder to save mod metadata files into, for the install commands (relative to the pack root)
- `acceptable-game-versions` A list of additional Minecraft versions to accept when installing or updating mods - `acceptable-game-versions` A list of additional Minecraft versions to accept when installing or updating mods
- `no-internal-hashes` If this is set to true, packwiz will not generate hashes of local files, to prevent merge conflicts and inconsistent hashes when using git/etc. - `no-internal-hashes` If this is set to true, packwiz will not generate hashes of local files, to prevent merge conflicts and inconsistent hashes when using git/etc.
- `packwiz refresh --build` can be used in this mode to generate internal hashes for distributing the pack with packwiz-installer - `packwiz refresh --build` can be used in this mode to generate internal hashes for distributing the pack with packwiz-installer
- `pack-root` A custom directory, containing the modpack files and optional .packwizignore file (defaults to the index file location)

View File

@ -5,6 +5,7 @@ import (
"io" "io"
"net/http" "net/http"
"os" "os"
"path"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
@ -40,17 +41,25 @@ var serveCmd = &cobra.Command{
os.Exit(1) os.Exit(1)
} }
indexPath := filepath.Join(filepath.Dir(viper.GetString("pack-file")), filepath.FromSlash(pack.Index.File)) indexPath := filepath.Join(filepath.Dir(viper.GetString("pack-file")), filepath.FromSlash(pack.Index.File))
indexDir := filepath.Dir(indexPath)
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
path := strings.TrimPrefix(req.URL.Path, "/") urlPath := strings.TrimPrefix(path.Clean("/"+strings.TrimPrefix(req.URL.Path, "/")), "/")
indexRelPath, err := filepath.Rel(indexDir, filepath.FromSlash(urlPath))
if err != nil {
fmt.Println(err)
return
}
indexRelPathSlash := path.Clean(filepath.ToSlash(indexRelPath))
var destPath string
found := false found := false
if path == pack.Index.File { if urlPath == filepath.ToSlash(indexPath) {
found = true found = true
path = indexPath destPath = indexPath
// Must be done here, to ensure all paths gain the lock at some point // Must be done here, to ensure all paths gain the lock at some point
refreshMutex.RLock() refreshMutex.RLock()
} else if path == filepath.ToSlash(viper.GetString("pack-file")) { } else if urlPath == filepath.ToSlash(viper.GetString("pack-file")) {
found = true found = true
if viper.GetBool("serve.refresh") { if viper.GetBool("serve.refresh") {
// Get write lock, to do a refresh // Get write lock, to do a refresh
@ -92,24 +101,25 @@ var serveCmd = &cobra.Command{
refreshMutex.Unlock() refreshMutex.Unlock()
} }
refreshMutex.RLock() refreshMutex.RLock()
destPath = viper.GetString("pack-file")
} else { } else {
refreshMutex.RLock() refreshMutex.RLock()
// Only allow indexed files // Only allow indexed files
for _, v := range index.Files { for _, v := range index.Files {
if path == v.File { if indexRelPathSlash == v.File {
found = true found = true
break break
} }
} }
if found { if found {
path = filepath.Join(filepath.Dir(indexPath), filepath.FromSlash(path)) destPath = filepath.FromSlash(urlPath)
} }
} }
defer refreshMutex.RUnlock() defer refreshMutex.RUnlock()
if found { if found {
f, err := os.Open(path) f, err := os.Open(destPath)
if err != nil { if err != nil {
fmt.Printf("Error reading file \"%s\": %s\n", path, err) fmt.Printf("Error reading file \"%s\": %s\n", destPath, err)
w.WriteHeader(404) w.WriteHeader(404)
_, _ = w.Write([]byte("File not found")) _, _ = w.Write([]byte("File not found"))
return return
@ -120,13 +130,13 @@ var serveCmd = &cobra.Command{
err = err2 err = err2
} }
if err != nil { if err != nil {
fmt.Printf("Error reading file \"%s\": %s\n", path, err) fmt.Printf("Error reading file \"%s\": %s\n", destPath, err)
w.WriteHeader(500) w.WriteHeader(500)
_, _ = w.Write([]byte("Failed to read file")) _, _ = w.Write([]byte("Failed to read file"))
return return
} }
} else { } else {
fmt.Printf("File not found: %s\n", path) fmt.Printf("File not found: %s\n", destPath)
w.WriteHeader(404) w.WriteHeader(404)
_, _ = w.Write([]byte("File not found")) _, _ = w.Write([]byte("File not found"))
return return

View File

@ -166,11 +166,7 @@ func (in *Index) updateFile(path string) error {
} }
func (in Index) GetPackRoot() string { func (in Index) GetPackRoot() string {
packRoot := viper.GetString("pack-root") return filepath.Dir(in.indexFile)
if len(packRoot) == 0 {
packRoot = filepath.Dir(in.indexFile)
}
return packRoot
} }
// Refresh updates the hashes of all the files in the index, and adds new files to the index // Refresh updates the hashes of all the files in the index, and adds new files to the index