Inital commit

This commit is contained in:
2026-06-24 13:44:39 -04:00
commit f098d5f170
15 changed files with 471 additions and 0 deletions

37
server/container.go Normal file
View File

@@ -0,0 +1,37 @@
package server
import (
"bufio"
"log"
"os"
"strings"
)
func getServerName(dir string) string {
if err := os.Chdir(dir); err != nil {
log.Fatal(err)
}
file, err := os.Open("docker-compose.yml")
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
name := dir
for scanner.Scan() {
txt := scanner.Text()
txt = strings.TrimSpace(txt)
s, found := strings.CutPrefix(txt, "container_name: ")
if found {
name = s
}
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
return name
}

47
server/new.go Normal file
View File

@@ -0,0 +1,47 @@
package server
import (
"log"
"mc-tool/docker"
"os"
"path/filepath"
"github.com/spf13/cobra"
)
func New(cmd *cobra.Command, args []string) {
dir := args[0]
if err := os.Mkdir(dir, 0755); err != nil {
log.Fatal(err)
}
if err := os.Chdir(dir); err != nil {
log.Fatal(err)
}
compose := docker.NewCompose(dir)
if compose.Type != docker.VANILLA.String() {
if err := os.MkdirAll("data/mods", 0755); err != nil {
log.Fatal(err)
}
if err := os.MkdirAll("data/config", 0755); err != nil {
log.Fatal(err)
}
}
if err := os.MkdirAll("data/world", 0755); err != nil {
log.Fatal(err)
}
if err := writeFile("docker-compose.yml", compose.String()); err != nil {
log.Fatal(err)
}
}
func writeFile(path string, data string) error {
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
return err
}
return os.WriteFile(path, []byte(data), 0644)
}

21
server/restart.go Normal file
View File

@@ -0,0 +1,21 @@
package server
import (
"log"
"os"
"os/exec"
"github.com/spf13/cobra"
)
func Restart(cmd *cobra.Command, args []string) {
dir := args[0]
if err := os.Chdir(dir); err != nil {
log.Fatal(err)
}
start_cmd := exec.Command("docker", "compose", "restart")
if err := start_cmd.Run(); err != nil {
log.Fatal(err)
}
}

31
server/shell.go Normal file
View File

@@ -0,0 +1,31 @@
package server
import (
"log"
"os/exec"
"strings"
"github.com/spf13/cobra"
)
func Shell(cmd *cobra.Command, args []string) {
dir := args[0]
name := getServerName(dir)
start_cmd := exec.Command("docker", "exec", name, "rcon-cli")
if err := start_cmd.Run(); err != nil {
log.Fatal(err)
}
}
func Say(cmd *cobra.Command, args []string) {
dir := args[0]
name := getServerName(dir)
start_cmd := exec.Command("docker", "exec", name, "rcon-cli", strings.Join(args[1:], " "))
if err := start_cmd.Run(); err != nil {
log.Fatal(err)
}
}

21
server/start.go Normal file
View File

@@ -0,0 +1,21 @@
package server
import (
"log"
"os"
"os/exec"
"github.com/spf13/cobra"
)
func Start(cmd *cobra.Command, args []string) {
dir := args[0]
if err := os.Chdir(dir); err != nil {
log.Fatal(err)
}
start_cmd := exec.Command("docker", "compose", "up", "-d")
if err := start_cmd.Run(); err != nil {
log.Fatal(err)
}
}

28
server/status.go Normal file
View File

@@ -0,0 +1,28 @@
package server
import (
"bytes"
"fmt"
"log"
"os/exec"
"github.com/spf13/cobra"
)
func Status(cmd *cobra.Command, args []string) {
dir := args[0]
name := getServerName(dir)
start_cmd := exec.Command("docker", "exec", name, "rcon-cli", "list")
var outb, errb bytes.Buffer
start_cmd.Stdout = &outb
start_cmd.Stderr = &errb
if err := start_cmd.Run(); err != nil {
log.Fatal(err)
}
fmt.Println(outb.String())
}

21
server/stop.go Normal file
View File

@@ -0,0 +1,21 @@
package server
import (
"log"
"os"
"os/exec"
"github.com/spf13/cobra"
)
func Stop(cmd *cobra.Command, args []string) {
dir := args[0]
if err := os.Chdir(dir); err != nil {
log.Fatal(err)
}
start_cmd := exec.Command("docker", "compose", "down")
if err := start_cmd.Run(); err != nil {
log.Fatal(err)
}
}