This commit introduces support for running Bedrock servers and sets up context folders for managing server versions and working directories. It includes updates to the .gitignore file, configuration options, and command-line execution logic.
149 lines
4.2 KiB
Go
149 lines
4.2 KiB
Go
package cmd
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
|
|
"github.com/goccy/go-yaml"
|
|
"github.com/urfave/cli/v3"
|
|
"sirherobrine23.com.br/go-bds/bds-cli/app/config"
|
|
"sirherobrine23.com.br/go-bds/bds-cli/cmd/run"
|
|
)
|
|
|
|
// Version holds the current version
|
|
var Version = "development"
|
|
|
|
// Make new cli app
|
|
func App() *cli.Command {
|
|
return &cli.Command{
|
|
Name: "bds",
|
|
Usage: "Tool to run Minecraft server with many integrations",
|
|
Version: fmt.Sprintf("Version: %s\tGo version: %s", Version, strings.ReplaceAll(runtime.Version(), "go", "")),
|
|
EnableShellCompletion: true,
|
|
Before: func(ctx context.Context, c *cli.Command) (context.Context, error) {
|
|
configFile := c.String("config")
|
|
appConfig := new(config.Config)
|
|
|
|
// Config file to parse
|
|
fileConfig, err := os.Open(configFile)
|
|
if os.IsNotExist(err) {
|
|
fileConfig, err = os.Create(configFile)
|
|
if err == nil {
|
|
appConfig = config.DefaultConfig // append to var
|
|
switch strings.ToLower(path.Ext(path.Base(configFile))) {
|
|
case ".json":
|
|
js := json.NewEncoder(fileConfig)
|
|
js.SetIndent("", " ")
|
|
err = js.Encode(config.DefaultConfig)
|
|
case ".yaml", ".yml":
|
|
yml := yaml.NewEncoder(fileConfig)
|
|
err = yml.Encode(config.DefaultConfig)
|
|
default:
|
|
err = fmt.Errorf("invalid file config")
|
|
}
|
|
}
|
|
} else if err == nil {
|
|
switch strings.ToLower(path.Ext(path.Base(configFile))) {
|
|
case ".json":
|
|
js := json.NewDecoder(fileConfig)
|
|
err = js.Decode(appConfig)
|
|
case ".yaml", ".yml":
|
|
yml := yaml.NewDecoder(fileConfig)
|
|
err = yml.Decode(appConfig)
|
|
default:
|
|
err = fmt.Errorf("invalid file config")
|
|
}
|
|
}
|
|
|
|
// Close file
|
|
if fileConfig != nil {
|
|
defer fileConfig.Close()
|
|
}
|
|
if err == io.EOF {
|
|
err = nil
|
|
}
|
|
|
|
if err == nil {
|
|
var versionFolder, cwd, upper, workdir string
|
|
if appConfig.PathRoot == "" {
|
|
versionFolder = appConfig.VersionFolder
|
|
cwd = appConfig.Cwd
|
|
upper = filepath.Join(appConfig.Workdir, "upper")
|
|
workdir = filepath.Join(appConfig.Workdir, "work")
|
|
} else {
|
|
upper = filepath.Join(appConfig.PathRoot, "cwd/storage")
|
|
workdir = filepath.Join(appConfig.PathRoot, "cwd/work")
|
|
cwd = filepath.Join(appConfig.PathRoot, "cwd/exec")
|
|
versionFolder = filepath.Join(appConfig.PathRoot, "versions")
|
|
}
|
|
|
|
if !filepath.IsAbs(versionFolder) {
|
|
if versionFolder, err = filepath.Abs(versionFolder); err != nil {
|
|
return ctx, fmt.Errorf("cannot abs versionFolder path, error: %s", err)
|
|
}
|
|
}
|
|
if !filepath.IsAbs(cwd) {
|
|
if cwd, err = filepath.Abs(cwd); err != nil {
|
|
return ctx, fmt.Errorf("cannot abs cwd path, error: %s", err)
|
|
}
|
|
}
|
|
if !filepath.IsAbs(upper) {
|
|
if upper, err = filepath.Abs(upper); err != nil {
|
|
return ctx, fmt.Errorf("cannot abs upper path, error: %s", err)
|
|
}
|
|
}
|
|
if !filepath.IsAbs(workdir) {
|
|
if workdir, err = filepath.Abs(workdir); err != nil {
|
|
return ctx, fmt.Errorf("cannot abs workdir path, error: %s", err)
|
|
}
|
|
}
|
|
|
|
if _, err = os.Stat(versionFolder); os.IsNotExist(err) {
|
|
if err = os.MkdirAll(versionFolder, 0777); err != nil {
|
|
return ctx, fmt.Errorf("cannot create versionFolder: %s", err)
|
|
}
|
|
}
|
|
if _, err = os.Stat(cwd); os.IsNotExist(err) {
|
|
if err = os.MkdirAll(cwd, 0777); err != nil {
|
|
return ctx, fmt.Errorf("cannot create cwd: %s", err)
|
|
}
|
|
}
|
|
if _, err = os.Stat(upper); os.IsNotExist(err) {
|
|
if err = os.MkdirAll(upper, 0777); err != nil {
|
|
return ctx, fmt.Errorf("cannot create upper: %s", err)
|
|
}
|
|
}
|
|
if _, err = os.Stat(workdir); os.IsNotExist(err) {
|
|
if err = os.MkdirAll(workdir, 0777); err != nil {
|
|
return ctx, fmt.Errorf("cannot create workdir: %s", err)
|
|
}
|
|
}
|
|
ctx = config.SetFolders(ctx, versionFolder, cwd, upper, workdir)
|
|
}
|
|
|
|
// Context cofig to global cmd
|
|
ctx = config.SetConfigToContext(ctx, appConfig)
|
|
|
|
return ctx, err
|
|
},
|
|
Flags: []cli.Flag{
|
|
&cli.StringFlag{
|
|
Name: "config",
|
|
Value: "./config.yml",
|
|
Usage: "Global config file",
|
|
Aliases: []string{"c"},
|
|
},
|
|
},
|
|
Commands: []*cli.Command{
|
|
run.RunCmd,
|
|
},
|
|
}
|
|
}
|