Skip to content

Commit

Permalink
server flavor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
nothub committed Sep 7, 2022
1 parent 1f025e1 commit 621bb68
Show file tree
Hide file tree
Showing 13 changed files with 154 additions and 186 deletions.
40 changes: 9 additions & 31 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"github.com/nothub/mrpack-install/modrinth/mrpack"
"github.com/nothub/mrpack-install/requester"
"github.com/nothub/mrpack-install/server"
"github.com/nothub/mrpack-install/util"
"github.com/spf13/cobra"
"log"
"net/url"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -75,10 +75,10 @@ var rootCmd = &cobra.Command{
}

archivePath := ""
if isFilePath(input) {
if util.FileExists(input) {
archivePath = input

} else if isUrl(input) {
} else if util.IsValidUrl(input) {
log.Println("Downloading mrpack file from", args)
file, err := requester.DefaultHttpClient.DownloadFile(input, serverDir, "")
if err != nil {
Expand Down Expand Up @@ -138,31 +138,25 @@ var rootCmd = &cobra.Command{
log.Fatalln(err)
}
log.Println("Installing:", index.Name)

log.Printf("Loader dependencies: %+v\n", index.Dependencies)
log.Printf("Flavor dependencies: %+v\n", index.Dependencies)

// download server if not present
if serverFile != "" && !isFilePath(path.Join(serverDir, serverFile)) {
if serverFile != "" && !util.FileExists(path.Join(serverDir, serverFile)) {
// Determine server platform
var supplier server.DownloadSupplier = nil
var provider server.Provider = nil
if index.Dependencies.Fabric != "" {
supplier = &server.Fabric{
provider = &server.Fabric{
MinecraftVersion: index.Dependencies.Minecraft,
FabricVersion: index.Dependencies.Fabric,
}
} else if index.Dependencies.Quilt != "" || index.Dependencies.Forge != "" {
log.Fatalln("Automatic server deployment not yet implemented for this platform! Supply the path to an existing server jar file with the --server-dir and --server-file flags.")
log.Fatalln("Automatic server deployment not yet implemented for this flavor! Supply the path to an existing server jar file with the --server-dir and --server-file flags.")
} else {
// TODO: vanilla server download
}

// Download server
u, err := supplier.GetUrl()
if err != nil {
log.Fatalln(err)
}
log.Println("Downloading server file from", u)
_, err = requester.DefaultHttpClient.DownloadFile(u, serverDir, serverFile)
err := provider.Provide(serverDir, serverFile)
if err != nil {
log.Fatalln(err)
}
Expand Down Expand Up @@ -204,22 +198,6 @@ var rootCmd = &cobra.Command{
},
}

func isFilePath(s string) bool {
_, err := os.Stat(s)
return err == nil
}

func isUrl(s string) bool {
u, err := url.Parse(s)
if err != nil {
return false
}
if u.Scheme == "" {
return false
}
return true
}

func Execute() {
if rootCmd.Execute() != nil {
os.Exit(1)
Expand Down
57 changes: 23 additions & 34 deletions cmd/server.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
package cmd

import (
"fmt"
"github.com/nothub/mrpack-install/mojang"
"github.com/nothub/mrpack-install/requester"
"github.com/nothub/mrpack-install/server"
"github.com/spf13/cobra"
"log"
"os"
)

func init() {
serverCmd.Flags().String("minecraft-version", "latest", "Minecraft version")
serverCmd.Flags().String("loader-version", "latest", "Mod loader version")
serverCmd.Flags().String("flavor-version", "latest", "Flavor build version")
serverCmd.Flags().String("server-dir", "mc", "Server directory path")
serverCmd.Flags().String("server-file", "", "Server jar file name")
/*
TODO: eula flag
TODO: ops flag
TODO: whitelist flag
TODO: whitelist flags
*/
rootCmd.AddCommand(serverCmd)
}
Expand All @@ -34,7 +31,7 @@ var serverCmd = &cobra.Command{
if err != nil {
log.Fatalln(err)
}
loaderVersion, err := cmd.Flags().GetString("loader-version")
flavorVersion, err := cmd.Flags().GetString("flavor-version")
if err != nil {
log.Fatalln(err)
}
Expand All @@ -55,50 +52,42 @@ var serverCmd = &cobra.Command{
minecraftVersion = latestMinecraftVersion
}

var supplier server.DownloadSupplier = nil
var provider server.Provider = nil
switch args[0] {
case "vanilla":
log.Fatalln("Not yet implemented!")
provider = &server.Vanilla{
MinecraftVersion: minecraftVersion,
}
case "fabric":
supplier = &server.Fabric{
provider = &server.Fabric{
MinecraftVersion: minecraftVersion,
FabricVersion: loaderVersion,
FabricVersion: flavorVersion,
}
case "forge":
log.Fatalln("Not yet implemented!")
case "quilt":
err = os.MkdirAll("work/quilt", 0755)
if err != nil {
log.Fatalln(err)
provider = &server.Quilt{
MinecraftVersion: minecraftVersion,
QuiltVersion: flavorVersion,
}
case "forge":
provider = &server.Forge{
MinecraftVersion: minecraftVersion,
ForgeVersion: flavorVersion,
}
// download https://maven.quiltmc.org/repository/release/org/quiltmc/quilt-installer/latest/quilt-installer-latest.jar
// java -jar quilt-installer-latest.jar install server ${minecraftVersion} --download-server
log.Fatalln("Not yet implemented!")
case "paper":
supplier = &server.Paper{
provider = &server.Paper{
MinecraftVersion: minecraftVersion,
PaperVersion: loaderVersion,
PaperVersion: flavorVersion,
}
case "spigot":
err = os.MkdirAll("work/spigot", 0755)
if err != nil {
log.Fatalln(err)
provider = &server.Spigot{
MinecraftVersion: minecraftVersion,
SpigotVersion: flavorVersion,
}
// download https://hub.spigotmc.org/jenkins/job/BuildTools/lastSuccessfulBuild/artifact/target/BuildTools.jar
// git config --global --unset core.autocrlf
// java -jar BuildTools.jar --rev ${minecraftVersion}
log.Fatalln("Not yet implemented!")
}

url, err := supplier.GetUrl()
err = provider.Provide(serverDir, serverFile)
if err != nil {
log.Fatalln(err)
}

file, err := requester.DefaultHttpClient.DownloadFile(url, serverDir, serverFile)
if err != nil {
return
}
fmt.Println("Server jar downloaded to:", file)
},
}
17 changes: 12 additions & 5 deletions server/fabric.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package server

import (
"errors"
"fmt"
"github.com/nothub/mrpack-install/requester"
"net/url"
)
Expand All @@ -11,28 +12,34 @@ type Fabric struct {
FabricVersion string
}

func (supplier *Fabric) GetUrl() (string, error) {
func (supplier *Fabric) Provide(serverDir string, serverFile string) error {
loaderVersion := supplier.FabricVersion
if loaderVersion == "" || loaderVersion == "latest" {
latestLoaderVersion, err := latestFabricLoaderVersion(supplier.MinecraftVersion)
if err != nil {
return "", err
return err
}
loaderVersion = latestLoaderVersion
}

installerVersion, err := latestFabricInstallerVersion()
if err != nil {
return "", err
return err
}

versionTriple := supplier.MinecraftVersion + "/" + loaderVersion + "/" + installerVersion
u, err := url.Parse("https://meta.fabricmc.net/v2/versions/loader/" + versionTriple + "/server/jar")
if err != nil {
return "", err
return err
}

file, err := requester.DefaultHttpClient.DownloadFile(u.String(), serverDir, serverFile)
if err != nil {
return err
}

return u.String(), nil
fmt.Println("Server jar downloaded to:", file)
return nil
}

func latestFabricLoaderVersion(mcVer string) (string, error) {
Expand Down
56 changes: 0 additions & 56 deletions server/fabric_test.go

This file was deleted.

12 changes: 12 additions & 0 deletions server/forge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package server

import "errors"

type Forge struct {
MinecraftVersion string
ForgeVersion string
}

func (supplier *Forge) Provide(serverDir string, serverFile string) error {
return errors.New("forge provider not yet implemented")
}
4 changes: 2 additions & 2 deletions server/interface.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package server

type DownloadSupplier interface {
GetUrl() (string, error)
type Provider interface {
Provide(serverDir string, serverFile string) error
}
19 changes: 14 additions & 5 deletions server/paper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package server

import (
"errors"
"fmt"
"github.com/nothub/mrpack-install/requester"
"strconv"
)
Expand All @@ -11,7 +12,7 @@ type Paper struct {
PaperVersion string
}

func (supplier *Paper) GetUrl() (string, error) {
func (supplier *Paper) Provide(serverDir string, serverFile string) error {
var response struct {
Builds []struct {
Id int `json:"build"`
Expand All @@ -26,13 +27,21 @@ func (supplier *Paper) GetUrl() (string, error) {
}
err := requester.DefaultHttpClient.GetJson("https://api.papermc.io/v2/projects/paper/versions/"+supplier.MinecraftVersion+"/builds", &response, nil)
if err != nil {
return "", err
return err
}
for i := range response.Builds {
i = len(response.Builds) - 1 - i
if response.Builds[i].Channel == "default" {
return "https://api.papermc.io/v2/projects/paper/versions/" + supplier.MinecraftVersion + "/builds/" + strconv.Itoa(response.Builds[i].Id) + "/downloads/" + response.Builds[i].Downloads.Application.Name, nil
b := response.Builds[i]
if b.Channel == "default" {
u := "https://api.papermc.io/v2/projects/paper/versions/" + supplier.MinecraftVersion + "/builds/" + strconv.Itoa(b.Id) + "/downloads/" + b.Downloads.Application.Name
file, err := requester.DefaultHttpClient.DownloadFile(u, serverDir, serverFile)
if err != nil {
return err
}

fmt.Println("Server jar downloaded to:", file)
return nil
}
}
return "", errors.New("no stable paper release found")
return errors.New("no stable paper release found")
}
Loading

0 comments on commit 621bb68

Please sign in to comment.