Added the 'new' command, which creates a new minecraft server instance
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed

This commit is contained in:
Gregory Ballantine 2022-09-19 14:38:27 -04:00
parent 92153508b3
commit ccce234ae3
4 changed files with 149 additions and 0 deletions

92
cmd/new.go Normal file
View File

@ -0,0 +1,92 @@
package cmd
import (
"fmt"
"io"
"log"
"net/http"
"os"
"github.com/spf13/cobra"
"git.metaunix.net/BitGoblin/mcst/util"
)
var (
serverName string
minecraftVersion string
)
var newCmd = &cobra.Command{
Use: "new",
Short: "Create a new Minecraft server instance.",
Long: `Create a new Minecraft server instance.`,
Run: func(cmd *cobra.Command, args []string) {
log.Printf("Creating new server with name '%s', and version '%s'\n", serverName, minecraftVersion)
serverDirectoryPath := util.ResolveTilde(fmt.Sprintf("~/%s", serverName))
err := os.Mkdir(serverDirectoryPath, 0755)
if err != nil && !os.IsExist(err) {
log.Fatal(err)
}
var serverJarURL string = "https://piston-data.mojang.com/v1/objects/f69c284232d7c7580bd89a5a4931c3581eae1378/server.jar"
var serverFilePath string = fmt.Sprintf("%s/server_%s.jar", serverDirectoryPath, minecraftVersion)
log.Printf("Downloading %s to %s\n", serverJarURL, serverFilePath)
file, err := os.Create(serverFilePath)
if err != nil {
log.Fatal(err)
}
client := http.Client{
CheckRedirect: func(r *http.Request, via []*http.Request) error {
r.URL.Opaque = r.URL.Path
return nil
},
}
// Put content on file
resp, err := client.Get(serverJarURL)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
_, err = io.Copy(file, resp.Body)
defer file.Close()
// create the shell script to start the server
log.Printf("Creating start.sh shell script.\n")
var scriptFilePath string = fmt.Sprintf("%s/start.sh", serverDirectoryPath)
scriptFile, err := os.Create(scriptFilePath)
if err != nil {
log.Fatalf("Unable to open file: %v", err)
}
defer scriptFile.Close()
// add text to the file
_, err = scriptFile.WriteString(fmt.Sprintf("#!/bin/sh\n\ncd %s\njava -Xmx2048M -Xms2048M -jar server_%s.jar nogui", serverDirectoryPath, minecraftVersion))
if err != nil {
log.Fatalf("Unable to write data: %v", err)
}
// set permissions on the script
err = os.Chmod(scriptFilePath, 0755)
if err != nil {
log.Fatalf("Unable to change script permissions: %v", err)
}
},
}
func init() {
// bind flags to variables
newCmd.Flags().StringVarP(&serverName, "server-name", "n", "", "The name for your new server.")
newCmd.MarkFlagRequired("server-name")
newCmd.Flags().StringVarP(&minecraftVersion, "minecraft-version", "m", "", "Minecraft Java Edition server version to use.")
newCmd.MarkFlagRequired("minecraft-version")
// add this command to the command root
rootCmd.AddCommand(newCmd)
}

View File

@ -7,6 +7,10 @@ import (
"github.com/spf13/cobra"
)
var (
version string
)
var rootCmd = &cobra.Command{
Use: "mcst",
Short: "MCST is a tool to manage Minecraft Java edition servers.",
@ -15,6 +19,7 @@ var rootCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
log.Printf("This is a test.")
},
Version: version,
}
func Start() {

23
util/env.go Normal file
View File

@ -0,0 +1,23 @@
package util
import (
"path/filepath"
"os/user"
"strings"
)
func ResolveTilde(path string) string {
usr, _ := user.Current()
dir := usr.HomeDir
if path == "~" {
// In case of "~", which won't be caught by the "else if"
path = dir
} else if strings.HasPrefix(path, "~/") {
// Use strings.HasPrefix so we don't match paths like
// "/something/~/something/"
path = filepath.Join(dir, path[2:])
}
return path
}

29
util/env_test.go Normal file
View File

@ -0,0 +1,29 @@
package util
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
// define our test suite struct
type EnvTestSuite struct {
suite.Suite
}
// the tilde should expand to user's home directory
func (s *EnvTestSuite) TestResolveTilde() {
resolvedPath := ResolveTilde("~")
assert.NotEqual(s.T(), resolvedPath, "~")
}
// ensure the tilde + relative path gets expanded fully
func (s *EnvTestSuite) TestResolveTildePath() {
resolvedPath := ResolveTilde("~/test")
assert.NotEqual(s.T(), resolvedPath, "~/test")
}
// this is needed to run the test suite
func TestEnvTestSuite(t *testing.T) {
suite.Run(t, new(EnvTestSuite))
}